From 8f960401c4267f5b53e6c4886d0c4035122d2c39 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Wed, 26 Jan 2022 22:19:36 +0000 Subject: [PATCH] flake: add local building This adds the ability to build a system closure locally and push it to the remote using `nix copy` --- README.md | 1 + flake.nix | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b697773..93dfe3d 100644 --- a/README.md +++ b/README.md @@ -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 ... diff --git a/flake.nix b/flake.nix index 73c2b97..356e41e 100644 --- a/flake.nix +++ b/flake.nix @@ -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 {