From daea2da358287f1059dc8f73afbdd1616e6b01ff Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 16 Sep 2025 15:28:12 +0200 Subject: [PATCH] lib: move clan out of lib into flake This allows clanLib to stay agnostic and be more testable --- lib/clan/default.nix | 52 +++++++++++++++++++++++++++++ lib/default.nix | 74 +++++++++++++++++------------------------ lib/flake-module.nix | 44 ++++++++++++++++++++++-- lib/modules/default.nix | 59 -------------------------------- 4 files changed, 124 insertions(+), 105 deletions(-) create mode 100644 lib/clan/default.nix delete mode 100644 lib/modules/default.nix diff --git a/lib/clan/default.nix b/lib/clan/default.nix new file mode 100644 index 000000000..bdd41c88b --- /dev/null +++ b/lib/clan/default.nix @@ -0,0 +1,52 @@ +## WARNING: Do not add core logic here. +## This is only a wrapper such that 'clan' can be called as a function. +{ + lib, + clan-core, +}: +/** + Function that takes clan options as function arguments. + + It behaves equivalent to: + + ```nix + { + imports = [ + clan-core.modules.clan.default + args + ]; + } + ``` + + Arguments: + + - self: Reference to the current flake. This is required be passed. + - ...: All other arguments - Which are options as of the 'clan.*' module + + Returns: + The clan configuration. +*/ +{ + self ? lib.warn "Argument: 'self' must be set" null, # Reference to the current flake + ... +}@m: +let + nixpkgs = self.inputs.nixpkgs or clan-core.inputs.nixpkgs; + nix-darwin = self.inputs.nix-darwin or clan-core.inputs.nix-darwin; +in +lib.evalModules { + class = "clan"; + specialArgs = { + inherit + self + ; + inherit + nixpkgs + nix-darwin + ; + }; + modules = [ + clan-core.modules.clan.default + m + ]; +} diff --git a/lib/default.nix b/lib/default.nix index 2697ffb73..e77897acb 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,54 +1,42 @@ { 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 ? throw "'self' should not be used in lib/default.nix, dont depend on it. It will be removed in short notice.", ... }: # Produces the # 'clanLib' attribute set # Wrapped with fix, so we can depend on other clanLib functions without passing the whole flake lib.fix ( - clanLib: let - buildClanLib = ( - clanLib.callLib ./modules { - clan-core = self; - } - ); + f = clanLib: { + __unfix__ = f; + clan = throw "lib.clan is not yet initialized. Use lib.clan exported by the clan-core flake."; + /** + Like callPackage, but doesn't try to automatically detect arguments + 'lib' and 'clanLib' are always passed, plus the additional arguments + */ + callLib = file: args: import file ({ inherit lib clanLib; } // args); + + evalService = clanLib.callLib ./modules/inventory/distributed-service/evalService.nix { }; + # ------------------------------------ + # ClanLib functions + inventory = clanLib.callLib ./modules/inventory { }; + test = clanLib.callLib ./test { }; + flake-inputs = clanLib.callLib ./flake-inputs.nix { }; + # Custom types + types = clanLib.callLib ./types { }; + + # Plain imports. + introspection = import ./introspection { inherit lib; }; + jsonschema = import ./jsonschema { inherit lib; }; + facts = import ./facts.nix { inherit lib; }; + docs = import ./docs.nix { inherit lib; }; + + # flakes + flakes = clanLib.callLib ./flakes.nix { }; + + # TODO: Flatten our lib functions like this: + resolveModule = clanLib.callLib ./resolve-module { }; + }; in - { - - inherit (buildClanLib) - buildClan - clan - ; - /** - Like callPackage, but doesn't try to automatically detect arguments - 'lib' and 'clanLib' are always passed, plus the additional arguments - */ - callLib = file: args: import file ({ inherit lib clanLib; } // args); - - evalService = clanLib.callLib ./modules/inventory/distributed-service/evalService.nix { }; - # ------------------------------------ - # ClanLib functions - inventory = clanLib.callLib ./modules/inventory { }; - test = clanLib.callLib ./test { }; - flake-inputs = clanLib.callLib ./flake-inputs.nix { }; - # Custom types - types = clanLib.callLib ./types { }; - - # Plain imports. - introspection = import ./introspection { inherit lib; }; - jsonschema = import ./jsonschema { inherit lib; }; - facts = import ./facts.nix { inherit lib; }; - docs = import ./docs.nix { inherit lib; }; - - # flakes - flakes = clanLib.callLib ./flakes.nix { }; - - # TODO: Flatten our lib functions like this: - resolveModule = clanLib.callLib ./resolve-module { }; - } + f ) diff --git a/lib/flake-module.nix b/lib/flake-module.nix index c2d3f3545..6a0de7a12 100644 --- a/lib/flake-module.nix +++ b/lib/flake-module.nix @@ -17,9 +17,47 @@ rec { ./jsonschema/flake-module.nix ./types/flake-module.nix ]; - flake.clanLib = import ./default.nix { - inherit lib inputs self; - }; + flake.clanLib = + let + clanLib = import ./default.nix { + inherit lib inputs self; + }; + in + # Extend clanLib here by lib.clan + # This allows clanLib to stay agnostic from flakes or clan-core + lib.fix ( + lib.extends (final: _: { + buildClan = + module: + lib.warn '' + ==================== DEPRECATION NOTICE ==================== + Please migrate + from: 'clan = inputs..lib.buildClan' + to : 'clan = inputs..lib.clan' + in your flake.nix. + + Please also migrate + from: 'inherit (clan) nixosConfigurations clanInternals; ' + to : " + inherit (clan.config) nixosConfigurations clanInternals; + clan = clan.config; + " + in your flake.nix. + + Reason: + - Improves consistency between flake-parts and non-flake-parts users. + + - It also allows us to use the top level attribute 'clan' to expose + attributes that can be used for cross-clan functionality. + ============================================================ + '' (final.clan module).config; + clan = import ./clan { + inherit lib; + clan-core = self; + }; + }) clanLib.__unfix__ + ); + # TODO: remove this legacy alias flake.lib = flake.clanLib; diff --git a/lib/modules/default.nix b/lib/modules/default.nix deleted file mode 100644 index 87071b479..000000000 --- a/lib/modules/default.nix +++ /dev/null @@ -1,59 +0,0 @@ -## WARNING: Do not add core logic here. -## This is only a wrapper such that 'clan' can be called as a function. -{ - lib, - clan-core, - ... -}: -rec { - buildClan = - module: - lib.warn '' - ==================== DEPRECATION NOTICE ==================== - Please migrate - from: 'clan = inputs..lib.buildClan' - to : 'clan = inputs..lib.clan' - in your flake.nix. - - Please also migrate - from: 'inherit (clan) nixosConfigurations clanInternals; ' - to : " - inherit (clan.config) nixosConfigurations clanInternals; - clan = clan.config; - " - in your flake.nix. - - Reason: - - Improves consistency between flake-parts and non-flake-parts users. - - - It also allows us to use the top level attribute 'clan' to expose - attributes that can be used for cross-clan functionality. - ============================================================ - '' (clan module).config; - - clan = - { - self ? lib.warn "Argument: 'self' must be set" null, # Reference to the current flake - ... - }@m: - let - nixpkgs = self.inputs.nixpkgs or clan-core.inputs.nixpkgs; - nix-darwin = self.inputs.nix-darwin or clan-core.inputs.nix-darwin; - in - lib.evalModules { - class = "clan"; - specialArgs = { - inherit - self - ; - inherit - nixpkgs - nix-darwin - ; - }; - modules = [ - m - clan-core.modules.clan.default - ]; - }; -}