From 50cce9052720a5d014b35b69c280ecc090b5f759 Mon Sep 17 00:00:00 2001 From: DavHau Date: Mon, 30 Jun 2025 16:03:48 +0700 Subject: [PATCH] clan services: use service modules from clan-core by default Users will use clan-core services first before making their own. Therefore this is a better default. Also it allow us to simplify rendering of our docs --- lib/clanTest/flake-module.nix | 3 - lib/default.nix | 2 +- lib/modules/clan/module.nix | 2 +- lib/modules/inventory/default.nix | 10 +++- .../distributed-service/inventory-adapter.nix | 3 +- .../distributed-service/resolveModule.nix | 60 ++++++++++--------- 6 files changed, 43 insertions(+), 37 deletions(-) diff --git a/lib/clanTest/flake-module.nix b/lib/clanTest/flake-module.nix index 214615981..c83e9bf8c 100644 --- a/lib/clanTest/flake-module.nix +++ b/lib/clanTest/flake-module.nix @@ -205,9 +205,6 @@ in nixpkgs nix-darwin ; - # By default clan.directory defaults to self, but we don't - # have a sensible default for self here - self = throw "set clan.directory in the test"; }; modules = [ clan-core.modules.clan.default diff --git a/lib/default.nix b/lib/default.nix index 22d9f52ff..26de72211 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -31,7 +31,7 @@ lib.fix ( # ------------------------------------ # ClanLib functions evalClan = clanLib.callLib ./modules/inventory/eval-clan-modules { }; - inventory = clanLib.callLib ./modules/inventory { }; + inventory = clanLib.callLib ./modules/inventory { clan-core = self; }; modules = clanLib.callLib ./modules/inventory/frontmatter { }; test = clanLib.callLib ./test { }; # Custom types diff --git a/lib/modules/clan/module.nix b/lib/modules/clan/module.nix index dabea62c8..795e3f079 100644 --- a/lib/modules/clan/module.nix +++ b/lib/modules/clan/module.nix @@ -238,7 +238,7 @@ in imports = [ ../inventoryClass/builder/default.nix (lib.modules.importApply ../inventoryClass/service-list-from-inputs.nix { - inherit localModuleSet flakeInputs clanLib; + inherit flakeInputs clanLib localModuleSet; }) { inherit inventory directory; diff --git a/lib/modules/inventory/default.nix b/lib/modules/inventory/default.nix index 4509fd074..2eb17d793 100644 --- a/lib/modules/inventory/default.nix +++ b/lib/modules/inventory/default.nix @@ -1,6 +1,12 @@ -{ lib, clanLib }: +{ + lib, + clanLib, + clan-core, +}: let - services = clanLib.callLib ./distributed-service/inventory-adapter.nix { }; + services = clanLib.callLib ./distributed-service/inventory-adapter.nix { + inherit clan-core; + }; in { inherit (services) mapInstances; diff --git a/lib/modules/inventory/distributed-service/inventory-adapter.nix b/lib/modules/inventory/distributed-service/inventory-adapter.nix index 95fdc0ccf..5b73c9328 100644 --- a/lib/modules/inventory/distributed-service/inventory-adapter.nix +++ b/lib/modules/inventory/distributed-service/inventory-adapter.nix @@ -12,10 +12,11 @@ { lib, clanLib, + clan-core, ... }: let - resolveModule = import ./resolveModule.nix { inherit lib; }; + resolveModule = import ./resolveModule.nix { inherit lib clan-core; }; in { mapInstances = diff --git a/lib/modules/inventory/distributed-service/resolveModule.nix b/lib/modules/inventory/distributed-service/resolveModule.nix index 14ed86684..339e02d99 100644 --- a/lib/modules/inventory/distributed-service/resolveModule.nix +++ b/lib/modules/inventory/distributed-service/resolveModule.nix @@ -1,43 +1,45 @@ -{ lib }: +{ lib, clan-core }: { moduleSpec, flakeInputs, localModuleSet, }: let + inputName = if moduleSpec.input == null then "" else moduleSpec.input; + inputError = throw '' + Flake doesn't provide input with name '${moduleSpec.input}' + + Choose one of the following inputs: + - ${ + builtins.concatStringsSep "\n- " ( + lib.attrNames (lib.filterAttrs (_name: input: input ? clan) flakeInputs) + ) + } + + To import a local module from 'clan.modules' remove the 'input' attribute from the module definition + Remove the following line from the module definition: + + ... + - module.input = "${moduleSpec.input}" + + ''; resolvedModuleSet = # If the module.name is self then take the modules defined in the flake # Otherwise its an external input which provides the modules via 'clan.modules' attribute - if moduleSpec.input == null then - localModuleSet - else - let - input = - flakeInputs.${moduleSpec.input} or (throw '' - Flake doesn't provide input with name '${moduleSpec.input}' - - Choose one of the following inputs: - - ${ - builtins.concatStringsSep "\n- " ( - lib.attrNames (lib.filterAttrs (_name: input: input ? clan) flakeInputs) - ) - } - - To import a local module from 'clan.modules' remove the 'input' attribute from the module definition - Remove the following line from the module definition: - - ... - - module.input = "${moduleSpec.input}" - - ''); - clanAttrs = - input.clan - or (throw "It seems the flake input ${moduleSpec.input} doesn't export any clan resources"); - in - clanAttrs.modules; + let + input = + if moduleSpec.input == null then + clan-core + else if moduleSpec.input == "self" then + { clan.modules = localModuleSet; } + 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"); resolvedModule = resolvedModuleSet.${moduleSpec.name} - or (throw "flake doesn't provide clan-module with name ${moduleSpec.name}"); + or (throw "flake input '${inputName}' doesn't provide clan-module with name ${moduleSpec.name}"); in resolvedModule