Add dry runs.

This commit is contained in:
Chloe Kever 2022-02-11 18:42:23 -08:00
parent ebb3670b83
commit bcd1ff1f86

View file

@ -11,36 +11,42 @@
forAllSystems = nixpkgs.lib.genAttrs supportedSystems; forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; overlays = [ self.overlay ]; }); nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; overlays = [ self.overlay ]; });
in rec in rec
{ {
overlay = final: prev: { overlay = final: prev: {
generateApps = flake: generateApps = flake:
let let
machines = builtins.attrNames flake.nixosConfigurations; machines = builtins.attrNames flake.nixosConfigurations;
validMachines = final.lib.remove "" (final.lib.forEach machines (x: final.lib.optionalString (flake.nixosConfigurations."${x}"._module.args ? nixinate) "${x}" )); 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" '' mkDeployScript = dry: machine: let
set -e inherit (builtins) abort;
SSH_USER=${flake.nixosConfigurations.${machine}._module.args.nixinate.sshUser}
SSH_HOST=${flake.nixosConfigurations.${machine}._module.args.nixinate.host} n = flake.nixosConfigurations.${machine}._module.args.nixinate;
BUILD_ON=${flake.nixosConfigurations.${machine}._module.args.nixinate.buildOn} user = n.sshUser or "root";
SYSTEM_CLOSURE=${flake}#nixosConfigurations.${machine}.config.system.build.toplevel host = n.host;
closure = "${flake}#nixosConfigurations.${machine}.config.system.build.toplevel";
echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}" where = n.buildOn or "remote";
echo "👤 SSH User: $SSH_USER" 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'";
echo "🌐 SSH Host: $SSH_HOST" switch = if dry then "dry-activate" else "switch";
if [ $BUILD_ON = "remote" ]; then script = (if remote then ''
echo "🚀 Sending flake to ${machine} via rsync:" 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:" 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}' ) ( set -x; ${final.openssh}/bin/ssh -t ${user}@${host} 'sudo nixos-rebuild ${switch} --flake /tmp/nixcfg#${machine}' )
elif [ $BUILD_ON = "local" ]; then '' else ''
echo "🔨 Building system closure locally and copying it to remote store:" echo "🔨 Building system closure locally and copying it to remote store:"
( set -x; ${final.nixFlakes}/bin/nix copy --to ssh://$SSH_USER@$SSH_HOST $SYSTEM_CLOSURE ) ( set -x; ${final.nixFlakes}/bin/nix copy --to ssh://${user}@${host} ${closure} )
echo "🤞 Activating configuration on ${machine} via ssh:" echo "🤞 Activating configuration on ${machine} via ssh:"
SYSTEM_CLOSURE_PATH=$(${final.nixFlakes}/bin/nix path-info $SYSTEM_CLOSURE) SYSTEM_CLOSURE_PATH=$(${final.nixFlakes}/bin/nix path-info ${closure})
( set -x; ${final.openssh}/bin/ssh -t $SSH_USER@$SSH_HOST "sudo $SYSTEM_CLOSURE_PATH/bin/switch-to-configuration switch" ) ( set -x; ${final.openssh}/bin/ssh -t ${user}@${host} "sudo $SYSTEM_CLOSURE_PATH/bin/switch-to-configuration ${switch}" )
else '') + ''
echo "_module.args.nixinate.buildOn is not set to a valid value of 'local' or 'remote'" echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}"
fi echo "👤 SSH User: ${user}"
echo "🌐 SSH Host: ${host}"
'';
in final.writeScript "deploy-${machine}.sh" ''
set -e
SYSTEM_CLOSURE=${flake}#nixosConfigurations.${machine}.config.system.build.toplevel
${script}
''; '';
in in
{ {
@ -48,16 +54,24 @@
( (
nixpkgs.lib.genAttrs nixpkgs.lib.genAttrs
validMachines validMachines
(x: (x:
{ {
type = "app"; type = "app";
program = toString (mkDeployScript x); program = toString (mkDeployScript false x);
} }
) )
// nixpkgs.lib.genAttrs
(map (a: a ++ "-dry-run") validMachines)
(x:
{
type = "app";
program = toString (mkDeployScript true x);
}
)
); );
}; };
}; };
nixinate = forAllSystems (system: nixpkgsFor.${system}.generateApps); nixinate = forAllSystems (system: nixpkgsFor.${system}.generateApps);
apps = nixinate.x86_64-linux examples; apps = nixinate.x86_64-linux examples;
}; };
} }