Init: eval clanmodule interface with roles

This commit is contained in:
Johannes Kirschbauer
2024-10-07 22:31:46 +02:00
committed by hsjobeki
parent b1d88e5ac3
commit a414981089
3 changed files with 79 additions and 5 deletions

View File

@@ -4,8 +4,11 @@
nixpkgs,
...
}:
let
eval = import ./eval-clan-modules { inherit clan-core nixpkgs lib; };
in
{
evalClanModules = import ./eval-clan-modules { inherit clan-core nixpkgs lib; };
inherit (eval) evalClanModules evalClanModulesWithRoles;
buildClan = import ./build-clan { inherit lib nixpkgs clan-core; };
facts = import ./facts.nix { inherit lib; };
inventory = import ./inventory { inherit lib clan-core; };

View File

@@ -19,7 +19,7 @@ let
# This function takes a list of module names and evaluates them
# evalClanModules :: [ String ] -> { config, options, ... }
evalClanModules =
evalClanModulesLegacy =
modulenames:
let
evaled = lib.evalModules {
@@ -32,6 +32,74 @@ let
] ++ (map (name: clanModules.${name}) modulenames);
};
in
evaled;
lib.warn ''
EvalClanModules doesn't respect role specific interfaces.
The following {module}/default.nix file trying to be imported.
Modules: ${builtins.toJSON modulenames}
This might result in incomplete or incorrect interfaces.
FIX: Use evalClanModuleWithRole instead.
'' evaled;
/*
This function takes a list of module names and evaluates them
Returns a set of interfaces as described below:
Fn :: { ${moduleName} = Module; } -> {
${moduleName} :: {
${roleName}: JSONSchema
}
}
*/
evalClanModulesWithRoles =
clanModules:
let
getRoles =
modulePath:
let
rolesDir = "${modulePath}/roles";
in
if builtins.pathExists rolesDir then
lib.pipe rolesDir [
builtins.readDir
(lib.filterAttrs (_n: v: v == "regular"))
lib.attrNames
(lib.filter (fileName: lib.hasSuffix ".nix" fileName))
(map (fileName: lib.removeSuffix ".nix" fileName))
]
else
[ ];
res = builtins.mapAttrs (
moduleName: module:
let
# module must be a path to the clanModule root by convention
# See: clanModules/flake-module.nix
roles =
assert lib.isPath module;
getRoles module;
in
lib.listToAttrs (
lib.map (role: {
name = role;
value =
(lib.evalModules {
modules = [
baseModule
clan-core.nixosModules.clanCore
# Role interface
(module + "/roles/${role}.nix")
];
}).options.clan.${moduleName} or { };
}) roles
)
) clanModules;
in
res;
in
evalClanModules
{
evalClanModules = evalClanModulesLegacy;
inherit evalClanModulesWithRoles;
}

View File

@@ -94,9 +94,10 @@ let
rolesOf =
moduleName:
let
# null | [ string ]
roles = getRoles self.clanModules.${moduleName};
in
if roles == null then [ "default" ] else roles;
if roles == null then [ ] else roles;
moduleServices = lib.mapAttrs moduleToService (
lib.filterAttrs (n: _v: rolesOf n != [ ]) modulesSchema
);
@@ -129,4 +130,6 @@ in
}
*/
schemaWithModules = schema;
inherit modulesSchema;
}