2022-01-03 10:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
description = "Nixinate your systems 🕶️";
|
2022-01-26 20:17:20 +00:00
|
|
|
|
inputs = {
|
2022-02-07 00:59:29 +00:00
|
|
|
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
2022-01-26 20:17:20 +00:00
|
|
|
|
};
|
2022-03-23 23:18:23 +00:00
|
|
|
|
outputs = { self, nixpkgs, ... }:
|
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
|
2022-02-15 18:19:48 -08:00
|
|
|
|
{
|
2022-01-03 10:50:50 +00:00
|
|
|
|
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}" ));
|
2022-02-15 18:42:53 -08:00
|
|
|
|
mkDeployScript = { machine, dryRun }: let
|
2022-02-15 18:37:50 -08:00
|
|
|
|
inherit (builtins) abort;
|
|
|
|
|
|
|
|
|
|
n = flake.nixosConfigurations.${machine}._module.args.nixinate;
|
2022-02-15 18:39:10 -08:00
|
|
|
|
user = n.sshUser or "root";
|
2022-02-15 18:37:50 -08:00
|
|
|
|
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'";
|
2022-02-15 18:42:53 -08:00
|
|
|
|
switch = if dryRun then "dry-activate" else "switch";
|
2022-02-15 18:37:50 -08:00
|
|
|
|
script = ''
|
|
|
|
|
set -e
|
|
|
|
|
echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}"
|
|
|
|
|
echo "👤 SSH User: ${user}"
|
|
|
|
|
echo "🌐 SSH Host: ${host}"
|
|
|
|
|
'' + (if remote then ''
|
2022-02-28 01:15:27 +00:00
|
|
|
|
echo "🚀 Sending flake to ${machine} via nix copy:"
|
|
|
|
|
( set -x; ${final.nix}/bin/nix copy ${flake} --to ssh://${user}@${host} )
|
2022-01-26 22:19:36 +00:00
|
|
|
|
echo "🤞 Activating configuration on ${machine} via ssh:"
|
2022-02-28 01:15:27 +00:00
|
|
|
|
( set -x; ${final.openssh}/bin/ssh -t ${user}@${host} 'sudo nixos-rebuild ${switch} --flake ${flake}#${machine}' )
|
2022-02-15 18:37:50 -08:00
|
|
|
|
'' else ''
|
2022-02-07 00:58:06 +00:00
|
|
|
|
echo "🔨 Building system closure locally, copying it to remote store and activating it:"
|
2022-02-15 18:42:53 -08:00
|
|
|
|
( set -x; NIX_SSHOPTS="-t" ${final.nixos-rebuild}/bin/nixos-rebuild ${switch} --flake ${flake}#${machine} --target-host ${user}@${host} --use-remote-sudo )
|
2022-02-15 18:37:50 -08:00
|
|
|
|
'');
|
|
|
|
|
in final.writeScript "deploy-${machine}.sh" script;
|
2022-01-03 10:50:50 +00:00
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
nixinate =
|
|
|
|
|
(
|
|
|
|
|
nixpkgs.lib.genAttrs
|
|
|
|
|
validMachines
|
2022-02-15 18:19:48 -08:00
|
|
|
|
(x:
|
|
|
|
|
{
|
2022-01-03 10:50:50 +00:00
|
|
|
|
type = "app";
|
2022-02-15 18:42:53 -08:00
|
|
|
|
program = toString (mkDeployScript {
|
|
|
|
|
machine = x;
|
|
|
|
|
dryRun = false;
|
|
|
|
|
});
|
2022-01-03 10:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
)
|
2022-02-15 18:42:53 -08:00
|
|
|
|
// nixpkgs.lib.genAttrs
|
|
|
|
|
(map (a: a + "-dry-run") validMachines)
|
|
|
|
|
(x:
|
|
|
|
|
{
|
|
|
|
|
type = "app";
|
|
|
|
|
program = toString (mkDeployScript {
|
|
|
|
|
machine = x;
|
|
|
|
|
dryRun = true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
)
|
2022-01-03 10:50:50 +00:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
nixinate = forAllSystems (system: nixpkgsFor.${system}.generateApps);
|
|
|
|
|
};
|
2022-02-15 18:19:48 -08:00
|
|
|
|
}
|