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-31 20:36:59 +01:00
|
|
|
|
outputs = { self, nixpkgs, ... }@inputs:
|
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" ];
|
2022-03-31 17:40:00 +01:00
|
|
|
|
forSystems = systems: f:
|
|
|
|
|
nixpkgs.lib.genAttrs systems
|
|
|
|
|
(system: f system nixpkgs.legacyPackages.${system});
|
|
|
|
|
forAllSystems = forSystems supportedSystems;
|
|
|
|
|
nixpkgsFor = forAllSystems (system: pkgs: import nixpkgs { inherit system; overlays = [ self.overlay ]; });
|
2022-01-03 10:50:50 +00:00
|
|
|
|
in rec
|
2022-02-15 18:19:48 -08:00
|
|
|
|
{
|
2022-05-24 03:23:19 +01:00
|
|
|
|
herculesCI.ciSystems = [ "x86_64-linux" ];
|
2022-01-03 10:50:50 +00:00
|
|
|
|
overlay = final: prev: {
|
2022-05-23 18:57:50 +01:00
|
|
|
|
nixinate = {
|
|
|
|
|
nix = prev.pkgs.writeShellScriptBin "nix"
|
|
|
|
|
''${final.nixVersions.unstable}/bin/nix --experimental-features "nix-command flakes" "$@"'';
|
|
|
|
|
nixos-rebuild = prev.nixos-rebuild.override { inherit (final) nix; };
|
|
|
|
|
};
|
2022-01-03 10:50:50 +00:00
|
|
|
|
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;
|
2022-08-27 20:55:15 +02:00
|
|
|
|
inherit (final.lib) getExe optionalString concatStringsSep;
|
2022-05-23 18:57:50 +01:00
|
|
|
|
nix = "${getExe final.nix}";
|
|
|
|
|
nixos-rebuild = "${getExe final.nixos-rebuild}";
|
|
|
|
|
openssh = "${getExe final.openssh}";
|
2022-10-26 22:56:41 +01:00
|
|
|
|
flock = "${getExe final.flock}";
|
2022-02-15 18:37:50 -08:00
|
|
|
|
|
|
|
|
|
n = flake.nixosConfigurations.${machine}._module.args.nixinate;
|
2022-12-15 15:08:40 +00:00
|
|
|
|
hermetic = n.hermetic or true;
|
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-06-07 23:19:09 +02:00
|
|
|
|
substituteOnTarget = n.substituteOnTarget or false;
|
2022-02-15 18:42:53 -08:00
|
|
|
|
switch = if dryRun then "dry-activate" else "switch";
|
2022-08-27 20:55:15 +02:00
|
|
|
|
nixOptions = concatStringsSep " " (n.nixOptions or []);
|
2023-04-02 15:08:25 +02:00
|
|
|
|
port = toString n.port or "22";
|
|
|
|
|
nixSSHOPTS = opts: ''NIX_SSHOPTS="-p ${port} ${opts}"'';
|
2022-08-27 20:55:15 +02:00
|
|
|
|
|
2022-05-23 18:57:50 +01:00
|
|
|
|
script =
|
|
|
|
|
''
|
2022-02-15 18:37:50 -08:00
|
|
|
|
set -e
|
|
|
|
|
echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}"
|
|
|
|
|
echo "👤 SSH User: ${user}"
|
|
|
|
|
echo "🌐 SSH Host: ${host}"
|
2023-04-02 15:08:25 +02:00
|
|
|
|
echo "🌐 SSH Port: ${port}"
|
2022-02-15 18:37:50 -08:00
|
|
|
|
'' + (if remote then ''
|
2022-02-28 01:15:27 +00:00
|
|
|
|
echo "🚀 Sending flake to ${machine} via nix copy:"
|
2023-04-02 15:08:25 +02:00
|
|
|
|
( set -x; ${nixSSHOPTS ""} ${nix} ${nixOptions} copy ${flake} --to ssh://${user}@${host} )
|
2022-05-23 18:57:50 +01:00
|
|
|
|
'' + (if hermetic then ''
|
|
|
|
|
echo "🤞 Activating configuration hermetically on ${machine} via ssh:"
|
2023-04-02 15:08:25 +02:00
|
|
|
|
( set -x; ${nixSSHOPTS ""} ${nix} ${nixOptions} copy --derivation ${nixos-rebuild} ${flock} --to ssh://${user}@${host} )
|
|
|
|
|
( set -x; ${openssh} -t ${user}@${host} -p ${port} "sudo nix-store --realise ${nixos-rebuild} ${flock} && sudo ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine}" )
|
2022-02-15 18:37:50 -08:00
|
|
|
|
'' else ''
|
2022-05-23 18:57:50 +01:00
|
|
|
|
echo "🤞 Activating configuration non-hermetically on ${machine} via ssh:"
|
2023-04-02 15:08:25 +02:00
|
|
|
|
( set -x; ${openssh} -t ${user}@${host} -p ${port} "sudo flock -w 60 /dev/shm/nixinate-${machine} nixos-rebuild ${switch} --flake ${flake}#${machine}" )
|
2022-05-23 18:57:50 +01:00
|
|
|
|
'')
|
|
|
|
|
else ''
|
2022-02-07 00:58:06 +00:00
|
|
|
|
echo "🔨 Building system closure locally, copying it to remote store and activating it:"
|
2023-04-02 15:08:25 +02:00
|
|
|
|
( set -x; ${nixSSHOPTS "-t"} ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine} --target-host ${user}@${host} --use-remote-sudo ${optionalString substituteOnTarget "-s"} )
|
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 {
|
2022-03-24 18:45:39 +00:00
|
|
|
|
machine = nixpkgs.lib.removeSuffix "-dry-run" x;
|
2022-02-15 18:42:53 -08:00
|
|
|
|
dryRun = true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
)
|
2022-01-03 10:50:50 +00:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
};
|
2022-03-31 17:40:00 +01:00
|
|
|
|
nixinate = forAllSystems (system: pkgs: nixpkgsFor.${system}.generateApps);
|
2022-03-31 20:36:59 +01:00
|
|
|
|
checks = forAllSystems (system: pkgs:
|
|
|
|
|
let
|
|
|
|
|
vmTests = import ./tests {
|
|
|
|
|
makeTest = (import (nixpkgs + "/nixos/lib/testing-python.nix") { inherit system; }).makeTest;
|
2022-05-23 18:57:50 +01:00
|
|
|
|
inherit inputs; pkgs = nixpkgsFor.${system};
|
2022-03-31 20:36:59 +01:00
|
|
|
|
};
|
|
|
|
|
in
|
|
|
|
|
pkgs.lib.optionalAttrs pkgs.stdenv.isLinux vmTests # vmTests can only be ran on Linux, so append them only if on Linux.
|
|
|
|
|
//
|
|
|
|
|
{
|
|
|
|
|
# Other checks here...
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-01-03 10:50:50 +00:00
|
|
|
|
};
|
2022-02-15 18:19:48 -08:00
|
|
|
|
}
|