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

@@ -16,7 +16,6 @@ nixosLib.runTest (
name = "service-mycelium"; name = "service-mycelium";
clan = { clan = {
test.useContainers = false; test.useContainers = false;
directory = ./.; directory = ./.;
modules."@clan/mycelium" = ../../clanServices/mycelium/default.nix; modules."@clan/mycelium" = ../../clanServices/mycelium/default.nix;

View File

@@ -5,6 +5,7 @@
}: }:
let let
testFlake = clanLib.clan { testFlake = clanLib.clan {
self = { };
# Point to the folder of the module # Point to the folder of the module
# TODO: make this optional # TODO: make this optional
directory = ./..; directory = ./..;

View File

@@ -6,7 +6,6 @@
name = "service-mycelium"; name = "service-mycelium";
clan = { clan = {
test.useContainers = false; test.useContainers = false;
directory = ./.; directory = ./.;
inventory = { inventory = {

View File

@@ -5,6 +5,7 @@
}: }:
let let
testFlake = clanLib.clan { testFlake = clanLib.clan {
self = { };
# Point to the folder of the module # Point to the folder of the module
# TODO: make this optional # TODO: make this optional
directory = ./..; directory = ./..;

View File

@@ -6,6 +6,7 @@
let let
testFlake = testFlake =
(clanLib.clan { (clanLib.clan {
self = { };
directory = ./vm; directory = ./vm;
machines.jon = { machines.jon = {

View File

@@ -149,6 +149,14 @@ in
modules = [ modules = [
clan-core.modules.clan.default 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 = [ _prefix = [
"checks" "checks"
"<system>" "<system>"

View File

@@ -1,5 +1,8 @@
{ {
lib, 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, self,
... ...
}: }:
@@ -31,7 +34,7 @@ lib.fix (
# ------------------------------------ # ------------------------------------
# ClanLib functions # ClanLib functions
evalClan = clanLib.callLib ./modules/inventory/eval-clan-modules { }; 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 { }; modules = clanLib.callLib ./modules/inventory/frontmatter { };
test = clanLib.callLib ./test { }; test = clanLib.callLib ./test { };
# Custom types # Custom types

View File

@@ -2,6 +2,7 @@
lib, lib,
clanLib, clanLib,
self, self,
config,
# TODO: Use dependency injection to allow for testing # TODO: Use dependency injection to allow for testing
# inventoryInterface, # inventoryInterface,
... ...
@@ -24,6 +25,18 @@ in
description = '' description = ''
This is used to import external clan modules. 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 { directory = lib.mkOption {

View File

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

View File

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

View File

@@ -18,6 +18,9 @@ in
inherit lib; inherit lib;
clanLib = self.clanLib; clanLib = self.clanLib;
}; };
legacyPackages.eval-tests-resolve-module = import ./test-resolve-module.nix {
inherit lib;
};
checks = { checks = {
eval-lib-distributedServices = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } '' eval-lib-distributedServices = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } ''
@@ -30,6 +33,16 @@ in
touch $out 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, lib,
clanLib, clanLib,
clan-core,
... ...
}: }:
let let
resolveModule = import ./resolveModule.nix { inherit lib clan-core; }; resolveModule = import ./resolveModule.nix { inherit lib; };
in in
{ {
mapInstances = mapInstances =
@@ -25,7 +24,7 @@ in
flakeInputs, flakeInputs,
# The clan inventory # The clan inventory
inventory, inventory,
localModuleSet, clanCoreModules,
prefix ? [ ], prefix ? [ ],
}: }:
let let
@@ -37,8 +36,7 @@ in
let let
resolvedModule = resolveModule { resolvedModule = resolveModule {
moduleSpec = instance.module; moduleSpec = instance.module;
inherit localModuleSet; inherit flakeInputs clanCoreModules;
inherit flakeInputs;
}; };
# Every instance includes machines via roles # Every instance includes machines via roles

View File

@@ -1,11 +1,10 @@
{ lib, clan-core }: { lib }:
{ {
moduleSpec, moduleSpec,
flakeInputs, flakeInputs,
localModuleSet, clanCoreModules,
}: }:
let let
inputName = if moduleSpec.input == null then "<clan>" else moduleSpec.input;
inputError = throw '' inputError = throw ''
Flake doesn't provide input with name '${moduleSpec.input}' Flake doesn't provide input with name '${moduleSpec.input}'
@@ -29,21 +28,28 @@ let
let let
input = input =
if moduleSpec.input == null then if moduleSpec.input == null then
clan-core { clan.modules = clanCoreModules; }
else if moduleSpec.input == "self" then
{ clan.modules = localModuleSet; }
else else
flakeInputs.${moduleSpec.input} or inputError; flakeInputs.${moduleSpec.input} or inputError;
in in
input.clan.modules 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 = resolvedModule =
resolvedModuleSet.${moduleSpec.name} or (throw '' 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>`. 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 in
resolvedModule 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; }).config;
flakeInputsFixture = {
# Example upstream module
upstream.clan.modules = {
uzzi = {
_class = "clan.service";
manifest = {
name = "uzzi-from-upstream";
};
};
};
};
callInventoryAdapter = callInventoryAdapter =
inventoryModule: inventoryModule:
let let
inventory = evalInventory inventoryModule; 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 in
clanLib.inventory.mapInstances { clanLib.inventory.mapInstances {
clanCoreModules = { };
flakeInputs = flakeInputsFixture; flakeInputs = flakeInputsFixture;
inherit inventory; inherit inventory;
localModuleSet = inventory.modules;
}; };
in in
{ {