From 575db8e7735e5a3bc06a0e41aeee5528e94ebe37 Mon Sep 17 00:00:00 2001 From: Nickolaj Jepsen Date: Thu, 22 Jan 2026 23:12:42 +0100 Subject: [PATCH] feat: add more scripts --- .github/copilot-instructions.md | 1 + modules/scripts/default.nix | 45 +++++++++++++++++++++++------ modules/scripts/kctx.bash | 21 ++++++++++++++ modules/scripts/port-kill.bash | 25 ++++++++++++++++ modules/scripts/reboot-windows.bash | 1 + modules/scripts/screenshot.bash | 2 ++ modules/scripts/ssh-select.bash | 13 +++++++++ 7 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 modules/scripts/kctx.bash create mode 100644 modules/scripts/port-kill.bash create mode 100644 modules/scripts/ssh-select.bash diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index da9edcc..41bc25d 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -88,6 +88,7 @@ Secrets use agenix + agenix-rekey with YubiKey master identity: 1. **New program**: Create `modules/programs/.nix`, guard with `lib.mkIf config.fireproof.desktop.enable` or similar 2. **New homelab service**: Create `modules/homelab/.nix`, add to `modules/homelab/default.nix` imports, and **add a link to the dashboard in `modules/homelab/glance.nix`** 3. **New host**: Run `just new-host `, then add to `hosts/default.nix` +4. **New script**: Always include `set -euo pipefail` at the start of bash scripts. ## Common Patterns diff --git a/modules/scripts/default.nix b/modules/scripts/default.nix index 7fe8c71..8098db8 100644 --- a/modules/scripts/default.nix +++ b/modules/scripts/default.nix @@ -23,14 +23,41 @@ in { systemd # for bootctl and systemctl ]; }) + (makeScript { + path = ./port-kill.bash; + runtimeInputs = with pkgs; [ + lsof + procps + coreutils + ]; + }) + (makeScript { + path = ./ssh-select.bash; + runtimeInputs = with pkgs; [ + fzf + openssh + gawk + gnused + coreutils + ]; + }) + (makeScript { + path = ./kctx.bash; + runtimeInputs = with pkgs; [ + kubectl + fzf + ]; + }) ] - ++ lib.optional config.fireproof.desktop.enable (makeScript { - path = ./screenshot.bash; - runtimeInputs = with pkgs; [ - slurp - grim - satty - wl-clipboard - ]; - }); + ++ lib.optionals config.fireproof.desktop.enable [ + (makeScript { + path = ./screenshot.bash; + runtimeInputs = with pkgs; [ + slurp + grim + satty + wl-clipboard + ]; + }) + ]; } diff --git a/modules/scripts/kctx.bash b/modules/scripts/kctx.bash new file mode 100644 index 0000000..51107f6 --- /dev/null +++ b/modules/scripts/kctx.bash @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -euo pipefail + +if ! command -v kubectl &> /dev/null; then + echo "kubectl not found" + exit 1 +fi + +CONTEXTS=$(kubectl config get-contexts -o name) + +if [ -z "$CONTEXTS" ]; then + echo "No kubernetes contexts found" + exit 1 +fi + +SELECTED=$(echo "$CONTEXTS" | fzf --prompt="Kube Context > " --height=20% --layout=reverse) + +if [ -n "$SELECTED" ]; then + kubectl config use-context "$SELECTED" + echo "Switched to context: $SELECTED" +fi diff --git a/modules/scripts/port-kill.bash b/modules/scripts/port-kill.bash new file mode 100644 index 0000000..613554f --- /dev/null +++ b/modules/scripts/port-kill.bash @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail + +PORT=$1 +if [ -z "$PORT" ]; then + echo "Usage: port-kill " + exit 1 +fi + +# lsof returns exit code 1 if no files found +PID=$(lsof -t -i:"$PORT" || true) + +if [ -z "$PID" ]; then + echo "No process found on port $PORT" + exit 1 +fi + +COMMAND=$(ps -p "$PID" -o comm=) +echo "Process '$COMMAND' (PID $PID) is using port $PORT." +read -p "Kill? [y/N] " -n 1 -r +echo +if [[ $REPLY =~ ^[Yy]$ ]]; then + kill "$PID" + echo "Killed." +fi diff --git a/modules/scripts/reboot-windows.bash b/modules/scripts/reboot-windows.bash index 2044d45..5a3068a 100644 --- a/modules/scripts/reboot-windows.bash +++ b/modules/scripts/reboot-windows.bash @@ -1,4 +1,5 @@ #!/usr/bin/env bash +set -euo pipefail # Find windows entry id using bootctl WINDOWS_ID=$(bootctl list --json=short | jq -r '.[] | select(.title != null) | select(.title | ascii_downcase | contains("windows")) | .id' | head -n 1) diff --git a/modules/scripts/screenshot.bash b/modules/scripts/screenshot.bash index 0851e08..bde8f51 100644 --- a/modules/scripts/screenshot.bash +++ b/modules/scripts/screenshot.bash @@ -1,3 +1,5 @@ #!/usr/bin/env bash +set -euo pipefail + AREA=$(slurp -d) grim -t ppm -g "$AREA" - | satty -f - --initial-tool=arrow --early-exit --copy-command="wl-copy" --action-on-enter="save-to-clipboard" --disable-notifications diff --git a/modules/scripts/ssh-select.bash b/modules/scripts/ssh-select.bash new file mode 100644 index 0000000..e907867 --- /dev/null +++ b/modules/scripts/ssh-select.bash @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Extract hosts from config and known_hosts +# shellcheck disable=SC2002 +hosts=$(cat ~/.ssh/config ~/.ssh/config.d/* 2>/dev/null | grep -P "^Host ([^*]+)$" | awk '{print $2}' ; cat ~/.ssh/known_hosts 2>/dev/null | cut -f 1 -d ' ' | sed -e 's/,.*//g' | sort -u) + +selected=$(echo "$hosts" | fzf --prompt="SSH > " --height=20% --layout=reverse) + +if [ -n "$selected" ]; then + echo "Connecting to $selected..." + ssh "$selected" +fi