From 6766412769f07fb9671963087e8795b9d5414318 Mon Sep 17 00:00:00 2001 From: Chloe Kever Date: Tue, 15 Feb 2022 18:19:48 -0800 Subject: [PATCH 1/4] Remove trailing whitespace. --- flake.nix | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index ef29de3..1f4fd6f 100644 --- a/flake.nix +++ b/flake.nix @@ -11,7 +11,7 @@ forAllSystems = nixpkgs.lib.genAttrs supportedSystems; nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; overlays = [ self.overlay ]; }); in rec - { + { overlay = final: prev: { generateApps = flake: let @@ -43,8 +43,8 @@ ( nixpkgs.lib.genAttrs validMachines - (x: - { + (x: + { type = "app"; program = toString (mkDeployScript x); } @@ -55,4 +55,4 @@ nixinate = forAllSystems (system: nixpkgsFor.${system}.generateApps); apps = nixinate.x86_64-linux examples; }; -} +} -- 2.45.3 From d9e1a0153fa96b54e4f8251108577cc7dd20b474 Mon Sep 17 00:00:00 2001 From: Chloe Kever Date: Tue, 15 Feb 2022 18:37:50 -0800 Subject: [PATCH 2/4] Define the script within nix, not bash. --- flake.nix | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/flake.nix b/flake.nix index 1f4fd6f..6545d8a 100644 --- a/flake.nix +++ b/flake.nix @@ -17,26 +17,29 @@ 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: final.writeScript "deploy-${machine}.sh" '' - set -e - SSH_USER=${flake.nixosConfigurations.${machine}._module.args.nixinate.sshUser} - SSH_HOST=${flake.nixosConfigurations.${machine}._module.args.nixinate.host} - BUILD_ON=${flake.nixosConfigurations.${machine}._module.args.nixinate.buildOn} - echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}" - echo "👤 SSH User: $SSH_USER" - echo "🌐 SSH Host: $SSH_HOST" - if [ $BUILD_ON = "remote" ]; then + mkDeployScript = machine: let + inherit (builtins) abort; + + n = flake.nixosConfigurations.${machine}._module.args.nixinate; + user = n.sshUser; + 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'"; + script = '' + set -e + echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}" + echo "👤 SSH User: ${user}" + echo "🌐 SSH Host: ${host}" + '' + (if remote then '' echo "🚀 Sending flake to ${machine} via rsync:" - ( set -x; ${final.rsync}/bin/rsync -q -vz --recursive --zc=zstd ${flake}/* $SSH_USER@$SSH_HOST:/tmp/nixcfg/ ) + ( 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 $SSH_USER@$SSH_HOST 'sudo nixos-rebuild switch --flake /tmp/nixcfg#${machine}' ) - elif [ $BUILD_ON = "local" ]; then + ( 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 $SSH_USER@$SSH_HOST --use-remote-sudo ) - else - echo "_module.args.nixinate.buildOn is not set to a valid value of 'local' or 'remote'" - fi - ''; + ( 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 { nixinate = -- 2.45.3 From 48f1c6d1dfd3cdee00499733e575a485ebef5d10 Mon Sep 17 00:00:00 2001 From: Chloe Kever Date: Tue, 15 Feb 2022 18:39:10 -0800 Subject: [PATCH 3/4] `root` as the default user. --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 6545d8a..9165584 100644 --- a/flake.nix +++ b/flake.nix @@ -21,7 +21,7 @@ inherit (builtins) abort; n = flake.nixosConfigurations.${machine}._module.args.nixinate; - user = n.sshUser; + user = n.sshUser or "root"; 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'"; -- 2.45.3 From 2a107a1b7b19d1f349b176d1b3d4f60bb7212901 Mon Sep 17 00:00:00 2001 From: Chloe Kever Date: Tue, 15 Feb 2022 18:42:53 -0800 Subject: [PATCH 4/4] 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; + }); + } + ) ); }; }; -- 2.45.3