diff options
| author | vkcku <[email protected]> | 2026-06-01 20:04:02 +0530 |
|---|---|---|
| committer | vkcku <[email protected]> | 2026-06-01 20:04:02 +0530 |
| commit | 8ed767242ad201aa2a77b57d021391f1a04f2a30 (patch) | |
| tree | 1a6eda339eb5b2e8b01960cfc3b41f7fff45672c /infra | |
| parent | infra: set treefmt working directory to infra (diff) | |
infra: add deploy script
monorepo-revid: 84b860704659f6c878f887f7e0b6ce61dad9f907
Diffstat (limited to 'infra')
| -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"; + }; + }; +} |
