Compare commits

..

8 Commits

6 changed files with 128 additions and 95 deletions

View File

@@ -76,9 +76,13 @@
{ self, nixpkgs, ... }@inputs:
let
inherit (nixpkgs) lib;
flake = import ./flake { inherit lib inputs self; };
in
{
inherit (import ./flake { inherit lib inputs self; }) nixosConfigurations;
# Every host under hosts/ is discovered and built.
nixosConfigurations = flake.mkHosts {
root = self + "/hosts";
};
# A project shell for agent-local resources that should travel with this
# checkout rather than the operator's global profile.

View File

@@ -4,50 +4,55 @@
self,
}:
let
# TODO: refactor the registry code
registryAssembly = import ./registry.nix { inherit lib self; };
util = import ../lib { inherit lib; };
mkModules = import ./module.nix { inherit util; };
registry = import ./registry.nix {
inherit lib;
hosts = self.nixosConfigurations;
root = self + "/registry";
};
modules = import ./module.nix {
inherit util;
modules = mkModules {
root = self + "/modules";
};
guestModules = import ./guest.nix {
inherit
inputs
lib
registry
specialArgs
util
;
repositoryModules = modules;
guestModules = guests.mkGuests {
root = self + "/guests";
};
#----------------------------------------------------------
collectedRegistry = registryAssembly.collect { hosts = self.nixosConfigurations; };
specialArgs = {
inherit
inputs
self
util
;
registry = registry.collected;
registry = collectedRegistry;
};
in
{
nixosConfigurations = import ./host.nix {
guests = import ./guest.nix {
inherit
inputs
lib
specialArgs
registryAssembly
util
;
repositoryModules = modules;
};
hosts = import ./host.nix {
inherit
inputs
lib
specialArgs
guestModules
registryAssembly
;
registryModule = registry.module;
repositoryModules = modules;
root = self + "/hosts";
};
in
{
inherit (hosts) mkHosts;
}

View File

@@ -1,10 +1,9 @@
{
inputs,
lib,
root,
specialArgs,
repositoryModules,
registry,
registryAssembly,
util,
}:
let
@@ -66,7 +65,7 @@ let
enable = lib.mkEnableOption "a reverse-proxy route for this guest";
route = lib.mkOption {
type = registry.types.route;
type = registryAssembly.definitions.route.type;
description = "Route registry entry published for this guest.";
};
};
@@ -248,7 +247,7 @@ let
inputs.stylix.nixosModules.stylix
repositoryModules
registry.module
registryAssembly.module
cfg.interior
{
@@ -269,11 +268,15 @@ let
};
};
in
let
files = util.collectNixFiles root;
guestFiles = lib.filterAttrs (file: type: type == "regular" && lib.hasSuffix ".nix" file) (builtins.readDir root);
names = map (file: lib.removeSuffix ".nix" file) (lib.attrNames guestFiles);
in
{
imports = files ++ map mkGuest names;
mkGuests =
{ root }:
let
files = util.collectNixFiles root;
guestFiles = lib.filterAttrs (file: type: type == "regular" && lib.hasSuffix ".nix" file) (builtins.readDir root);
names = map (file: lib.removeSuffix ".nix" file) (lib.attrNames guestFiles);
in
{
imports = files ++ map mkGuest names;
};
}

View File

@@ -1,12 +1,12 @@
{
inputs,
lib,
root,
specialArgs,
repositoryModules,
guestModules,
registryModule,
# TODO refactor registry
registryAssembly,
}:
let
mkHost =
@@ -26,7 +26,7 @@ let
guestModules
# TODO: refactor
registryModule (root + "/${name}")
registryAssembly.module (root + "/${name}")
{
networking.hostName = name;
@@ -35,11 +35,15 @@ let
];
};
in
let
directories = lib.filterAttrs (_name: type: type == "directory") (builtins.readDir root);
names = lib.attrNames directories;
in
lib.genAttrs names (
name:
mkHost { inherit root name; }
)
{
mkHosts =
{ root }:
let
directories = lib.filterAttrs (_name: type: type == "directory") (builtins.readDir root);
names = lib.attrNames directories;
in
lib.genAttrs names (
name:
mkHost { inherit root name; }
);
}

View File

@@ -1,7 +1,5 @@
{
root,
util,
}:
{ util }:
{ root }:
{
imports = util.collectNixFiles root;
}

View File

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