Compare commits

..

10 Commits

6 changed files with 95 additions and 128 deletions

View File

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

View File

@@ -4,55 +4,50 @@
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; };
modules = mkModules {
registry = import ./registry.nix {
inherit lib;
hosts = self.nixosConfigurations;
root = self + "/registry";
};
modules = import ./module.nix {
inherit util;
root = self + "/modules";
};
guestModules = guests.mkGuests {
guestModules = import ./guest.nix {
inherit
inputs
lib
registry
specialArgs
util
;
repositoryModules = modules;
root = self + "/guests";
};
#----------------------------------------------------------
collectedRegistry = registryAssembly.collect { hosts = self.nixosConfigurations; };
specialArgs = {
inherit
inputs
self
util
;
registry = collectedRegistry;
registry = registry.collected;
};
guests = import ./guest.nix {
inherit
inputs
lib
specialArgs
registryAssembly
util
;
repositoryModules = modules;
};
hosts = import ./host.nix {
in
{
nixosConfigurations = import ./host.nix {
inherit
inputs
lib
specialArgs
guestModules
registryAssembly
;
registryModule = registry.module;
repositoryModules = modules;
root = self + "/hosts";
};
in
{
inherit (hosts) mkHosts;
}

View File

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

View File

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

View File

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

View File

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