From e40cb529ac635cdb42cff29f5913dac61b903a0c Mon Sep 17 00:00:00 2001 From: alexion Date: Sat, 22 Aug 2026 19:21:58 -0400 Subject: [PATCH] feat(fish): add remote deploy helper --- modules/fish/fish.nix | 5 +++ modules/fish/functions/deploy-to.fish | 53 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 modules/fish/functions/deploy-to.fish diff --git a/modules/fish/fish.nix b/modules/fish/fish.nix index ae11c1b..0aec84a 100644 --- a/modules/fish/fish.nix +++ b/modules/fish/fish.nix @@ -120,6 +120,11 @@ in description = "Copy a file, or recursively copy a source directory into a destination"; body = builtins.readFile ./functions/copy.fish; }; + + deploy-to = { + description = "Deploy the current dotfiles branch to a host through target-local nixos-rebuild"; + body = builtins.readFile ./functions/deploy-to.fish; + }; }; # Rendered by home-manager into ~/.config/fish/config.fish. diff --git a/modules/fish/functions/deploy-to.fish b/modules/fish/functions/deploy-to.fish new file mode 100644 index 0000000..0c8f6e0 --- /dev/null +++ b/modules/fish/functions/deploy-to.fish @@ -0,0 +1,53 @@ +if test (count $argv) -lt 1; or test (count $argv) -gt 2 + echo "usage: deploy-to [branch]" >&2 + return 2 +end + +set host $argv[1] +set branch $argv[2] + +if test -z "$branch" + set branch (git branch --show-current) +end + +if test -z "$branch" + echo "deploy-to: could not determine the current branch" >&2 + return 2 +end + +git rev-parse --show-toplevel >/dev/null 2>&1 +or begin + echo "deploy-to: run this from a git checkout" >&2 + return 2 +end + +set dirty_lines (git status --porcelain) +if test (count $dirty_lines) -gt 0 + echo "deploy-to: refusing to deploy with a dirty local worktree" >&2 + return 1 +end + +git fetch origin +or return $status + +set remote_ref refs/remotes/origin/$branch +git rev-parse --verify --quiet $remote_ref >/dev/null +or begin + echo "deploy-to: origin/$branch does not exist" >&2 + return 1 +end + +set local_head (git rev-parse HEAD) +set remote_head (git rev-parse $remote_ref) + +if test "$local_head" != "$remote_head" + echo "deploy-to: local HEAD differs from origin/$branch" >&2 + echo "deploy-to: push or reset the branch before deploying" >&2 + return 1 +end + +set escaped_branch (string escape -- $branch) +set escaped_host (string escape -- $host) +set remote_command "cd ~/wrk/dotfiles && git fetch origin && git switch -- $escaped_branch && git reset --hard origin/$escaped_branch && sudo nixos-rebuild switch --flake .#$escaped_host --print-build-logs" + +ssh $host $remote_command