services: allow inline modules

This commit is contained in:
Johannes Kirschbauer
2025-10-23 18:26:57 +02:00
parent ae5efd9e2f
commit f50475fcfd
3 changed files with 33 additions and 9 deletions

View File

@@ -216,7 +216,7 @@ in
options.extraModules = lib.mkOption {
default = [ ];
type = types.listOf (types.either types.deferredModule types.str);
type = types.listOf types.deferredModule;
};
}
)

View File

@@ -73,15 +73,8 @@ in
}
```
'';
apply = value: if lib.isString value then value else builtins.seq (builtins.toJSON value) value;
default = [ ];
type = types.listOf (
types.oneOf [
types.str
types.path
(types.attrsOf types.anything)
]
);
type = types.listOf types.deferredModule;
};
};
}

View File

@@ -316,3 +316,34 @@ def test_delete_static_service_instance(
# TODO: improve error message
assert "Cannot delete path 'instances.static" in str(excinfo.value)
@pytest.mark.with_core
def test_inline_extra_modules(clan_flake: Callable[..., Flake]) -> None:
"""ExtraModules are excluded from serialization to allow arbitrary inlining"""
# Data that can be mutated via API calls
mutable_inventory_json: Inventory = {
"instances": {
"static": {"module": {"name": "admin"}},
}
}
nix = r"""
{
inventory.instances.static = {
roles.default.extraModules = [
(_: { }) # non-serializable inline module
];
};
}
"""
flake = clan_flake(
{},
raw=nix,
mutable_inventory_json=mutable_inventory_json,
)
# Ensure preconditions
instances = list_service_instances(flake)
assert set(instances.keys()) == {"static"}