refactor(dotfiles): order let bindings #98

Merged
alexion merged 1 commits from natu-let-order-dotfiles into main 2026-09-26 21:05:12 -04:00
3 changed files with 115 additions and 116 deletions
Showing only changes of commit 8000367613 - Show all commits

View File

@@ -5,19 +5,6 @@
root, root,
}: }:
let let
util = import ./util.nix { inherit lib; };
registry = import ./registry.nix {
inherit lib;
hosts = self.nixosConfigurations;
root = root + "/registry";
};
modules = import ./modules.nix {
inherit util;
root = root + "/modules";
};
specialArgs = { specialArgs = {
inherit inherit
inputs inputs
@@ -27,6 +14,14 @@ let
registry = registry.collected; registry = registry.collected;
}; };
util = import ./util.nix { inherit lib; };
registry = import ./registry.nix {
inherit lib;
hosts = self.nixosConfigurations;
root = root + "/registry";
};
guestModules = import ./guests.nix { guestModules = import ./guests.nix {
inherit inherit
inputs inputs
@@ -38,6 +33,11 @@ let
repositoryModules = modules; repositoryModules = modules;
root = root + "/guests"; root = root + "/guests";
}; };
modules = import ./modules.nix {
inherit util;
root = root + "/modules";
};
in in
{ {
nixosConfigurations = import ./hosts.nix { nixosConfigurations = import ./hosts.nix {

View File

@@ -4,21 +4,39 @@
root, root,
}: }:
let let
checkType = lib.types.submodule { definitions =
options = { lib.mapAttrs'
assertion = lib.mkOption { (
type = lib.types.functionTo lib.types.bool; name: _type:
description = '' let
Predicate run against this registry's flattened collected entries. path = root + "/${name}";
''; in
}; lib.nameValuePair (registryName name) (mkRegistry path)
)
(
lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (builtins.readDir root)
);
message = lib.mkOption { registryName = fileName: lib.removeSuffix ".nix" fileName;
type = lib.types.str;
description = '' mkRegistry =
Error message shown when this registry validation check fails. path:
''; let
evaluated = lib.evalModules {
specialArgs.lib = registryLib;
modules = [
registryDefinitionModule
path
];
}; };
in
{
inherit (evaluated.config) type replicate checks;
};
registryLib = lib // {
registry = (lib.registry or { }) // {
inherit predicates;
}; };
}; };
@@ -27,12 +45,6 @@ let
value: entries: builtins.length (lib.unique (map value entries)) == builtins.length entries; value: entries: builtins.length (lib.unique (map value entries)) == builtins.length entries;
}; };
registryLib = lib // {
registry = (lib.registry or { }) // {
inherit predicates;
};
};
registryDefinitionModule = { registryDefinitionModule = {
options = { options = {
type = lib.mkOption { type = lib.mkOption {
@@ -60,7 +72,23 @@ let
}; };
}; };
registryName = fileName: lib.removeSuffix ".nix" fileName; checkType = lib.types.submodule {
options = {
assertion = lib.mkOption {
type = lib.types.functionTo lib.types.bool;
description = ''
Predicate run against this registry's flattened collected entries.
'';
};
message = lib.mkOption {
type = lib.types.str;
description = ''
Error message shown when this registry validation check fails.
'';
};
};
};
registryOption = registryOption =
registry: registry:
@@ -71,50 +99,40 @@ let
Lazily collected fleet-visible registry entries, nested by host and entry name. Lazily collected fleet-visible registry entries, nested by host and entry name.
''; '';
}; };
withOperations =
mkRegistry = registry:
path: registry
let // {
evaluated = lib.evalModules { join = host: entry: join registry host entry;
specialArgs.lib = registryLib;
modules = [
registryDefinitionModule
path
];
};
in
{
inherit (evaluated.config) type replicate checks;
}; };
join =
definitions = registry: host: entry:
lib.mapAttrs' lib.filterAttrs (_name: value: value != null) (
( lib.mapAttrs (
name: _type: registryName: _registry: (registry.${registryName}.${host} or { }).${entry} or null
let ) definitions
path = root + "/${name}";
in
lib.nameValuePair (registryName name) (mkRegistry path)
)
(
lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (builtins.readDir root)
);
flattenRegistryEntries =
registryName: data:
lib.concatLists (
lib.mapAttrsToList (
host: entries:
lib.mapAttrsToList (entry: value: {
path = "registry.${registryName}.${host}.${entry}";
inherit host entry value;
}) entries
) data
); );
rawDeclarations = collectData =
registryName: hosts: { hosts }:
lib.mapAttrs (_hostName: host: host.config.registry.${registryName} or { }) hosts; let
data = lib.mapAttrs (
registryName: registry:
if registry.replicate or false then
replicatedData registryName hosts raw.${registryName}
else
lib.mapAttrs (hostName: host: host.config.registry.${registryName}.${hostName} or { }) hosts
) definitions;
raw = lib.mapAttrs (registryName: _registry: rawDeclarations registryName hosts) definitions;
in
assertValid data;
replicatedData =
registryName: hosts: raw:
let
entries = lib.mapAttrs (_entryName: entry: entry.value) (replicatedEntries registryName raw);
in
lib.mapAttrs (_hostName: _host: entries) hosts;
replicatedEntries = replicatedEntries =
registryName: raw: registryName: raw:
@@ -148,12 +166,20 @@ let
} }
) { } entries; ) { } entries;
replicatedData = rawDeclarations =
registryName: hosts: raw: registryName: hosts:
lib.mapAttrs (_hostName: host: host.config.registry.${registryName} or { }) hosts;
assertValid =
data:
let let
entries = lib.mapAttrs (_entryName: entry: entry.value) (replicatedEntries registryName raw); assertions = validationAssertions data;
failures = lib.filter (assertion: !assertion.assertion) assertions;
in in
lib.mapAttrs (_hostName: _host: entries) hosts; assert lib.assertMsg (lib.all (assertion: assertion.assertion) assertions) (
lib.concatMapStringsSep "\n" (assertion: assertion.message) failures
);
data;
validationAssertions = validationAssertions =
data: data:
@@ -170,45 +196,18 @@ let
) definitions ) definitions
); );
assertValid = flattenRegistryEntries =
data: registryName: data:
let lib.concatLists (
assertions = validationAssertions data; lib.mapAttrsToList (
failures = lib.filter (assertion: !assertion.assertion) assertions; host: entries:
in lib.mapAttrsToList (entry: value: {
assert lib.assertMsg (lib.all (assertion: assertion.assertion) assertions) ( path = "registry.${registryName}.${host}.${entry}";
lib.concatMapStringsSep "\n" (assertion: assertion.message) failures inherit host entry value;
); }) entries
data; ) data
join =
registry: host: entry:
lib.filterAttrs (_name: value: value != null) (
lib.mapAttrs (
registryName: _registry: (registry.${registryName}.${host} or { }).${entry} or null
) definitions
); );
collectData =
{ hosts }:
let
raw = lib.mapAttrs (registryName: _registry: rawDeclarations registryName hosts) definitions;
data = lib.mapAttrs (
registryName: registry:
if registry.replicate or false then
replicatedData registryName hosts raw.${registryName}
else
lib.mapAttrs (hostName: host: host.config.registry.${registryName}.${hostName} or { }) hosts
) definitions;
in
assertValid data;
withOperations =
registry:
registry
// {
join = host: entry: join registry host entry;
};
in in
{ {
types = lib.mapAttrs (_name: definition: definition.type) definitions; types = lib.mapAttrs (_name: definition: definition.type) definitions;

View File

@@ -25,8 +25,8 @@ in
deriveMac = deriveMac =
name: name:
let let
hash = builtins.hashString "sha256" name;
octet = i: builtins.substring (i * 2) 2 hash; octet = i: builtins.substring (i * 2) 2 hash;
hash = builtins.hashString "sha256" name;
in in
lib.concatStringsSep ":" ( lib.concatStringsSep ":" (
[ "02" ] [ "02" ]