diff options
Diffstat (limited to 'infra/deploy.nix')
| -rw-r--r-- | infra/deploy.nix | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/infra/deploy.nix b/infra/deploy.nix new file mode 100644 index 0000000..c828008 --- /dev/null +++ b/infra/deploy.nix @@ -0,0 +1,62 @@ +{ + perSystem = + { lib, pkgs, ... }: + { + apps.infra-deploy = + let + hosts = builtins.attrNames ( + lib.attrsets.filterAttrs (_: type: type == "directory") (builtins.readDir ./hosts) + ); + + bin = pkgs.writeShellApplication { + name = "deploy"; + runtimeInputs = [ + # keep-sorted start + pkgs.fzf + pkgs.git + pkgs.inetutils + pkgs.nixos-rebuild + # keep-sorted end + ]; + text = '' + selected=("$@") + + if [ "''${#selected[@]}" -eq 0 ]; then + readarray -t selected < <(printf '%s\n' ${lib.escapeShellArgs hosts} | fzf --multi) + fi + + if [ "''${#selected[@]}" -eq 0 ]; then + echo "error: no host selected" >&2 + exit 1 + fi + + if [ "''${#selected[@]}" -eq 1 ] && [ "''${selected[0]}" = "all" ]; then + selected=(${lib.escapeShellArgs hosts}) + fi + + rootdir="$(git rev-parse --show-toplevel)" + + for host in "''${selected[@]}"; do + target=() + if [ "$host" != "$(hostname)" ]; then + target=(--target-host "deploy@$host") + fi + + echo "info: deploying $host" + nixos-rebuild switch \ + --no-reexec \ + --use-substitutes \ + --sudo \ + --flake "$rootdir#$host" \ + "''${target[@]}" + done + ''; + }; + in + { + type = "app"; + program = "${bin}/bin/deploy"; + meta.description = "deploy the nixos configurations to the given hosts"; + }; + }; +} |
