feat(clan-service): add module context for better error messages

This commit is contained in:
Johannes Kirschbauer
2025-06-14 19:57:59 +02:00
parent f0e3b753dc
commit ca0a56292e
2 changed files with 44 additions and 8 deletions

View File

@@ -19,6 +19,7 @@ let
{ modules, prefix }: { modules, prefix }:
(lib.evalModules { (lib.evalModules {
class = "clan.service"; class = "clan.service";
specialArgs._ctx = prefix;
modules = [ modules = [
./service-module.nix ./service-module.nix
# feature modules # feature modules

View File

@@ -1,8 +1,14 @@
{ lib, config, ... }: {
lib,
config,
_ctx,
...
}:
let let
inherit (lib) mkOption types; inherit (lib) mkOption types;
inherit (types) attrsWith submoduleWith; inherit (types) attrsWith submoduleWith;
errorContext = "Error context: ${lib.concatStringsSep "." _ctx}";
# TODO: # TODO:
# Remove once this gets merged upstream; performs in O(n*log(n) instead of O(n^2)) # Remove once this gets merged upstream; performs in O(n*log(n) instead of O(n^2))
# https://github.com/NixOS/nixpkgs/pull/355616/files # https://github.com/NixOS/nixpkgs/pull/355616/files
@@ -53,7 +59,8 @@ let
# This prints the path where the option should be defined rather than the plain path within settings # This prints the path where the option should be defined rather than the plain path within settings
# "The option `instances.foo.roles.server.machines.test.settings.<>' was accessed but has no value defined. Try setting the option." # "The option `instances.foo.roles.server.machines.test.settings.<>' was accessed but has no value defined. Try setting the option."
prefix = prefix =
[ _ctx
++ [
"instances" "instances"
instanceName instanceName
"roles" "roles"
@@ -78,7 +85,7 @@ let
(lib.setDefaultModuleLocation "Via clan.service module: roles.${roleName}.interface" (lib.setDefaultModuleLocation "Via clan.service module: roles.${roleName}.interface"
config.roles.${roleName}.interface config.roles.${roleName}.interface
) )
(lib.setDefaultModuleLocation "inventory.instances.${instanceName}.roles.${roleName}.settings" (lib.setDefaultModuleLocation "instances.${instanceName}.roles.${roleName}.settings"
config.instances.${instanceName}.roles.${roleName}.settings config.instances.${instanceName}.roles.${roleName}.settings
) )
settings settings
@@ -156,7 +163,9 @@ in
default = throw '' default = throw ''
The clan service module ${config.manifest.name} doesn't define any instances. The clan service module ${config.manifest.name} doesn't define any instances.
Did you forget to create instances via 'inventory.instances'? Did you forget to create instances via 'instances'?
${errorContext}
''; '';
description = '' description = ''
Instances of the service. Instances of the service.
@@ -204,7 +213,9 @@ in
Instance '${name}' of service '${config.manifest.name}' mut define members via 'roles'. Instance '${name}' of service '${config.manifest.name}' mut define members via 'roles'.
To include a machine: To include a machine:
'instances.${name}.roles.<role-name>.machines.<your-machine-name>' must be set. 'instances.${name}.roles.<role-name>.machines.<machine-name>' must be set.
${errorContext}
''; '';
type = attrsWith { type = attrsWith {
placeholder = "roleName"; placeholder = "roleName";
@@ -301,6 +312,8 @@ in
To define multiple instance behavior: To define multiple instance behavior:
`roles.client.perInstance = { ... }: {}` `roles.client.perInstance = { ... }: {}`
${errorContext}
''; '';
type = attrsWith { type = attrsWith {
placeholder = "roleName"; placeholder = "roleName";
@@ -379,7 +392,7 @@ in
}; };
``` ```
- `settings`: The settings of the role, as defined in `inventory` - `settings`: The settings of the role, as defined in `instances`
```nix ```nix
{ {
timeout = 30; timeout = 30;
@@ -438,7 +451,18 @@ in
type = attrsWith { type = attrsWith {
placeholder = "serviceName"; placeholder = "serviceName";
elemType = submoduleWith { elemType = submoduleWith {
modules = [ ./service-module.nix ]; modules = [
{
_module.args._ctx = _ctx ++ [
config.manifest.name
"roles"
roleName
"perInstance"
"services"
];
}
./service-module.nix
];
}; };
}; };
apply = _: throw "Not implemented yet"; apply = _: throw "Not implemented yet";
@@ -554,7 +578,16 @@ in
type = attrsWith { type = attrsWith {
placeholder = "serviceName"; placeholder = "serviceName";
elemType = submoduleWith { elemType = submoduleWith {
modules = [ ./service-module.nix ]; modules = [
{
_module.args._ctx = _ctx ++ [
config.manifest.name
"perMachine"
"services"
];
}
./service-module.nix
];
}; };
}; };
apply = _: throw "Not implemented yet"; apply = _: throw "Not implemented yet";
@@ -605,6 +638,8 @@ in
- 'instances.<instanceName>.roles.<roleName>.machines.<machineName>.settings' should be used instead. - 'instances.<instanceName>.roles.<roleName>.machines.<machineName>.settings' should be used instead.
If that is insufficient, you might also consider using 'roles.<roleName>.perInstance' instead of 'perMachine'. If that is insufficient, you might also consider using 'roles.<roleName>.perInstance' instead of 'perMachine'.
${errorContext}
''; '';
}; };