From 2a107a1b7b19d1f349b176d1b3d4f60bb7212901 Mon Sep 17 00:00:00 2001 From: Chloe Kever Date: Tue, 15 Feb 2022 18:42:53 -0800 Subject: [PATCH] Add dry runs. Co-authored-by: Mason Mackaman --- flake.nix | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index 9165584..a5f19d1 100644 --- a/flake.nix +++ b/flake.nix @@ -17,7 +17,7 @@ let machines = builtins.attrNames flake.nixosConfigurations; validMachines = final.lib.remove "" (final.lib.forEach machines (x: final.lib.optionalString (flake.nixosConfigurations."${x}"._module.args ? nixinate) "${x}" )); - mkDeployScript = machine: let + mkDeployScript = { machine, dryRun }: let inherit (builtins) abort; n = flake.nixosConfigurations.${machine}._module.args.nixinate; @@ -25,6 +25,7 @@ host = n.host; where = n.buildOn or "remote"; remote = if where == "remote" then true else if where == "local" then false else abort "_module.args.nixinate.buildOn is not set to a valid value of 'local' or 'remote'"; + switch = if dryRun then "dry-activate" else "switch"; script = '' set -e echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}" @@ -34,10 +35,10 @@ echo "🚀 Sending flake to ${machine} via rsync:" ( set -x; ${final.rsync}/bin/rsync -q -vz --recursive --zc=zstd ${flake}/* ${user}@${host}:/tmp/nixcfg/ ) echo "🤞 Activating configuration on ${machine} via ssh:" - ( set -x; ${final.openssh}/bin/ssh -t ${user}@${host} 'sudo nixos-rebuild switch --flake /tmp/nixcfg#${machine}' ) + ( set -x; ${final.openssh}/bin/ssh -t ${user}@${host} 'sudo nixos-rebuild ${switch} --flake /tmp/nixcfg#${machine}' ) '' else '' echo "🔨 Building system closure locally, copying it to remote store and activating it:" - ( set -x; NIX_SSHOPTS="-t" ${final.nixos-rebuild}/bin/nixos-rebuild switch --flake ${flake}#${machine} --target-host ${user}@${host} --use-remote-sudo ) + ( set -x; NIX_SSHOPTS="-t" ${final.nixos-rebuild}/bin/nixos-rebuild ${switch} --flake ${flake}#${machine} --target-host ${user}@${host} --use-remote-sudo ) ''); in final.writeScript "deploy-${machine}.sh" script; in @@ -49,9 +50,23 @@ (x: { type = "app"; - program = toString (mkDeployScript x); + program = toString (mkDeployScript { + machine = x; + dryRun = false; + }); } ) + // nixpkgs.lib.genAttrs + (map (a: a + "-dry-run") validMachines) + (x: + { + type = "app"; + program = toString (mkDeployScript { + machine = x; + dryRun = true; + }); + } + ) ); }; };