Run nixpkgs-fmt

This commit is contained in:
Tristan D. 2023-12-11 20:37:06 +01:00
parent 46696bdb78
commit 54df2c1238
Signed by: tristan
SSH key fingerprint: SHA256:U7y6eMb7CQDaTHv9XoX6/BaQnPqyxxKc+Xnfcefi6rY
6 changed files with 227 additions and 152 deletions

View file

@ -6,7 +6,12 @@
nixinate.url = "github:matthewcroughan/nixinate"; nixinate.url = "github:matthewcroughan/nixinate";
}; };
outputs = { self, nixpkgs, nixinate }: { outputs =
{ 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,7 +3,11 @@
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
}; };
outputs = { self, nixpkgs, ... }@inputs: outputs =
{ 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" ];
@ -11,13 +15,19 @@
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: import nixpkgs { inherit system; overlays = [ self.overlay ]; }); nixpkgsFor = forAllSystems (system: pkgs:
in rec import nixpkgs {
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 = prev.pkgs.writeShellScriptBin "nix" 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; };
}; };
@ -25,7 +35,12 @@
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 = { machine, dryRun }: let mkDeployScript =
{ 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}";
@ -39,11 +54,24 @@
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 = if sshConfigHost != "" then sshConfigHost else if host != "" then "${user}@${host}" else abort "_module.args.nixinate.host or _module.args.nixinate.sshConfigHost must be set"; 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"; 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'"; 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 = if dryRun then "dry-activate" else "switch"; switch =
if dryRun
then "dry-activate"
else "switch";
nixOptions = concatStringsSep " " (n.nixOptions or [ ]); nixOptions = concatStringsSep " " (n.nixOptions or [ ]);
script = script =
@ -51,36 +79,51 @@
#!${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;
@ -90,8 +133,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;
@ -103,16 +146,17 @@
}; };
}; };
nixinate = forAllSystems (system: pkgs: nixpkgsFor.${system}.generateApps); nixinate = forAllSystems (system: pkgs: nixpkgsFor.${system}.generateApps);
checks = forAllSystems (system: pkgs: checks = forAllSystems (
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; pkgs = nixpkgsFor.${system}; inherit inputs;
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,5 +1,8 @@
{ pkgs, makeTest, inputs }: { pkgs
{ , 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,10 +1,11 @@
# 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 empty_registry = builtins.toFile "empty-flake-registry.json" ''{"flakes":[],"version":2}''; in let
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,10 +1,15 @@
{ pkgs, makeTest, inputs }: { pkgs
, 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 name = "allDrvOutputs-${pkg.pname or pkg.name or "unknown"}"; let
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
@ -18,12 +23,18 @@ 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: let callLocklessFlake = path: inputs:
r = {outPath = path;} // let
((import (path + "/flake.nix")).outputs (inputs // {self = r;})); r =
{ outPath = path; }
// ((import (path + "/flake.nix")).outputs (inputs // { self = r; }));
in in
r; r;
mkNixinateTest = { buildOn, hermetic ? false, ... }: mkNixinateTest =
{ buildOn
, hermetic ? false
, ...
}:
let let
exampleFlake = pkgs.writeTextFile { exampleFlake = pkgs.writeTextFile {
name = "nixinate-example-flake"; name = "nixinate-example-flake";
@ -68,7 +79,8 @@ 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);
@ -79,7 +91,8 @@ 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
@ -87,8 +100,7 @@ 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")
@ -109,8 +121,14 @@ let
}; };
in in
{ {
local = (mkNixinateTest { buildOn = "local"; }); local = mkNixinateTest { buildOn = "local"; };
remote = (mkNixinateTest { buildOn = "remote"; }); remote = mkNixinateTest { buildOn = "remote"; };
localHermetic = (mkNixinateTest { buildOn = "local"; hermetic = true; }); localHermetic = mkNixinateTest {
remoteHermetic = (mkNixinateTest { buildOn = "remote"; hermetic = true; }); buildOn = "local";
hermetic = true;
};
remoteHermetic = mkNixinateTest {
buildOn = "remote";
hermetic = true;
};
} }

View file

@ -3,13 +3,17 @@
{ {
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 = {