Compare commits

..

No commits in common. "a53e5eb6767562fe651e3a0c387eb95b8970c8bd" and "46696bdb78bf1c425dab83bbb61a73122e7dc3aa" have entirely different histories.

7 changed files with 153 additions and 228 deletions

View file

@ -31,7 +31,7 @@ Below is a minimal example:
{ {
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05"; nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
nixinate.url = "git+https://git.vlt81.de/tristan/nixinate.git"; nixinate.url = "github:matthewcroughan/nixinate";
}; };
outputs = { self, nixpkgs, nixinate }: { outputs = { self, nixpkgs, nixinate }: {

View file

@ -6,12 +6,7 @@
nixinate.url = "github:matthewcroughan/nixinate"; nixinate.url = "github:matthewcroughan/nixinate";
}; };
outputs = outputs = { self, nixpkgs, nixinate }: {
{ self
, nixpkgs
, nixinate
,
}: {
apps = nixinate.nixinate.x86_64-linux self; apps = nixinate.nixinate.x86_64-linux self;
nixosConfigurations = { nixosConfigurations = {
myMachine = nixpkgs.lib.nixosSystem { myMachine = nixpkgs.lib.nixosSystem {

View file

@ -3,11 +3,7 @@
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
}; };
outputs = outputs = { self, nixpkgs, ... }@inputs:
{ self
, nixpkgs
, ...
} @ inputs:
let let
version = builtins.substring 0 8 self.lastModifiedDate; version = builtins.substring 0 8 self.lastModifiedDate;
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
@ -15,19 +11,13 @@
nixpkgs.lib.genAttrs systems nixpkgs.lib.genAttrs systems
(system: f system nixpkgs.legacyPackages.${system}); (system: f system nixpkgs.legacyPackages.${system});
forAllSystems = forSystems supportedSystems; forAllSystems = forSystems supportedSystems;
nixpkgsFor = forAllSystems (system: pkgs: nixpkgsFor = forAllSystems (system: pkgs: import nixpkgs { inherit system; overlays = [ self.overlay ]; });
import nixpkgs { in rec
inherit system;
overlays = [ self.overlay ];
});
in
rec
{ {
herculesCI.ciSystems = [ "x86_64-linux" ]; herculesCI.ciSystems = [ "x86_64-linux" ];
overlay = final: prev: { overlay = final: prev: {
nixinate = { nixinate = {
nix = nix = prev.pkgs.writeShellScriptBin "nix"
prev.pkgs.writeShellScriptBin "nix"
''${final.nixVersions.unstable}/bin/nix --experimental-features "nix-command flakes" "$@"''; ''${final.nixVersions.unstable}/bin/nix --experimental-features "nix-command flakes" "$@"'';
nixos-rebuild = prev.nixos-rebuild.override { inherit (final) nix; }; nixos-rebuild = prev.nixos-rebuild.override { inherit (final) nix; };
}; };
@ -35,12 +25,7 @@
let let
machines = builtins.attrNames flake.nixosConfigurations; machines = builtins.attrNames flake.nixosConfigurations;
validMachines = final.lib.remove "" (final.lib.forEach machines (x: final.lib.optionalString (flake.nixosConfigurations."${x}"._module.args ? nixinate) "${x}" )); validMachines = final.lib.remove "" (final.lib.forEach machines (x: final.lib.optionalString (flake.nixosConfigurations."${x}"._module.args ? nixinate) "${x}" ));
mkDeployScript = mkDeployScript = { machine, dryRun }: let
{ machine
, dryRun
,
}:
let
inherit (builtins) abort; inherit (builtins) abort;
inherit (final.lib) getExe optionalString concatStringsSep; inherit (final.lib) getExe optionalString concatStringsSep;
nix = "${getExe final.nix}"; nix = "${getExe final.nix}";
@ -54,24 +39,11 @@
user = n.sshUser or "root"; user = n.sshUser or "root";
host = n.host or ""; host = n.host or "";
sshConfigHost = n.sshConfigHost or ""; sshConfigHost = n.sshConfigHost or "";
userHost = 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";
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"; where = n.buildOn or "remote";
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'";
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; substituteOnTarget = n.substituteOnTarget or false;
switch = switch = if dryRun then "dry-activate" else "switch";
if dryRun
then "dry-activate"
else "switch";
nixOptions = concatStringsSep " " (n.nixOptions or []); nixOptions = concatStringsSep " " (n.nixOptions or []);
script = script =
@ -79,51 +51,36 @@
#!${bash} #!${bash}
set -e set -e
echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}" echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}"
'' '' + (if sshConfigHost != "" then ''
+ (
if sshConfigHost != ""
then ''
echo "🌐 SSH Config Host: ${sshConfigHost}" echo "🌐 SSH Config Host: ${sshConfigHost}"
'' '' else ''
else ''
echo "👤 SSH User: ${user}" echo "👤 SSH User: ${user}"
echo "🌐 SSH Host: ${host}" echo "🌐 SSH Host: ${host}"
'' '') + (if remote then ''
)
+ (
if remote
then
''
echo "🚀 Sending flake to ${machine} via nix copy:" echo "🚀 Sending flake to ${machine} via nix copy:"
( set -x; ${nix} ${nixOptions} copy ${flake} --to ssh://${userHost} ) ( set -x; ${nix} ${nixOptions} copy ${flake} --to ssh://${userHost} )
'' '' + (if hermetic then ''
+ (
if hermetic
then ''
echo "🤞 Activating configuration hermetically on ${machine} via ssh:" echo "🤞 Activating configuration hermetically on ${machine} via ssh:"
( set -x; ${nix} ${nixOptions} copy --derivation ${nixos-rebuild} ${flock} --to ssh://${userHost} ) ( 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}" ) ( 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 ''
else ''
echo "🤞 Activating configuration non-hermetically on ${machine} via ssh:" 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}" ) ( set -x; ${openssh} -t ${userHost} "sudo flock -w 60 /dev/shm/nixinate-${machine} nixos-rebuild ${switch} --flake ${flake}#${machine}" )
'' '')
)
else '' else ''
echo "🔨 Building system closure locally, copying it to remote store and activating it:" 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"} ) ( 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"} )
'' '');
); in final.writeScript "deploy-${machine}.sh" script;
in
final.writeScript "deploy-${machine}.sh" script;
in in
{ {
nixinate = ( nixinate =
(
nixpkgs.lib.genAttrs nixpkgs.lib.genAttrs
validMachines validMachines
( (x:
x: { {
type = "app"; type = "app";
program = toString (mkDeployScript { program = toString (mkDeployScript {
machine = x; machine = x;
@ -133,8 +90,8 @@
) )
// nixpkgs.lib.genAttrs // nixpkgs.lib.genAttrs
(map (a: a + "-dry-run") validMachines) (map (a: a + "-dry-run") validMachines)
( (x:
x: { {
type = "app"; type = "app";
program = toString (mkDeployScript { program = toString (mkDeployScript {
machine = nixpkgs.lib.removeSuffix "-dry-run" x; machine = nixpkgs.lib.removeSuffix "-dry-run" x;
@ -146,17 +103,16 @@
}; };
}; };
nixinate = forAllSystems (system: pkgs: nixpkgsFor.${system}.generateApps); nixinate = forAllSystems (system: pkgs: nixpkgsFor.${system}.generateApps);
checks = forAllSystems ( checks = forAllSystems (system: pkgs:
system: pkgs:
let let
vmTests = import ./tests { vmTests = import ./tests {
makeTest = (import (nixpkgs + "/nixos/lib/testing-python.nix") { inherit system; }).makeTest; makeTest = (import (nixpkgs + "/nixos/lib/testing-python.nix") { inherit system; }).makeTest;
inherit inputs; inherit inputs; pkgs = nixpkgsFor.${system};
pkgs = nixpkgsFor.${system};
}; };
in in
pkgs.lib.optionalAttrs pkgs.stdenv.isLinux vmTests # vmTests can only be ran on Linux, so append them only if on Linux. pkgs.lib.optionalAttrs pkgs.stdenv.isLinux vmTests # vmTests can only be ran on Linux, so append them only if on Linux.
// { //
{
# Other checks here... # Other checks here...
} }
); );

View file

@ -1,8 +1,5 @@
{ pkgs { pkgs, makeTest, inputs }:
, makeTest {
, inputs
,
}: {
vmTestLocal = (import ./vmTest { inherit pkgs makeTest inputs; }).local; vmTestLocal = (import ./vmTest { inherit pkgs makeTest inputs; }).local;
vmTestRemote = (import ./vmTest { inherit pkgs makeTest inputs; }).remote; vmTestRemote = (import ./vmTest { inherit pkgs makeTest inputs; }).remote;
vmTestLocalHermetic = (import ./vmTest { inherit pkgs makeTest inputs; }).localHermetic; vmTestLocalHermetic = (import ./vmTest { inherit pkgs makeTest inputs; }).localHermetic;

View file

@ -1,11 +1,10 @@
# Configuration that will be added to both the nixinatee node and the nixinator # Configuration that will be added to both the nixinatee node and the nixinator
# node. # node.
{ inputs }: { { inputs }:
{
nix = { nix = {
extraOptions = extraOptions =
let let empty_registry = builtins.toFile "empty-flake-registry.json" ''{"flakes":[],"version":2}''; in
empty_registry = builtins.toFile "empty-flake-registry.json" ''{"flakes":[],"version":2}'';
in
'' ''
experimental-features = nix-command flakes experimental-features = nix-command flakes
flake-registry = ${empty_registry} flake-registry = ${empty_registry}

View file

@ -1,15 +1,10 @@
{ pkgs { pkgs, makeTest, inputs }:
, makeTest
, inputs
,
}:
let let
inherit (pkgs) lib; inherit (pkgs) lib;
# Return a store path with a closure containing everything including # Return a store path with a closure containing everything including
# derivations and all build dependency outputs, all the way down. # derivations and all build dependency outputs, all the way down.
allDrvOutputs = pkg: allDrvOutputs = pkg:
let let name = "allDrvOutputs-${pkg.pname or pkg.name or "unknown"}";
name = "allDrvOutputs-${pkg.pname or pkg.name or "unknown"}";
in in
pkgs.runCommand name { refs = pkgs.writeReferencesToFile pkg.drvPath; } '' pkgs.runCommand name { refs = pkgs.writeReferencesToFile pkg.drvPath; } ''
touch $out touch $out
@ -23,18 +18,12 @@ let
''; '';
# Imports a flake with inputs passed in by hand, rather than # Imports a flake with inputs passed in by hand, rather than
# builtins.getFlake, which cannot be used in this way. # builtins.getFlake, which cannot be used in this way.
callLocklessFlake = path: inputs: callLocklessFlake = path: inputs: let
let r = {outPath = path;} //
r = ((import (path + "/flake.nix")).outputs (inputs // {self = r;}));
{ outPath = path; }
// ((import (path + "/flake.nix")).outputs (inputs // { self = r; }));
in in
r; r;
mkNixinateTest = mkNixinateTest = { buildOn, hermetic ? false, ... }:
{ buildOn
, hermetic ? false
, ...
}:
let let
exampleFlake = pkgs.writeTextFile { exampleFlake = pkgs.writeTextFile {
name = "nixinate-example-flake"; name = "nixinate-example-flake";
@ -79,8 +68,7 @@ let
]; ];
virtualisation = { virtualisation = {
writableStore = true; writableStore = true;
additionalPaths = additionalPaths = []
[ ]
++ lib.optional (buildOn == "remote") (allDrvOutputs exampleSystem) ++ lib.optional (buildOn == "remote") (allDrvOutputs exampleSystem)
++ lib.optional (hermetic == true) (pkgs.nixinate.nixos-rebuild.drvPath) ++ lib.optional (hermetic == true) (pkgs.nixinate.nixos-rebuild.drvPath)
++ lib.optional (hermetic == true) (pkgs.flock.drvPath); ++ lib.optional (hermetic == true) (pkgs.flock.drvPath);
@ -91,8 +79,7 @@ let
(import ./common.nix { inherit inputs; }) (import ./common.nix { inherit inputs; })
]; ];
virtualisation = { virtualisation = {
additionalPaths = additionalPaths = [
[
(allDrvOutputs exampleSystem) (allDrvOutputs exampleSystem)
] ]
++ lib.optional (buildOn == "remote") exampleFlake ++ lib.optional (buildOn == "remote") exampleFlake
@ -100,7 +87,8 @@ let
}; };
}; };
}; };
testScript = '' testScript =
''
start_all() start_all()
nixinatee.wait_for_unit("sshd.service") nixinatee.wait_for_unit("sshd.service")
nixinator.wait_for_unit("multi-user.target") nixinator.wait_for_unit("multi-user.target")
@ -121,14 +109,8 @@ let
}; };
in in
{ {
local = mkNixinateTest { buildOn = "local"; }; local = (mkNixinateTest { buildOn = "local"; });
remote = mkNixinateTest { buildOn = "remote"; }; remote = (mkNixinateTest { buildOn = "remote"; });
localHermetic = mkNixinateTest { localHermetic = (mkNixinateTest { buildOn = "local"; hermetic = true; });
buildOn = "local"; remoteHermetic = (mkNixinateTest { buildOn = "remote"; hermetic = true; });
hermetic = true;
};
remoteHermetic = mkNixinateTest {
buildOn = "remote";
hermetic = true;
};
} }

View file

@ -3,17 +3,13 @@
{ {
config = { config = {
nix.trustedUsers = [ "nixinator" ]; nix.trustedUsers = [ "nixinator" ];
security.sudo.extraRules = [ security.sudo.extraRules = [{
{
users = [ "nixinator" ]; users = [ "nixinator" ];
commands = [ commands = [{
{
command = "ALL"; command = "ALL";
options = [ "NOPASSWD" ]; options = [ "NOPASSWD" ];
} }];
]; }];
}
];
users = { users = {
mutableUsers = false; mutableUsers = false;
users = { users = {