Inventory: init external modules support

This commit is contained in:
Johannes Kirschbauer
2024-11-21 11:45:31 +01:00
committed by hsjobeki
parent 604c16c30f
commit 25fb899f64
7 changed files with 37 additions and 27 deletions

View File

@@ -102,6 +102,7 @@ in
# We don't specify the type here, for better performance.
inventory = lib.mkOption { type = lib.types.raw; };
inventoryFile = lib.mkOption { type = lib.types.raw; };
serviceConfigs = lib.mkOption { type = lib.types.raw; };
clanModules = lib.mkOption { type = lib.types.raw; };
source = lib.mkOption { type = lib.types.raw; };
meta = lib.mkOption { type = lib.types.raw; };

View File

@@ -159,7 +159,7 @@ in
inventory.machines = lib.mapAttrs (_n: _: { }) config.machines;
}
# Merge the meta attributes from the buildClan function
#
{ inventory.modules = clan-core.clanModules; }
# config.inventory.meta <- config.meta
{ inventory.meta = config.meta; }
# Set default for computed tags
@@ -169,6 +169,7 @@ in
inherit nixosConfigurations;
clanInternals = {
inherit serviceConfigs;
inherit (clan-core) clanModules;
inherit inventoryFile;
inventory = config.inventory;

View File

@@ -16,5 +16,5 @@ in
facts = import ./facts.nix { inherit lib; };
inventory = import ./inventory { inherit lib clan-core; };
jsonschema = import ./jsonschema { inherit lib; };
modules = import ./frontmatter { inherit clan-core lib; };
modules = import ./frontmatter { inherit lib; };
}

View File

@@ -53,7 +53,7 @@ let
}
*/
evalClanModulesWithRoles =
clanModules:
allModules:
let
res = builtins.mapAttrs (
moduleName: module:
@@ -62,7 +62,7 @@ let
roles =
if builtins.elem "inventory" frontmatter.features or [ ] then
assert lib.isPath module;
clan-core.lib.modules.getRoles moduleName
clan-core.lib.modules.getRoles allModules moduleName
else
[ ];
in
@@ -83,7 +83,7 @@ let
}).options.clan.${moduleName} or { };
}) roles
)
) clanModules;
) allModules;
in
res;
in

View File

@@ -1,4 +1,4 @@
{ clan-core, lib }:
{ lib }:
let
# Trim the .nix extension from a filename
trimExtension = name: builtins.substring 0 (builtins.stringLength name - 4) name;
@@ -8,18 +8,20 @@ let
moduleName,
instanceName,
resolvedRoles,
allModules,
}:
lib.evalModules {
specialArgs = {
inherit moduleName resolvedRoles instanceName;
allRoles = getRoles moduleName;
allRoles = getRoles allModules moduleName;
};
modules = [
(getFrontmatter moduleName)
(getFrontmatter allModules.${moduleName} moduleName)
./interface.nix
];
};
# For Documentation purposes only
frontmatterOptions =
(lib.evalModules {
specialArgs = {
@@ -32,26 +34,24 @@ let
}).options;
getRoles =
serviceName:
allModules: serviceName:
lib.mapAttrsToList (name: _value: trimExtension name) (
lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (
builtins.readDir (
if clan-core.clanModules ? ${serviceName} then
clan-core.clanModules.${serviceName} + "/roles"
if allModules ? ${serviceName} then
allModules.${serviceName} + "/roles"
else
throw "ClanModule not found: '${serviceName}'. Make sure the module is added in the 'clanModules' attribute of clan-core."
)
)
);
getConstraints = modulename: (getFrontmatter modulename).constraints;
checkConstraints = args: (evalFrontmatter args).config.constraints.assertions;
getReadme =
modulename:
modulepath: modulename:
let
readme = "${clan-core}/clanModules/${modulename}/README.md";
readme = modulepath + "/README.md";
readmeContents =
if (builtins.pathExists readme) then
(builtins.readFile readme)
@@ -61,9 +61,9 @@ let
readmeContents;
getFrontmatter =
modulename:
modulepath: modulename:
let
content = getReadme modulename;
content = getReadme modulepath modulename;
parts = lib.splitString "---" content;
# Partition the parts into the first part (the readme content) and the rest (the metadata)
parsed = builtins.partition ({ index, ... }: if index >= 2 then false else true) (
@@ -89,12 +89,10 @@ let
in
{
inherit
evalFrontmatter
frontmatterOptions
getFrontmatter
getReadme
getConstraints
checkConstraints
getRoles
;

View File

@@ -38,8 +38,9 @@ let
};
checkService =
serviceName:
builtins.elem "inventory" (clan-core.lib.modules.getFrontmatter serviceName).features or [ ];
modulepath: serviceName:
builtins.elem "inventory"
(clan-core.lib.modules.getFrontmatter modulepath serviceName).features or [ ];
extendMachine =
{ machineConfig, inventory }:
@@ -53,7 +54,7 @@ let
acc
++ [
{
assertion = checkService serviceName;
assertion = checkService inventory.modules.${serviceName} serviceName;
message = ''
Service ${serviceName} cannot be used in inventory. It does not declare the 'inventory' feature.
@@ -94,7 +95,7 @@ let
acc2: instanceName: serviceConfig:
let
roles = clan-core.lib.modules.getRoles serviceName;
roles = clan-core.lib.modules.getRoles inventory.modules serviceName;
resolvedRoles = lib.genAttrs roles (
roleName:
@@ -129,11 +130,11 @@ let
# TODO: maybe optimize this dont lookup the role in inverse roles. Imports are not lazy
roleModules = builtins.map (
role:
if builtins.elem role roles && clan-core.clanModules ? ${serviceName} then
clan-core.clanModules.${serviceName} + "/roles/${role}.nix"
if builtins.elem role roles && inventory.modules ? ${serviceName} then
inventory.modules.${serviceName} + "/roles/${role}.nix"
else
throw "Module ${serviceName} doesn't have role: '${role}'. Role: ${
clan-core.clanModules.${serviceName}
inventory.modules.${serviceName}
}/roles/${role}.nix not found."
) machineRoles;
@@ -151,6 +152,7 @@ let
constraintAssertions = clan-core.lib.modules.checkConstraints {
moduleName = serviceName;
allModules = inventory.modules;
inherit resolvedRoles instanceName;
};
in

View File

@@ -92,6 +92,14 @@ in
./assertions.nix
];
options = {
modules = lib.mkOption {
type = types.attrsOf types.path;
internal = true;
visible = false;
default = { };
defaultText = "clanModules of clan-core";
};
assertions = lib.mkOption {
type = types.listOf types.unspecified;
internal = true;