Compare commits
8 Commits
dc74538cfe
...
d3d3987f86
| Author | SHA1 | Date | |
|---|---|---|---|
| d3d3987f86 | |||
| 8dde08bbcb | |||
| f5b854ca02 | |||
| 9f2957f4be | |||
| 98711c9575 | |||
| 3975548046 | |||
| ba3984d4f5 | |||
| cd721b5d6d |
@@ -76,9 +76,13 @@
|
|||||||
{ self, nixpkgs, ... }@inputs:
|
{ self, nixpkgs, ... }@inputs:
|
||||||
let
|
let
|
||||||
inherit (nixpkgs) lib;
|
inherit (nixpkgs) lib;
|
||||||
|
flake = import ./flake { inherit lib inputs self; };
|
||||||
in
|
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
|
# A project shell for agent-local resources that should travel with this
|
||||||
# checkout rather than the operator's global profile.
|
# checkout rather than the operator's global profile.
|
||||||
|
|||||||
@@ -4,50 +4,55 @@
|
|||||||
self,
|
self,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
|
# TODO: refactor the registry code
|
||||||
|
registryAssembly = import ./registry.nix { inherit lib self; };
|
||||||
|
|
||||||
util = import ../lib { inherit lib; };
|
util = import ../lib { inherit lib; };
|
||||||
|
mkModules = import ./module.nix { inherit util; };
|
||||||
|
|
||||||
registry = import ./registry.nix {
|
modules = mkModules {
|
||||||
inherit lib;
|
|
||||||
hosts = self.nixosConfigurations;
|
|
||||||
root = self + "/registry";
|
|
||||||
};
|
|
||||||
|
|
||||||
modules = import ./module.nix {
|
|
||||||
inherit util;
|
|
||||||
root = self + "/modules";
|
root = self + "/modules";
|
||||||
};
|
};
|
||||||
|
|
||||||
guestModules = import ./guest.nix {
|
guestModules = guests.mkGuests {
|
||||||
inherit
|
|
||||||
inputs
|
|
||||||
lib
|
|
||||||
registry
|
|
||||||
specialArgs
|
|
||||||
util
|
|
||||||
;
|
|
||||||
repositoryModules = modules;
|
|
||||||
root = self + "/guests";
|
root = self + "/guests";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#----------------------------------------------------------
|
||||||
|
|
||||||
|
collectedRegistry = registryAssembly.collect { hosts = self.nixosConfigurations; };
|
||||||
|
|
||||||
specialArgs = {
|
specialArgs = {
|
||||||
inherit
|
inherit
|
||||||
inputs
|
inputs
|
||||||
self
|
self
|
||||||
util
|
util
|
||||||
;
|
;
|
||||||
registry = registry.collected;
|
registry = collectedRegistry;
|
||||||
};
|
};
|
||||||
in
|
|
||||||
{
|
guests = import ./guest.nix {
|
||||||
nixosConfigurations = import ./host.nix {
|
inherit
|
||||||
|
inputs
|
||||||
|
lib
|
||||||
|
specialArgs
|
||||||
|
registryAssembly
|
||||||
|
util
|
||||||
|
;
|
||||||
|
repositoryModules = modules;
|
||||||
|
};
|
||||||
|
|
||||||
|
hosts = import ./host.nix {
|
||||||
inherit
|
inherit
|
||||||
inputs
|
inputs
|
||||||
lib
|
lib
|
||||||
specialArgs
|
specialArgs
|
||||||
guestModules
|
guestModules
|
||||||
|
registryAssembly
|
||||||
;
|
;
|
||||||
registryModule = registry.module;
|
|
||||||
repositoryModules = modules;
|
repositoryModules = modules;
|
||||||
root = self + "/hosts";
|
|
||||||
};
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
inherit (hosts) mkHosts;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
{
|
{
|
||||||
inputs,
|
inputs,
|
||||||
lib,
|
lib,
|
||||||
root,
|
|
||||||
specialArgs,
|
specialArgs,
|
||||||
repositoryModules,
|
repositoryModules,
|
||||||
registry,
|
registryAssembly,
|
||||||
util,
|
util,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
@@ -66,7 +65,7 @@ let
|
|||||||
enable = lib.mkEnableOption "a reverse-proxy route for this guest";
|
enable = lib.mkEnableOption "a reverse-proxy route for this guest";
|
||||||
|
|
||||||
route = lib.mkOption {
|
route = lib.mkOption {
|
||||||
type = registry.types.route;
|
type = registryAssembly.definitions.route.type;
|
||||||
description = "Route registry entry published for this guest.";
|
description = "Route registry entry published for this guest.";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -248,7 +247,7 @@ let
|
|||||||
inputs.stylix.nixosModules.stylix
|
inputs.stylix.nixosModules.stylix
|
||||||
|
|
||||||
repositoryModules
|
repositoryModules
|
||||||
registry.module
|
registryAssembly.module
|
||||||
cfg.interior
|
cfg.interior
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -269,11 +268,15 @@ let
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
in
|
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;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
inputs,
|
inputs,
|
||||||
lib,
|
lib,
|
||||||
root,
|
|
||||||
specialArgs,
|
specialArgs,
|
||||||
repositoryModules,
|
repositoryModules,
|
||||||
|
|
||||||
guestModules,
|
guestModules,
|
||||||
registryModule,
|
# TODO refactor registry
|
||||||
|
registryAssembly,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
mkHost =
|
mkHost =
|
||||||
@@ -26,7 +26,7 @@ let
|
|||||||
guestModules
|
guestModules
|
||||||
|
|
||||||
# TODO: refactor
|
# TODO: refactor
|
||||||
registryModule (root + "/${name}")
|
registryAssembly.module (root + "/${name}")
|
||||||
|
|
||||||
{
|
{
|
||||||
networking.hostName = name;
|
networking.hostName = name;
|
||||||
@@ -35,11 +35,15 @@ let
|
|||||||
];
|
];
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
let
|
{
|
||||||
directories = lib.filterAttrs (_name: type: type == "directory") (builtins.readDir root);
|
mkHosts =
|
||||||
names = lib.attrNames directories;
|
{ root }:
|
||||||
in
|
let
|
||||||
lib.genAttrs names (
|
directories = lib.filterAttrs (_name: type: type == "directory") (builtins.readDir root);
|
||||||
name:
|
names = lib.attrNames directories;
|
||||||
mkHost { inherit root name; }
|
in
|
||||||
)
|
lib.genAttrs names (
|
||||||
|
name:
|
||||||
|
mkHost { inherit root name; }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
{
|
{ util }:
|
||||||
root,
|
{ root }:
|
||||||
util,
|
|
||||||
}:
|
|
||||||
{
|
{
|
||||||
imports = util.collectNixFiles root;
|
imports = util.collectNixFiles root;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,35 @@
|
|||||||
{ hosts, lib, root }:
|
{ lib, self }:
|
||||||
let
|
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 = {
|
options = {
|
||||||
assertion = lib.mkOption {
|
assertion = mkOption {
|
||||||
type = lib.types.functionTo lib.types.bool;
|
type = types.functionTo types.bool;
|
||||||
description = ''
|
description = ''
|
||||||
Predicate run against this registry's flattened collected entries.
|
Predicate run against this registry's flattened collected entries.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
message = lib.mkOption {
|
message = mkOption {
|
||||||
type = lib.types.str;
|
type = types.str;
|
||||||
description = ''
|
description = ''
|
||||||
Error message shown when this registry validation check fails.
|
Error message shown when this registry validation check fails.
|
||||||
'';
|
'';
|
||||||
@@ -19,7 +38,7 @@ let
|
|||||||
};
|
};
|
||||||
|
|
||||||
predicates = {
|
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 // {
|
registryLib = lib // {
|
||||||
@@ -30,23 +49,23 @@ let
|
|||||||
|
|
||||||
registryDefinitionModule = {
|
registryDefinitionModule = {
|
||||||
options = {
|
options = {
|
||||||
type = lib.mkOption {
|
type = mkOption {
|
||||||
type = lib.types.raw;
|
type = types.raw;
|
||||||
description = ''
|
description = ''
|
||||||
NixOS option type for one registry entry.
|
NixOS option type for one registry entry.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
replicate = lib.mkOption {
|
replicate = mkOption {
|
||||||
type = lib.types.bool;
|
type = 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 = lib.mkOption {
|
checks = mkOption {
|
||||||
type = lib.types.listOf checkType;
|
type = 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.
|
||||||
@@ -55,10 +74,10 @@ let
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
registryName = fileName: lib.removeSuffix ".nix" fileName;
|
registryName = fileName: removeSuffix ".nix" fileName;
|
||||||
|
|
||||||
registryOption = registry: lib.mkOption {
|
registryOption = registry: mkOption {
|
||||||
type = lib.types.attrsOf (lib.types.attrsOf registry.type);
|
type = types.attrsOf (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.
|
||||||
@@ -79,19 +98,19 @@ let
|
|||||||
inherit (evaluated.config) type replicate checks;
|
inherit (evaluated.config) type replicate checks;
|
||||||
};
|
};
|
||||||
|
|
||||||
definitions = lib.mapAttrs' (
|
definitions = mapAttrs' (
|
||||||
name: type:
|
name: type:
|
||||||
let
|
let
|
||||||
path = root + "/${name}";
|
path = self + "/registry/${name}";
|
||||||
in
|
in
|
||||||
lib.nameValuePair (registryName name) (mkRegistry path)
|
nameValuePair (registryName name) (mkRegistry path)
|
||||||
) (lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (builtins.readDir root));
|
) (filterAttrs (name: type: type == "regular" && hasSuffix ".nix" name) (builtins.readDir (self + "/registry")));
|
||||||
|
|
||||||
flattenRegistryEntries = registryName: data:
|
flattenRegistryEntries = registryName: data:
|
||||||
lib.concatLists (
|
concatLists (
|
||||||
lib.mapAttrsToList (
|
mapAttrsToList (
|
||||||
host: entries:
|
host: entries:
|
||||||
lib.mapAttrsToList (entry: value: {
|
mapAttrsToList (entry: value: {
|
||||||
path = "registry.${registryName}.${host}.${entry}";
|
path = "registry.${registryName}.${host}.${entry}";
|
||||||
inherit host entry value;
|
inherit host entry value;
|
||||||
}) entries
|
}) entries
|
||||||
@@ -99,17 +118,17 @@ let
|
|||||||
);
|
);
|
||||||
|
|
||||||
rawDeclarations = registryName: hosts:
|
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:
|
replicatedEntries = registryName: raw:
|
||||||
let
|
let
|
||||||
entries = lib.concatLists (
|
entries = concatLists (
|
||||||
lib.concatLists (
|
concatLists (
|
||||||
lib.mapAttrsToList (
|
mapAttrsToList (
|
||||||
_evalHost: hostDeclarations:
|
_evalHost: hostDeclarations:
|
||||||
lib.mapAttrsToList (
|
mapAttrsToList (
|
||||||
hostName: declarations:
|
hostName: declarations:
|
||||||
lib.mapAttrsToList (entryName: value: {
|
mapAttrsToList (entryName: value: {
|
||||||
path = "registry.${registryName}.${hostName}.${entryName}";
|
path = "registry.${registryName}.${hostName}.${entryName}";
|
||||||
inherit entryName value;
|
inherit entryName value;
|
||||||
}) declarations
|
}) declarations
|
||||||
@@ -118,9 +137,9 @@ let
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
in
|
in
|
||||||
lib.foldl' (
|
foldl' (
|
||||||
acc: entry:
|
acc: entry:
|
||||||
if lib.hasAttr entry.entryName acc then
|
if 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
|
||||||
@@ -133,13 +152,13 @@ let
|
|||||||
|
|
||||||
replicatedData = registryName: hosts: raw:
|
replicatedData = registryName: hosts: raw:
|
||||||
let
|
let
|
||||||
entries = lib.mapAttrs (_entryName: entry: entry.value) (replicatedEntries registryName raw);
|
entries = mapAttrs (_entryName: entry: entry.value) (replicatedEntries registryName raw);
|
||||||
in
|
in
|
||||||
lib.mapAttrs (_hostName: _host: entries) hosts;
|
mapAttrs (_hostName: _host: entries) hosts;
|
||||||
|
|
||||||
validationAssertions = data:
|
validationAssertions = data:
|
||||||
lib.concatLists (
|
concatLists (
|
||||||
lib.mapAttrsToList (
|
mapAttrsToList (
|
||||||
registryName: registry:
|
registryName: registry:
|
||||||
let
|
let
|
||||||
entries = flattenRegistryEntries registryName data.${registryName};
|
entries = flattenRegistryEntries registryName data.${registryName};
|
||||||
@@ -154,28 +173,28 @@ let
|
|||||||
assertValid = data:
|
assertValid = data:
|
||||||
let
|
let
|
||||||
assertions = validationAssertions data;
|
assertions = validationAssertions data;
|
||||||
failures = lib.filter (assertion: !assertion.assertion) assertions;
|
failures = filter (assertion: !assertion.assertion) assertions;
|
||||||
in
|
in
|
||||||
assert lib.assertMsg (lib.all (assertion: assertion.assertion) assertions) (
|
assert lib.assertMsg (all (assertion: assertion.assertion) assertions) (
|
||||||
lib.concatMapStringsSep "\n" (assertion: assertion.message) failures
|
concatMapStringsSep "\n" (assertion: assertion.message) failures
|
||||||
);
|
);
|
||||||
data;
|
data;
|
||||||
|
|
||||||
join = registry: host: entry:
|
join = registry: host: entry:
|
||||||
lib.filterAttrs (_name: value: value != null) (
|
filterAttrs (_name: value: value != null) (
|
||||||
lib.mapAttrs (registryName: _registry: (registry.${registryName}.${host} or { }).${entry} or null) definitions
|
mapAttrs (registryName: _registry: (registry.${registryName}.${host} or { }).${entry} or null) definitions
|
||||||
);
|
);
|
||||||
|
|
||||||
collectData =
|
collectData =
|
||||||
{ hosts }:
|
{ hosts }:
|
||||||
let
|
let
|
||||||
raw = lib.mapAttrs (registryName: _registry: rawDeclarations registryName hosts) definitions;
|
raw = mapAttrs (registryName: _registry: rawDeclarations registryName hosts) definitions;
|
||||||
data = lib.mapAttrs (
|
data = 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
|
||||||
lib.mapAttrs (hostName: host: host.config.registry.${registryName}.${hostName} or { }) hosts
|
mapAttrs (hostName: host: host.config.registry.${registryName}.${hostName} or { }) hosts
|
||||||
) definitions;
|
) definitions;
|
||||||
in
|
in
|
||||||
assertValid data;
|
assertValid data;
|
||||||
@@ -187,11 +206,11 @@ let
|
|||||||
};
|
};
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
types = lib.mapAttrs (_name: definition: definition.type) definitions;
|
inherit definitions predicates;
|
||||||
|
|
||||||
module = {
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user