2022-01-03 10:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
description = "Nixinate your systems 🕶️";
|
2022-01-26 20:17:20 +00:00
|
|
|
|
inputs = {
|
|
|
|
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-21.11";
|
|
|
|
|
examples.url = "path:./examples";
|
|
|
|
|
};
|
|
|
|
|
outputs = { self, nixpkgs, examples, ... }:
|
2022-01-03 10:50:50 +00:00
|
|
|
|
let
|
|
|
|
|
version = builtins.substring 0 8 self.lastModifiedDate;
|
|
|
|
|
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
|
|
|
|
|
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
|
|
|
|
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; overlays = [ self.overlay ]; });
|
|
|
|
|
in rec
|
|
|
|
|
{
|
|
|
|
|
overlay = final: prev: {
|
|
|
|
|
generateApps = flake:
|
|
|
|
|
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}
|
2022-01-26 22:19:36 +00:00
|
|
|
|
BUILD_ON=${flake.nixosConfigurations.${machine}._module.args.nixinate.buildOn}
|
2022-01-03 10:50:50 +00:00
|
|
|
|
echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}"
|
|
|
|
|
echo "👤 SSH User: $SSH_USER"
|
|
|
|
|
echo "🌐 SSH Host: $SSH_HOST"
|
2022-01-26 22:19:36 +00:00
|
|
|
|
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
|
2022-02-07 00:58:06 +00:00
|
|
|
|
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 )
|
2022-01-26 22:19:36 +00:00
|
|
|
|
else
|
|
|
|
|
echo "_module.args.nixinate.buildOn is not set to a valid value of 'local' or 'remote'"
|
|
|
|
|
fi
|
2022-01-03 10:50:50 +00:00
|
|
|
|
'';
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
nixinate =
|
|
|
|
|
(
|
|
|
|
|
nixpkgs.lib.genAttrs
|
|
|
|
|
validMachines
|
|
|
|
|
(x:
|
|
|
|
|
{
|
|
|
|
|
type = "app";
|
|
|
|
|
program = toString (mkDeployScript x);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
nixinate = forAllSystems (system: nixpkgsFor.${system}.generateApps);
|
2022-01-26 20:17:20 +00:00
|
|
|
|
apps = nixinate.x86_64-linux examples;
|
2022-01-03 10:50:50 +00:00
|
|
|
|
};
|
|
|
|
|
}
|