refactor(dotfiles): extract registry schema #110
@@ -1,10 +1,10 @@
|
|||||||
{ lib, ... }:
|
{ lib, util, ... }:
|
||||||
{
|
{
|
||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
|
|
||||||
checks = [
|
checks = [
|
||||||
{
|
{
|
||||||
assertion = lib.registry.predicates.uniqueValue (entry: entry.value);
|
assertion = util.registry.predicates.uniqueValue (entry: entry.value);
|
||||||
message = "Address registry values must be unique.";
|
message = "Address registry values must be unique.";
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
{ lib, ... }:
|
{ lib, util, ... }:
|
||||||
{
|
{
|
||||||
checks = [
|
checks = [
|
||||||
{
|
{
|
||||||
assertion = lib.registry.predicates.uniqueValue (entry: entry.value.domain);
|
assertion = util.registry.predicates.uniqueValue (entry: entry.value.domain);
|
||||||
message = "Route domains must be unique.";
|
message = "Route domains must be unique.";
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
assertion =
|
assertion =
|
||||||
entries:
|
entries:
|
||||||
lib.registry.predicates.uniqueValue (entry: entry.value) (
|
util.registry.predicates.uniqueValue (entry: entry.value) (
|
||||||
lib.concatMap (entry: map (port: entry // { value = port; }) entry.value.udp.ports) entries
|
lib.concatMap (entry: map (port: entry // { value = port; }) entry.value.udp.ports) entries
|
||||||
);
|
);
|
||||||
message = "Route UDP ports must be unique.";
|
message = "Route UDP ports must be unique.";
|
||||||
|
|||||||
@@ -128,6 +128,11 @@ Review Nix files in entrypoint order:
|
|||||||
- [x] `src/dotfiles/default.nix`
|
- [x] `src/dotfiles/default.nix`
|
||||||
- [x] `src/dotfiles/util.nix`
|
- [x] `src/dotfiles/util.nix`
|
||||||
- [ ] `src/dotfiles/registry.nix`
|
- [ ] `src/dotfiles/registry.nix`
|
||||||
|
- [ ] `src/dotfiles/registry/options.nix`
|
||||||
|
- [ ] `src/dotfiles/registry/definitions.nix`
|
||||||
|
- [ ] `src/dotfiles/registry/schema.nix`
|
||||||
|
- [ ] `src/dotfiles/registry/collection.nix`
|
||||||
|
- [ ] `src/dotfiles/registry/validation.nix`
|
||||||
- [ ] `configs/nixos/registry/address.nix`
|
- [ ] `configs/nixos/registry/address.nix`
|
||||||
- [ ] `configs/nixos/registry/env-var.nix`
|
- [ ] `configs/nixos/registry/env-var.nix`
|
||||||
- [ ] `configs/nixos/registry/homepage.nix`
|
- [ ] `configs/nixos/registry/homepage.nix`
|
||||||
@@ -148,6 +153,7 @@ Review Nix files in entrypoint order:
|
|||||||
- [ ] `configs/nixos/modules/desktop/apps/firefox.nix`
|
- [ ] `configs/nixos/modules/desktop/apps/firefox.nix`
|
||||||
- [ ] `configs/nixos/modules/desktop/apps/ghostty.nix`
|
- [ ] `configs/nixos/modules/desktop/apps/ghostty.nix`
|
||||||
- [ ] `configs/nixos/modules/desktop/apps/obsidian.nix`
|
- [ ] `configs/nixos/modules/desktop/apps/obsidian.nix`
|
||||||
|
- [ ] `configs/nixos/modules/desktop/apps/r2modman.nix`
|
||||||
- [ ] `configs/nixos/modules/desktop/apps/signal.nix`
|
- [ ] `configs/nixos/modules/desktop/apps/signal.nix`
|
||||||
- [ ] `configs/nixos/modules/desktop/apps/steam.nix`
|
- [ ] `configs/nixos/modules/desktop/apps/steam.nix`
|
||||||
- [ ] `configs/nixos/modules/desktop/apps/vencord.nix`
|
- [ ] `configs/nixos/modules/desktop/apps/vencord.nix`
|
||||||
|
|||||||
@@ -5,208 +5,20 @@
|
|||||||
util,
|
util,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
definitions = lib.genAttrs' (util.collectNixFiles root) (
|
options = import ./registry/options.nix {
|
||||||
path: lib.nameValuePair (util.removeNixSuffix path) (mkRegistry path)
|
inherit definitions lib;
|
||||||
);
|
|
||||||
|
|
||||||
mkRegistry =
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
predicates = {
|
definitions = import ./registry/definitions.nix {
|
||||||
uniqueValue =
|
inherit lib root util;
|
||||||
value: entries: builtins.length (lib.unique (map value entries)) == builtins.length entries;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
registryDefinitionModule = {
|
|
||||||
options = {
|
|
||||||
type = lib.mkOption {
|
|
||||||
type = lib.types.raw;
|
|
||||||
description = ''
|
|
||||||
NixOS option type for one registry entry.
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
|
|
||||||
replicate = lib.mkOption {
|
|
||||||
type = lib.types.bool;
|
|
||||||
default = false;
|
|
||||||
description = ''
|
|
||||||
Whether collected entries are merged and published under every host key.
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
|
|
||||||
checks = lib.mkOption {
|
|
||||||
type = lib.types.listOf checkType;
|
|
||||||
default = [ ];
|
|
||||||
description = ''
|
|
||||||
Validation checks for this registry's flattened collected entries.
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
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 =
|
|
||||||
registry:
|
|
||||||
lib.mkOption {
|
|
||||||
type = lib.types.attrsOf (lib.types.attrsOf registry.type);
|
|
||||||
default = { };
|
|
||||||
description = ''
|
|
||||||
Lazily collected fleet-visible registry entries, nested by host and entry name.
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
withOperations =
|
|
||||||
registry:
|
|
||||||
registry
|
|
||||||
// {
|
|
||||||
join = host: entry: join registry host entry;
|
|
||||||
};
|
|
||||||
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
|
|
||||||
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 =
|
|
||||||
registryName: raw:
|
|
||||||
let
|
|
||||||
entries = lib.concatLists (
|
|
||||||
lib.concatLists (
|
|
||||||
lib.mapAttrsToList (
|
|
||||||
_evalHost: hostDeclarations:
|
|
||||||
lib.mapAttrsToList (
|
|
||||||
hostName: declarations:
|
|
||||||
lib.mapAttrsToList (entryName: value: {
|
|
||||||
path = "registry.${registryName}.${hostName}.${entryName}";
|
|
||||||
inherit entryName value;
|
|
||||||
}) declarations
|
|
||||||
) hostDeclarations
|
|
||||||
) raw
|
|
||||||
)
|
|
||||||
);
|
|
||||||
in
|
|
||||||
lib.foldl' (
|
|
||||||
acc: entry:
|
|
||||||
if lib.hasAttr entry.entryName acc then
|
|
||||||
if acc.${entry.entryName}.value == entry.value then
|
|
||||||
acc
|
|
||||||
else
|
|
||||||
throw "conflicting replicated registry entry ${entry.path} does not match ${acc.${entry.entryName}.path}"
|
|
||||||
else
|
|
||||||
acc
|
|
||||||
// {
|
|
||||||
${entry.entryName} = entry;
|
|
||||||
}
|
|
||||||
) { } entries;
|
|
||||||
|
|
||||||
rawDeclarations =
|
|
||||||
registryName: hosts:
|
|
||||||
lib.mapAttrs (_hostName: host: host.config.registry.${registryName} or { }) hosts;
|
|
||||||
|
|
||||||
assertValid =
|
|
||||||
data:
|
|
||||||
let
|
|
||||||
assertions = validationAssertions data;
|
|
||||||
failures = lib.filter (assertion: !assertion.assertion) assertions;
|
|
||||||
in
|
|
||||||
assert lib.assertMsg (lib.all (assertion: assertion.assertion) assertions) (
|
|
||||||
lib.concatMapStringsSep "\n" (assertion: assertion.message) failures
|
|
||||||
);
|
|
||||||
data;
|
|
||||||
|
|
||||||
validationAssertions =
|
|
||||||
data:
|
|
||||||
lib.concatLists (
|
|
||||||
lib.mapAttrsToList (
|
|
||||||
registryName: registry:
|
|
||||||
let
|
|
||||||
entries = flattenRegistryEntries registryName data.${registryName};
|
|
||||||
in
|
|
||||||
map (check: {
|
|
||||||
assertion = check.assertion entries;
|
|
||||||
inherit (check) message;
|
|
||||||
}) registry.checks
|
|
||||||
) definitions
|
|
||||||
);
|
|
||||||
|
|
||||||
flattenRegistryEntries =
|
|
||||||
registryName: data:
|
|
||||||
lib.concatLists (
|
|
||||||
lib.mapAttrsToList (
|
|
||||||
host: entries:
|
|
||||||
lib.mapAttrsToList (entry: value: {
|
|
||||||
path = "registry.${registryName}.${host}.${entry}";
|
|
||||||
inherit host entry value;
|
|
||||||
}) entries
|
|
||||||
) data
|
|
||||||
);
|
|
||||||
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
types = lib.mapAttrs (_name: definition: definition.type) definitions;
|
inherit (options) types module;
|
||||||
|
|
||||||
module = {
|
collected = import ./registry/collection.nix {
|
||||||
options.registry = lib.mapAttrs (_name: registryOption) definitions;
|
inherit definitions hosts lib;
|
||||||
|
validate = import ./registry/validation.nix { inherit definitions lib; };
|
||||||
};
|
};
|
||||||
|
|
||||||
collected = withOperations (collectData {
|
|
||||||
inherit hosts;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
82
src/dotfiles/registry/collection.nix
Normal file
82
src/dotfiles/registry/collection.nix
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
{
|
||||||
|
definitions,
|
||||||
|
hosts,
|
||||||
|
lib,
|
||||||
|
validate,
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
withOperations =
|
||||||
|
registry:
|
||||||
|
registry
|
||||||
|
// {
|
||||||
|
join = host: entry: join registry host entry;
|
||||||
|
};
|
||||||
|
|
||||||
|
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
|
||||||
|
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
|
||||||
|
validate data;
|
||||||
|
|
||||||
|
replicatedData =
|
||||||
|
registryName: hosts: raw:
|
||||||
|
let
|
||||||
|
entries = lib.mapAttrs (_entryName: entry: entry.value) (replicatedEntries registryName raw);
|
||||||
|
in
|
||||||
|
lib.mapAttrs (_hostName: _host: entries) hosts;
|
||||||
|
|
||||||
|
replicatedEntries =
|
||||||
|
registryName: raw:
|
||||||
|
let
|
||||||
|
entries = lib.concatLists (
|
||||||
|
lib.concatLists (
|
||||||
|
lib.mapAttrsToList (
|
||||||
|
_evalHost: hostDeclarations:
|
||||||
|
lib.mapAttrsToList (
|
||||||
|
hostName: declarations:
|
||||||
|
lib.mapAttrsToList (entryName: value: {
|
||||||
|
path = "registry.${registryName}.${hostName}.${entryName}";
|
||||||
|
inherit entryName value;
|
||||||
|
}) declarations
|
||||||
|
) hostDeclarations
|
||||||
|
) raw
|
||||||
|
)
|
||||||
|
);
|
||||||
|
in
|
||||||
|
lib.foldl' (
|
||||||
|
acc: entry:
|
||||||
|
if lib.hasAttr entry.entryName acc then
|
||||||
|
if acc.${entry.entryName}.value == entry.value then
|
||||||
|
acc
|
||||||
|
else
|
||||||
|
throw "conflicting replicated registry entry ${entry.path} does not match ${acc.${entry.entryName}.path}"
|
||||||
|
else
|
||||||
|
acc
|
||||||
|
// {
|
||||||
|
${entry.entryName} = entry;
|
||||||
|
}
|
||||||
|
) { } entries;
|
||||||
|
|
||||||
|
rawDeclarations =
|
||||||
|
registryName: hosts:
|
||||||
|
lib.mapAttrs (_hostName: host: host.config.registry.${registryName} or { }) hosts;
|
||||||
|
in
|
||||||
|
withOperations (collectData {
|
||||||
|
inherit hosts;
|
||||||
|
})
|
||||||
26
src/dotfiles/registry/definitions.nix
Normal file
26
src/dotfiles/registry/definitions.nix
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
lib,
|
||||||
|
root,
|
||||||
|
util,
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
definitions = lib.genAttrs' (util.collectNixFiles root) (
|
||||||
|
path: lib.nameValuePair (util.removeNixSuffix path) (mkRegistry path)
|
||||||
|
);
|
||||||
|
|
||||||
|
mkRegistry =
|
||||||
|
path:
|
||||||
|
let
|
||||||
|
evaluated = lib.evalModules {
|
||||||
|
specialArgs = { inherit util; };
|
||||||
|
modules = [
|
||||||
|
(import ./schema.nix { inherit lib; })
|
||||||
|
path
|
||||||
|
];
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
inherit (evaluated.config) type replicate checks;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
definitions
|
||||||
22
src/dotfiles/registry/options.nix
Normal file
22
src/dotfiles/registry/options.nix
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
definitions,
|
||||||
|
lib,
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
registryOption =
|
||||||
|
registry:
|
||||||
|
lib.mkOption {
|
||||||
|
type = lib.types.attrsOf (lib.types.attrsOf registry.type);
|
||||||
|
default = { };
|
||||||
|
description = ''
|
||||||
|
Lazily collected fleet-visible registry entries, nested by host and entry name.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
types = lib.mapAttrs (_name: definition: definition.type) definitions;
|
||||||
|
|
||||||
|
module = {
|
||||||
|
options.registry = lib.mapAttrs (_name: registryOption) definitions;
|
||||||
|
};
|
||||||
|
}
|
||||||
46
src/dotfiles/registry/schema.nix
Normal file
46
src/dotfiles/registry/schema.nix
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
{ lib }:
|
||||||
|
let
|
||||||
|
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.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
options = {
|
||||||
|
type = lib.mkOption {
|
||||||
|
type = lib.types.raw;
|
||||||
|
description = ''
|
||||||
|
NixOS option type for one registry entry.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
replicate = lib.mkOption {
|
||||||
|
type = lib.types.bool;
|
||||||
|
default = false;
|
||||||
|
description = ''
|
||||||
|
Whether collected entries are merged and published under every host key.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
checks = lib.mkOption {
|
||||||
|
type = lib.types.listOf checkType;
|
||||||
|
default = [ ];
|
||||||
|
description = ''
|
||||||
|
Validation checks for this registry's flattened collected entries.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
44
src/dotfiles/registry/validation.nix
Normal file
44
src/dotfiles/registry/validation.nix
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
{
|
||||||
|
definitions,
|
||||||
|
lib,
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
assertValid =
|
||||||
|
data:
|
||||||
|
let
|
||||||
|
assertions = validationAssertions data;
|
||||||
|
failures = lib.filter (assertion: !assertion.assertion) assertions;
|
||||||
|
in
|
||||||
|
assert lib.assertMsg (lib.all (assertion: assertion.assertion) assertions) (
|
||||||
|
lib.concatMapStringsSep "\n" (assertion: assertion.message) failures
|
||||||
|
);
|
||||||
|
data;
|
||||||
|
|
||||||
|
validationAssertions =
|
||||||
|
data:
|
||||||
|
lib.concatLists (
|
||||||
|
lib.mapAttrsToList (
|
||||||
|
registryName: registry:
|
||||||
|
let
|
||||||
|
entries = flattenRegistryEntries registryName data.${registryName};
|
||||||
|
in
|
||||||
|
map (check: {
|
||||||
|
assertion = check.assertion entries;
|
||||||
|
inherit (check) message;
|
||||||
|
}) registry.checks
|
||||||
|
) definitions
|
||||||
|
);
|
||||||
|
|
||||||
|
flattenRegistryEntries =
|
||||||
|
registryName: data:
|
||||||
|
lib.concatLists (
|
||||||
|
lib.mapAttrsToList (
|
||||||
|
host: entries:
|
||||||
|
lib.mapAttrsToList (entry: value: {
|
||||||
|
path = "registry.${registryName}.${host}.${entry}";
|
||||||
|
inherit host entry value;
|
||||||
|
}) entries
|
||||||
|
) data
|
||||||
|
);
|
||||||
|
in
|
||||||
|
assertValid
|
||||||
@@ -22,6 +22,9 @@ in
|
|||||||
|
|
||||||
removeNixSuffix = path: lib.removeSuffix ".nix" (builtins.baseNameOf path);
|
removeNixSuffix = path: lib.removeSuffix ".nix" (builtins.baseNameOf path);
|
||||||
|
|
||||||
|
registry.predicates.uniqueValue =
|
||||||
|
value: entries: builtins.length (lib.unique (map value entries)) == builtins.length entries;
|
||||||
|
|
||||||
bridgeName = id: "br-vlan${toString id}";
|
bridgeName = id: "br-vlan${toString id}";
|
||||||
|
|
||||||
deriveMac =
|
deriveMac =
|
||||||
|
|||||||
Reference in New Issue
Block a user