Merge pull request 'Refactor(inventory/constraints): use explizit dependency injections instead of specialArgs' (#3553) from hsjobeki/clan-core:clan-services into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3553
This commit is contained in:
hsjobeki
2025-05-12 12:26:45 +00:00
6 changed files with 106 additions and 60 deletions

View File

@@ -1,9 +1,12 @@
{
lib,
config,
resolvedRoles,
instanceName,
moduleName,
allRoles,
}:
{
lib,
config,
...
}:
let
@@ -11,7 +14,7 @@ let
in
{
imports = [
./interface.nix
(lib.modules.importApply ./interface.nix { inherit allRoles; })
# Role assertions
{
config.assertions = lib.foldlAttrs (
@@ -24,7 +27,7 @@ in
"${moduleName}.${instanceName}.roles.${roleName}.min" = {
assertion = memberCount >= roleConstraints.min;
message = ''
The ${moduleName} module requires at least ${builtins.toString roleConstraints.min} members of the '${roleName}' role
The '${moduleName}' module requires at least ${builtins.toString roleConstraints.min} members of the '${roleName}' role
but found '${builtins.toString memberCount}' members within instance '${instanceName}':
${lib.concatLines members}

View File

@@ -1,7 +1,8 @@
{
allRoles,
}:
{
lib,
allRoles,
moduleName,
...
}:
let
@@ -9,12 +10,6 @@ let
rolesAttrs = builtins.groupBy lib.id allRoles;
in
{
options.serviceName = mkOption {
type = types.str;
default = moduleName;
readOnly = true;
visible = false;
};
options.roles = lib.mapAttrs (
_name: _:
mkOption {

View File

@@ -0,0 +1,78 @@
{ lib, config, ... }:
let
inherit (lib) mkOption;
inherit (lib) types;
in
{
options = {
name = mkOption {
description = ''
The name of the module
Mainly used to create an error context while evaluating.
This helps backtracking which module was included; And where an error came from originally.
'';
type = types.str;
};
description = mkOption {
type = types.str;
description = ''
A Short description of the module.
'';
defaultText = "Short description";
default = config.name;
};
categories = mkOption {
default = [ "Uncategorized" ];
description = ''
Categories are used for Grouping and searching.
While initial oriented on [freedesktop](https://specifications.freedesktop.org/menu-spec/latest/category-registry.html) the following categories are allowed
'';
type = types.listOf (
types.enum [
"AudioVideo"
"Audio"
"Video"
"Development"
"Education"
"Game"
"Graphics"
"Social"
"Network"
"Office"
"Science"
"System"
"Settings"
"Utility"
"Uncategorized"
]
);
};
features = mkOption {
description = ''
Enable built-in features for the module
See the documentation for each feature:
- API
'';
type = types.submoduleWith {
modules = [
{
options.API = mkOption {
type = types.bool;
# This is read only, because we don't support turning it off yet
readOnly = true;
default = true;
description = ''
Enables automatic API schema conversion for the interface of this module.
'';
};
}
];
};
default = { };
};
};
}

View File

@@ -232,43 +232,7 @@ in
description = "Meta information about this module itself";
type = submoduleWith {
modules = [
{
options = {
name = mkOption {
description = ''
The name of the module
Mainly used to create an error context while evaluating.
This helps backtracking which module was included; And where an error came from originally.
'';
type = types.str;
};
features = mkOption {
description = ''
Enable built-in features for the module
See the documentation for each feature:
- API
'';
type = types.submoduleWith {
modules = [
{
options.API = mkOption {
type = types.bool;
# This is read only, because we don't support turning it off yet
readOnly = true;
default = true;
description = ''
Enables automatic API schema conversion for the interface of this module.
'';
};
}
];
};
default = { };
};
};
}
./manifest/default.nix
];
};
};

View File

@@ -34,25 +34,33 @@ let
allModules,
}:
lib.evalModules {
specialArgs = {
inherit moduleName resolvedRoles instanceName;
allRoles = getRoles "inventory.modules" allModules moduleName;
};
modules = [
(getFrontmatter allModules.${moduleName} moduleName)
./interface.nix
{
constraints.imports = [
(lib.modules.importApply ../constraints {
inherit moduleName resolvedRoles instanceName;
allRoles = getRoles "inventory.modules" allModules moduleName;
})
];
}
];
};
# For Documentation purposes only
frontmatterOptions =
(lib.evalModules {
specialArgs = {
moduleName = "{moduleName}";
allRoles = [ "{roleName}" ];
};
modules = [
./interface.nix
{
constraints.imports = [
(lib.modules.importApply ../constraints {
moduleName = "{moduleName}";
allRoles = [ "{roleName}" ];
})
];
}
];
}).options;

View File

@@ -1,6 +1,5 @@
{
lib,
specialArgs,
...
}:
let
@@ -76,9 +75,8 @@ in
```
'';
type = types.submoduleWith {
inherit specialArgs;
modules = [
../constraints
];
};
};