From e322c92b2ea367636445a4c91f3f2fcc5cd045f5 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 10 Jun 2025 16:31:42 +0200 Subject: [PATCH 1/3] Chore(clanInternals): remove unused attributes --- lib/build-clan/interface.nix | 25 ++++----- lib/build-clan/module.nix | 58 +++++++-------------- lib/inventory/build-inventory/interface.nix | 6 +++ 3 files changed, 37 insertions(+), 52 deletions(-) diff --git a/lib/build-clan/interface.nix b/lib/build-clan/interface.nix index 107806707..c63b81af1 100644 --- a/lib/build-clan/interface.nix +++ b/lib/build-clan/interface.nix @@ -198,24 +198,21 @@ in options = { # Those options are interfaced by the CLI # We don't specify the type here, for better performance. - inventory = lib.mkOption { type = lib.types.raw; }; - # all exported clan templates from this clan - templates = lib.mkOption { type = lib.types.raw; }; - # all exported clan modules from this clan - modules = lib.mkOption { type = lib.types.raw; }; - # all inventory module schemas - inventoryFile = lib.mkOption { type = lib.types.raw; }; - # The machine 'imports' generated by the inventory per machine - inventoryClass = lib.mkOption { type = lib.types.raw; }; - evalServiceSchema = lib.mkOption { }; # clan-core's modules clanModules = lib.mkOption { type = lib.types.raw; }; - source = lib.mkOption { type = lib.types.raw; }; - meta = lib.mkOption { type = lib.types.raw; }; + + # The machine 'imports' generated by the inventory per machine + inventoryClass = lib.mkOption { type = lib.types.raw; }; + + # TODO: remove all dependents in python, delete this option + inventory = lib.mkOption { + type = lib.types.raw; + apply = lib.warn "The 'clanInternals.inventory' option is deprecated, use 'clanInternals.inventoryClass' instead."; + }; + secrets = lib.mkOption { type = lib.types.raw; }; - clanLib = lib.mkOption { type = lib.types.raw; }; + machines = lib.mkOption { type = lib.types.raw; }; - all-machines-json = lib.mkOption { type = lib.types.raw; }; }; }; }; diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index 7ccd5c3ed..7df034d34 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -133,24 +133,26 @@ let ) supportedSystems ); - inventoryFile = "${directory}/inventory.json"; - - inventoryLoaded = - if builtins.pathExists inventoryFile then - (builtins.fromJSON (builtins.readFile inventoryFile)) - else - { }; - in { imports = [ - # Merge the inventory file - { - inventory = _: { - _file = inventoryFile; - config = inventoryLoaded; - }; - } + ( + { ... }: + let + file = "${directory}/inventory.json"; + + inventoryLoaded = + if builtins.pathExists file then (builtins.fromJSON (builtins.readFile file)) else { }; + in + { + imports = [ + { + inventory._inventoryFile = file; + } + ]; + inventory = inventoryLoaded; + } + ) { # TODO: Figure out why this causes infinite recursion inventory.machines = lib.optionalAttrs (builtins.pathExists "${directory}/machines") ( @@ -238,38 +240,18 @@ in clanInternals = { inherit inventoryClass; - - # Endpoint that can be called to get a service schema - evalServiceSchema = clan-core.clanLib.evalServiceSchema config.self; + inventory = config.inventory; # TODO: unify this interface # We should have only clan.modules. (consistent with clan.templates) - inherit (clan-core) clanModules clanLib; - modules = config.modules; - - inherit inventoryFile; + inherit (clan-core) clanModules; templates = config.templates; - inventory = config.inventory; - # TODO: Remove this in about a month - # It is only here for backwards compatibility for people with older CLI versions - inventoryValuesPrios = inventoryClass.introspection; - meta = config.inventory.meta; - secrets = config.secrets; - source = "${clan-core}"; + secrets = config.secrets; # machine specifics machines = configsPerSystem; - all-machines-json = - lib.trace "Your clan-cli and the clan-core input have incompatible versions" lib.mapAttrs - ( - system: configs: - nixpkgs.legacyPackages.${system}.writers.writeJSON "machines.json" ( - lib.mapAttrs (_: m: m.config.system.clan.deployment.data) configs - ) - ) - configsPerSystem; }; }; } diff --git a/lib/inventory/build-inventory/interface.nix b/lib/inventory/build-inventory/interface.nix index d693df3df..59b877fdf 100644 --- a/lib/inventory/build-inventory/interface.nix +++ b/lib/inventory/build-inventory/interface.nix @@ -97,6 +97,12 @@ in ./assertions.nix ]; options = { + _inventoryFile = lib.mkOption { + type = types.path; + readOnly = true; + internal = true; + visible = false; + }; _legacyModules = lib.mkOption { internal = true; visible = false; From 78563b0544928a6555985d4f7ba6d1697e642d5e Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 10 Jun 2025 18:51:42 +0200 Subject: [PATCH 2/3] Fix(clan.nix): create public attributes from 'clan', dont expose them from clanInternals ClanInternals is already exposed at the toplevel the API can access anything This also deduplicated 'templates' and 'modules' into 'clan.modules' and 'clan.templates' repsectively Only this one path is its source --- flakeModules/clan.nix | 7 +++---- lib/build-clan/module.nix | 4 +++- lib/build-clan/public.nix | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/flakeModules/clan.nix b/flakeModules/clan.nix index 4c3456998..95892d721 100644 --- a/flakeModules/clan.nix +++ b/flakeModules/clan.nix @@ -13,16 +13,15 @@ let publicAttrs = import ../lib/build-clan/public.nix; # Create output options only for listed attributes + # TODO: Refactor this into an explicit module, so we can have description and other attributes to be listed in flake-parts outputModule = { clan = lib.genAttrs publicAttrs.clan ( - name: - config.clan.clanInternals.${name} - or (throw "Output: clanInternals.${name} not found. Check: ${config.file}") + name: config.clan.${name} or (throw "Output: clan.${name} not found.") ); topLevel = { options = lib.genAttrs publicAttrs.topLevel (_: lib.mkOption { }); config = lib.genAttrs publicAttrs.topLevel ( - name: config.clan.${name} or (throw "Output: clan.${name} not found. See: ${config.file}") + name: config.clan.${name} or (throw "Output: clan.${name} not found.") ); }; }; diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index 7df034d34..a3de32f2d 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -150,7 +150,9 @@ in inventory._inventoryFile = file; } ]; - inventory = inventoryLoaded; + # Weirdly this works only if it is a function + # This seems to be a bug in nixpkgs + inventory = _: lib.setDefaultModuleLocation file inventoryLoaded; } ) { diff --git a/lib/build-clan/public.nix b/lib/build-clan/public.nix index cc302ac49..73e7978e4 100644 --- a/lib/build-clan/public.nix +++ b/lib/build-clan/public.nix @@ -5,7 +5,7 @@ This list is used to guarantee equivalent attribute sets for both flake-parts and buildClan users. */ { - # flake.clan.{name} <- clanInternals.{name} + # flake.clan.{name} <- clan.{name} clan = [ "templates" "modules" From b429aed8f431a7d7c7788398e1ca33be4d0f302a Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 10 Jun 2025 19:00:57 +0200 Subject: [PATCH 3/3] Fix(tests/buildClan): reduce expected attributes --- lib/build-clan/tests.nix | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/lib/build-clan/tests.nix b/lib/build-clan/tests.nix index ee0f14080..211837263 100644 --- a/lib/build-clan/tests.nix +++ b/lib/build-clan/tests.nix @@ -89,7 +89,7 @@ in }; in { - expr = config.clanInternals.meta; + expr = config.clanInternals.inventory.meta; expected = { description = null; icon = null; @@ -108,35 +108,8 @@ in }; in { - expr = result.clanInternals.meta; - expected = { - description = null; - icon = null; - name = "test"; - }; - }; - - test_fn_extensiv_meta = - let - result = buildClan { - self = { - inputs = { }; - }; - directory = ./.; - meta.name = "test"; - meta.description = "test"; - meta.icon = "test"; - inventory.meta.name = "superclan"; - inventory.meta.description = "description"; - inventory.meta.icon = "icon"; - }; - in - { - expr = result.clanInternals.meta; - expectedError = { - type = "ThrownError"; - msg = ""; - }; + expr = lib.isAttrs result.clanInternals; + expected = true; }; test_fn_clan_core =