nixinate/flake.nix

163 lines
6.6 KiB
Nix
Raw Normal View History

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-unstable";
2022-01-26 20:17:20 +00:00
};
2023-12-11 20:37:06 +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" ];
forSystems = systems: f:
nixpkgs.lib.genAttrs systems
2023-12-11 20:37:06 +01:00
(system: f system nixpkgs.legacyPackages.${system});
forAllSystems = forSystems supportedSystems;
2023-12-11 20:37:06 +01:00
nixpkgsFor = forAllSystems (system: pkgs:
import nixpkgs {
inherit system;
overlays = [ self.overlay ];
});
in
rec
2022-02-15 18:19:48 -08:00
{
herculesCI.ciSystems = [ "x86_64-linux" ];
2022-01-03 10:50:50 +00:00
overlay = final: prev: {
nixinate = {
2023-12-11 20:37:06 +01:00
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;
2023-12-11 20:37:06 +01:00
validMachines = final.lib.remove "" (final.lib.forEach machines (x: final.lib.optionalString (flake.nixosConfigurations."${x}"._module.args ? nixinate) "${x}"));
mkDeployScript =
{ machine
, dryRun
,
}:
let
inherit (builtins) abort;
inherit (final.lib) getExe optionalString concatStringsSep;
nix = "${getExe final.nix}";
nixos-rebuild = "${getExe final.nixos-rebuild}";
openssh = "${getExe final.openssh}";
bash = "${getExe final.bash}";
flock = "${getExe final.flock}";
2023-12-11 20:37:06 +01:00
n = flake.nixosConfigurations.${machine}._module.args.nixinate;
hermetic = n.hermetic or true;
user = n.sshUser or "root";
host = n.host or "";
sshConfigHost = n.sshConfigHost or "";
userHost =
if sshConfigHost != ""
then sshConfigHost
else if host != ""
then "${user}@${host}"
else abort "_module.args.nixinate.host or _module.args.nixinate.sshConfigHost must be set";
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'";
substituteOnTarget = n.substituteOnTarget or false;
switch =
if dryRun
then "dry-activate"
else "switch";
nixOptions = concatStringsSep " " (n.nixOptions or [ ]);
2023-12-11 20:37:06 +01:00
script =
''
#!${bash}
set -e
echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}"
''
+ (
if sshConfigHost != ""
then ''
echo "🌐 SSH Config Host: ${sshConfigHost}"
''
else ''
echo "👤 SSH User: ${user}"
echo "🌐 SSH Host: ${host}"
''
)
+ (
if remote
then
''
echo "🚀 Sending flake to ${machine} via nix copy:"
( set -x; ${nix} ${nixOptions} copy ${flake} --to ssh://${userHost} )
''
+ (
if hermetic
then ''
echo "🤞 Activating configuration hermetically on ${machine} via ssh:"
( set -x; ${nix} ${nixOptions} copy --derivation ${nixos-rebuild} ${flock} --to ssh://${userHost} )
( set -x; ${openssh} -t ${userHost} "sudo nix-store --realise ${nixos-rebuild} ${flock} && sudo ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine}" )
''
else ''
echo "🤞 Activating configuration non-hermetically on ${machine} via ssh:"
( set -x; ${openssh} -t ${userHost} "sudo flock -w 60 /dev/shm/nixinate-${machine} nixos-rebuild ${switch} --flake ${flake}#${machine}" )
''
)
else ''
echo "🔨 Building system closure locally, copying it to remote store and activating it:"
( set -x; NIX_SSHOPTS="-t" ${flock} -w 60 /dev/shm/nixinate-${machine} ${nixos-rebuild} ${nixOptions} ${switch} --flake ${flake}#${machine} --target-host ${userHost} --use-remote-sudo ${optionalString substituteOnTarget "-s"} )
2023-12-11 20:37:06 +01:00
''
);
in
final.writeScript "deploy-${machine}.sh" script;
2022-01-03 10:50:50 +00:00
in
(
nixpkgs.lib.genAttrs
(map (a: "nixinate-" + a) validMachines)
(
x: {
type = "app";
program = toString (mkDeployScript {
machine = nixpkgs.lib.removePrefix "nixinate-" x;
dryRun = false;
});
}
)
// nixpkgs.lib.genAttrs
(map (a: "nixinate-" + a + "-dry-run") validMachines)
(
x: {
type = "app";
program = toString (mkDeployScript {
machine = nixpkgs.lib.removePrefix "nixinate-" nixpkgs.lib.removeSuffix "-dry-run" x;
dryRun = true;
});
}
)
);
2023-12-11 20:37:06 +01:00
};
nixinate = forAllSystems (system: pkgs: nixpkgsFor.${system}.generateApps);
2023-12-11 20:37:06 +01:00
checks = forAllSystems (
system: pkgs:
let
vmTests = import ./tests {
makeTest = (import (nixpkgs + "/nixos/lib/testing-python.nix") { inherit system; }).makeTest;
inherit inputs;
pkgs = nixpkgsFor.${system};
};
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
}