Merge pull request 'clanServices: clean up, add tests' (#4157) from default-modules into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4157
This commit is contained in:
hsjobeki
2025-07-01 12:09:28 +00:00
15 changed files with 144 additions and 35 deletions

View File

@@ -149,6 +149,14 @@ in
modules = [
clan-core.modules.clan.default
{
self = {
inputs = {
# Simulate flake: 'self.inputs.self'
# Needed because local modules are imported from inputs.self
self = config;
set_inputs_in_tests_fixture_warning = throw "'self.inputs' within test needs to be set manually. Set 'clan.self.inputs' to mock inputs=`{}`";
};
};
_prefix = [
"checks"
"<system>"

View File

@@ -1,5 +1,8 @@
{
lib,
# TODO: Get rid of self here.
# DONT add any new functions that depend on self here.
# If a lib function depends on a piece in clan-core add that piece to the function arguments
self,
...
}:
@@ -31,7 +34,7 @@ lib.fix (
# ------------------------------------
# ClanLib functions
evalClan = clanLib.callLib ./modules/inventory/eval-clan-modules { };
inventory = clanLib.callLib ./modules/inventory { clan-core = self; };
inventory = clanLib.callLib ./modules/inventory { };
modules = clanLib.callLib ./modules/inventory/frontmatter { };
test = clanLib.callLib ./test { };
# Custom types

View File

@@ -2,6 +2,7 @@
lib,
clanLib,
self,
config,
# TODO: Use dependency injection to allow for testing
# inventoryInterface,
...
@@ -24,6 +25,18 @@ in
description = ''
This is used to import external clan modules.
'';
# Workaround for lib.clan
apply =
s:
if lib.isAttrs s then
s
// {
inputs = (s.inputs or { }) // {
self.clan = config;
};
}
else
s;
};
directory = lib.mkOption {

View File

@@ -248,7 +248,8 @@ in
{
distributedServices = clanLib.inventory.mapInstances {
inherit (config) inventory;
inherit localModuleSet flakeInputs;
inherit flakeInputs;
clanCoreModules = clan-core.clan.modules;
prefix = [ "distributedServices" ];
};
machines = config.distributedServices.allMachines;

View File

@@ -1,12 +1,9 @@
{
lib,
clanLib,
clan-core,
}:
let
services = clanLib.callLib ./distributed-service/inventory-adapter.nix {
inherit clan-core;
};
services = clanLib.callLib ./distributed-service/inventory-adapter.nix { };
in
{
inherit (services) mapInstances;

View File

@@ -18,6 +18,9 @@ in
inherit lib;
clanLib = self.clanLib;
};
legacyPackages.eval-tests-resolve-module = import ./test-resolve-module.nix {
inherit lib;
};
checks = {
eval-lib-distributedServices = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } ''
@@ -30,6 +33,16 @@ in
touch $out
'';
eval-tests-resolve-module = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } ''
export HOME="$(realpath .)"
nix-unit --eval-store "$HOME" \
--extra-experimental-features flakes \
--show-trace \
${inputOverrides} \
--flake ${self}#legacyPackages.${system}.eval-tests-resolve-module
touch $out
'';
};
};
}

View File

@@ -12,11 +12,10 @@
{
lib,
clanLib,
clan-core,
...
}:
let
resolveModule = import ./resolveModule.nix { inherit lib clan-core; };
resolveModule = import ./resolveModule.nix { inherit lib; };
in
{
mapInstances =
@@ -25,7 +24,7 @@ in
flakeInputs,
# The clan inventory
inventory,
localModuleSet,
clanCoreModules,
prefix ? [ ],
}:
let
@@ -37,8 +36,7 @@ in
let
resolvedModule = resolveModule {
moduleSpec = instance.module;
inherit localModuleSet;
inherit flakeInputs;
inherit flakeInputs clanCoreModules;
};
# Every instance includes machines via roles

View File

@@ -1,11 +1,10 @@
{ lib, clan-core }:
{ lib }:
{
moduleSpec,
flakeInputs,
localModuleSet,
clanCoreModules,
}:
let
inputName = if moduleSpec.input == null then "<clan>" else moduleSpec.input;
inputError = throw ''
Flake doesn't provide input with name '${moduleSpec.input}'
@@ -29,21 +28,28 @@ let
let
input =
if moduleSpec.input == null then
clan-core
else if moduleSpec.input == "self" then
{ clan.modules = localModuleSet; }
{ clan.modules = clanCoreModules; }
else
flakeInputs.${moduleSpec.input} or inputError;
in
input.clan.modules
or (throw "flake input ${moduleSpec.input} doesn't export any clan services via the `clan.modules` output attribute");
or (throw "flake input '${moduleSpec.input}' doesn't export any clan services via the `clan.modules` output attribute");
resolvedModule =
resolvedModuleSet.${moduleSpec.name} or (throw ''
flake input '${inputName}' doesn't provide clan-module with name ${moduleSpec.name}.
${
lib.optionalString (
moduleSpec.input != null
) "flake input '${moduleSpec.input}' doesn't provide clan-module with name '${moduleSpec.name}'."
}${
lib.optionalString (
moduleSpec.input == null
) "clan-core doesn't provide clan-module with name '${moduleSpec.name}'."
}
Set `module.name = "self"` if the module is defined in your own flake.
Set `module.input = "self"` if the module is defined in your own flake.
Set `module.input = "<flake-input>" if the module is defined by a flake input called `<flake-input>`.
Unset `module.input` (or set module.input = null) - to use the clan-core module '${moduleSpec.name}'
'');
in
resolvedModule

View File

@@ -0,0 +1,69 @@
# Run: nix-unit ./test-resolve-module.nix
{
lib ? import <nixpkgs/lib>,
}:
let
resolveModule = import ./resolveModule.nix { inherit lib; };
fromSpec =
moduleSpec:
resolveModule {
inherit moduleSpec;
flakeInputs = {
self.clan.modules = {
foo = {
name = "self/foo";
};
};
};
clanCoreModules = {
foo = {
name = "clan/foo";
};
bar = {
name = "clan/bar";
};
};
};
in
{
test_default_clan_core = {
expr = fromSpec {
name = "foo";
input = null;
};
expected = {
name = "clan/foo";
};
};
test_self_module = {
expr = fromSpec {
name = "foo";
input = "self";
};
expected = {
name = "self/foo";
};
};
test_missing_self_module = {
expr = fromSpec {
name = "bar";
input = "self";
};
expectedError = {
type = "ThrownError";
msg = "flake input 'self' doesn't provide clan-module with name 'bar'";
};
};
test_missing_core_module = {
expr = fromSpec {
name = "nana";
input = null;
};
expectedError = {
type = "ThrownError";
msg = "clan-core doesn't provide clan-module with name 'nana'";
};
};
}

View File

@@ -27,27 +27,27 @@ let
];
}).config;
flakeInputsFixture = {
# Example upstream module
upstream.clan.modules = {
uzzi = {
_class = "clan.service";
manifest = {
name = "uzzi-from-upstream";
};
};
};
};
callInventoryAdapter =
inventoryModule:
let
inventory = evalInventory inventoryModule;
flakeInputsFixture = {
self.clan.modules = inventory.modules;
# Example upstream module
upstream.clan.modules = {
uzzi = {
_class = "clan.service";
manifest = {
name = "uzzi-from-upstream";
};
};
};
};
in
clanLib.inventory.mapInstances {
clanCoreModules = { };
flakeInputs = flakeInputsFixture;
inherit inventory;
localModuleSet = inventory.modules;
};
in
{