flake: add local building

This adds the ability to build a system closure locally and push it to
the remote using `nix copy`
This commit is contained in:
matthewcroughan 2022-01-26 22:19:36 +00:00
parent 6003461f0e
commit 8f960401c4
2 changed files with 17 additions and 4 deletions

View file

@ -31,6 +31,7 @@ Below is a minimal example:
_module.args.nixinate = {
host = "itchy.scratchy.com";
sshUser = "matthew";
buildOn = "remote"; # valid args are "local" or "remote"
};
}
# ... other configuration ...

View file

@ -21,14 +21,26 @@
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}
SYSTEM_CLOSURE=${flake}#nixosConfigurations.${machine}.config.system.build.toplevel
echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}"
echo "👤 SSH User: $SSH_USER"
echo "🌐 SSH Host: $SSH_HOST"
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/ )
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}' )
if [ $BUILD_ON = "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/ )
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
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 )
echo "🤞 Activating configuration on ${machine} via ssh:"
SYSTEM_CLOSURE_PATH=$(${final.nixFlakes}/bin/nix path-info $SYSTEM_CLOSURE)
( set -x; ${final.openssh}/bin/ssh -t $SSH_USER@$SSH_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'"
fi
'';
in
{