refactor(flake): split assembly code #73

Merged
alexion merged 10 commits from refactor/flake-assembly into main 2026-08-29 17:21:41 -04:00
Showing only changes of commit dc74538cfe - Show all commits

View File

@@ -1,35 +1,16 @@
{ lib, self }: { hosts, lib, root }:
let let
inherit (lib) checkType = lib.types.submodule {
all
concatLists
concatMapStringsSep
filter
filterAttrs
foldl'
hasAttr
hasSuffix
mapAttrs
mapAttrs'
mapAttrsToList
mkOption
nameValuePair
removeSuffix
types
unique
;
checkType = types.submodule {
options = { options = {
assertion = mkOption { assertion = lib.mkOption {
type = types.functionTo types.bool; type = lib.types.functionTo lib.types.bool;
description = '' description = ''
Predicate run against this registry's flattened collected entries. Predicate run against this registry's flattened collected entries.
''; '';
}; };
message = mkOption { message = lib.mkOption {
type = types.str; type = lib.types.str;
description = '' description = ''
Error message shown when this registry validation check fails. Error message shown when this registry validation check fails.
''; '';
@@ -38,7 +19,7 @@ let
}; };
predicates = { predicates = {
uniqueValue = value: entries: builtins.length (unique (map value entries)) == builtins.length entries; uniqueValue = value: entries: builtins.length (lib.unique (map value entries)) == builtins.length entries;
}; };
registryLib = lib // { registryLib = lib // {
@@ -49,23 +30,23 @@ let
registryDefinitionModule = { registryDefinitionModule = {
options = { options = {
type = mkOption { type = lib.mkOption {
type = types.raw; type = lib.types.raw;
description = '' description = ''
NixOS option type for one registry entry. NixOS option type for one registry entry.
''; '';
}; };
replicate = mkOption { replicate = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Whether collected entries are merged and published under every host key. Whether collected entries are merged and published under every host key.
''; '';
}; };
checks = mkOption { checks = lib.mkOption {
type = types.listOf checkType; type = lib.types.listOf checkType;
default = [ ]; default = [ ];
description = '' description = ''
Validation checks for this registry's flattened collected entries. Validation checks for this registry's flattened collected entries.
@@ -74,10 +55,10 @@ let
}; };
}; };
registryName = fileName: removeSuffix ".nix" fileName; registryName = fileName: lib.removeSuffix ".nix" fileName;
registryOption = registry: mkOption { registryOption = registry: lib.mkOption {
type = types.attrsOf (types.attrsOf registry.type); type = lib.types.attrsOf (lib.types.attrsOf registry.type);
default = { }; default = { };
description = '' description = ''
Lazily collected fleet-visible registry entries, nested by host and entry name. Lazily collected fleet-visible registry entries, nested by host and entry name.
@@ -98,19 +79,19 @@ let
inherit (evaluated.config) type replicate checks; inherit (evaluated.config) type replicate checks;
}; };
definitions = mapAttrs' ( definitions = lib.mapAttrs' (
name: type: name: type:
let let
path = self + "/registry/${name}"; path = root + "/${name}";
in in
nameValuePair (registryName name) (mkRegistry path) lib.nameValuePair (registryName name) (mkRegistry path)
) (filterAttrs (name: type: type == "regular" && hasSuffix ".nix" name) (builtins.readDir (self + "/registry"))); ) (lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (builtins.readDir root));
flattenRegistryEntries = registryName: data: flattenRegistryEntries = registryName: data:
concatLists ( lib.concatLists (
mapAttrsToList ( lib.mapAttrsToList (
host: entries: host: entries:
mapAttrsToList (entry: value: { lib.mapAttrsToList (entry: value: {
path = "registry.${registryName}.${host}.${entry}"; path = "registry.${registryName}.${host}.${entry}";
inherit host entry value; inherit host entry value;
}) entries }) entries
@@ -118,17 +99,17 @@ let
); );
rawDeclarations = registryName: hosts: rawDeclarations = registryName: hosts:
mapAttrs (_hostName: host: host.config.registry.${registryName} or { }) hosts; lib.mapAttrs (_hostName: host: host.config.registry.${registryName} or { }) hosts;
replicatedEntries = registryName: raw: replicatedEntries = registryName: raw:
let let
entries = concatLists ( entries = lib.concatLists (
concatLists ( lib.concatLists (
mapAttrsToList ( lib.mapAttrsToList (
_evalHost: hostDeclarations: _evalHost: hostDeclarations:
mapAttrsToList ( lib.mapAttrsToList (
hostName: declarations: hostName: declarations:
mapAttrsToList (entryName: value: { lib.mapAttrsToList (entryName: value: {
path = "registry.${registryName}.${hostName}.${entryName}"; path = "registry.${registryName}.${hostName}.${entryName}";
inherit entryName value; inherit entryName value;
}) declarations }) declarations
@@ -137,9 +118,9 @@ let
) )
); );
in in
foldl' ( lib.foldl' (
acc: entry: acc: entry:
if hasAttr entry.entryName acc then if lib.hasAttr entry.entryName acc then
if acc.${entry.entryName}.value == entry.value then if acc.${entry.entryName}.value == entry.value then
acc acc
else else
@@ -152,13 +133,13 @@ let
replicatedData = registryName: hosts: raw: replicatedData = registryName: hosts: raw:
let let
entries = mapAttrs (_entryName: entry: entry.value) (replicatedEntries registryName raw); entries = lib.mapAttrs (_entryName: entry: entry.value) (replicatedEntries registryName raw);
in in
mapAttrs (_hostName: _host: entries) hosts; lib.mapAttrs (_hostName: _host: entries) hosts;
validationAssertions = data: validationAssertions = data:
concatLists ( lib.concatLists (
mapAttrsToList ( lib.mapAttrsToList (
registryName: registry: registryName: registry:
let let
entries = flattenRegistryEntries registryName data.${registryName}; entries = flattenRegistryEntries registryName data.${registryName};
@@ -173,28 +154,28 @@ let
assertValid = data: assertValid = data:
let let
assertions = validationAssertions data; assertions = validationAssertions data;
failures = filter (assertion: !assertion.assertion) assertions; failures = lib.filter (assertion: !assertion.assertion) assertions;
in in
assert lib.assertMsg (all (assertion: assertion.assertion) assertions) ( assert lib.assertMsg (lib.all (assertion: assertion.assertion) assertions) (
concatMapStringsSep "\n" (assertion: assertion.message) failures lib.concatMapStringsSep "\n" (assertion: assertion.message) failures
); );
data; data;
join = registry: host: entry: join = registry: host: entry:
filterAttrs (_name: value: value != null) ( lib.filterAttrs (_name: value: value != null) (
mapAttrs (registryName: _registry: (registry.${registryName}.${host} or { }).${entry} or null) definitions lib.mapAttrs (registryName: _registry: (registry.${registryName}.${host} or { }).${entry} or null) definitions
); );
collectData = collectData =
{ hosts }: { hosts }:
let let
raw = mapAttrs (registryName: _registry: rawDeclarations registryName hosts) definitions; raw = lib.mapAttrs (registryName: _registry: rawDeclarations registryName hosts) definitions;
data = mapAttrs ( data = lib.mapAttrs (
registryName: registry: registryName: registry:
if registry.replicate or false then if registry.replicate or false then
replicatedData registryName hosts raw.${registryName} replicatedData registryName hosts raw.${registryName}
else else
mapAttrs (hostName: host: host.config.registry.${registryName}.${hostName} or { }) hosts lib.mapAttrs (hostName: host: host.config.registry.${registryName}.${hostName} or { }) hosts
) definitions; ) definitions;
in in
assertValid data; assertValid data;
@@ -206,11 +187,11 @@ let
}; };
in in
{ {
inherit definitions predicates; types = lib.mapAttrs (_name: definition: definition.type) definitions;
module = { module = {
options.registry = mapAttrs (_name: registry: registryOption registry) definitions; options.registry = lib.mapAttrs (_name: registry: registryOption registry) definitions;
}; };
collect = args: withOperations (collectData args); collected = withOperations (collectData { inherit hosts; });
} }