From d69a07436699e1b6d0d77bde6ef58d897c4c39a4 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 1 Apr 2025 17:28:52 +0200 Subject: [PATCH] chore(buildClan): make buildClan and flake-parts return identical outputs --- flakeModules/clan.nix | 35 +++++++++++++++++++++++---------- lib/build-clan/default.nix | 40 ++++++++++++++++++++------------------ lib/build-clan/module.nix | 2 +- lib/build-clan/public.nix | 17 ++++++++++++++++ 4 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 lib/build-clan/public.nix diff --git a/flakeModules/clan.nix b/flakeModules/clan.nix index e50192dee..07e84dbf4 100644 --- a/flakeModules/clan.nix +++ b/flakeModules/clan.nix @@ -11,9 +11,24 @@ let inherit (lib) types; buildClanModule = clan-core.clanLib.buildClanModule; + + publicAttrs = import ../lib/build-clan/public.nix; + # Create output options only for listed attributes + outputModule = { + clan = lib.genAttrs publicAttrs.clan ( + name: + config.clan.clanInternals.${name} + or (throw "Output: clanInternals.${name} not found. Check: ${config.file}") + ); + 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}") + ); + }; + }; in { - options.clan = lib.mkOption { default = { }; type = types.submoduleWith { @@ -27,16 +42,16 @@ in }; }; - options.flake = flake-parts-lib.mkSubmoduleOptions { - clan = lib.mkOption { type = types.raw; }; - clanInternals = lib.mkOption { type = types.raw; }; - }; + options.flake = + flake-parts-lib.mkSubmoduleOptions { + clan = lib.mkOption { type = types.raw; }; + } + // outputModule.topLevel.options; config = { - flake.clan = { - inherit (config.clan.clanInternals) templates; - }; - flake.clanInternals = config.clan.clanInternals; - flake.nixosConfigurations = config.clan.nixosConfigurations; + flake = { + clan = outputModule.clan; + } // outputModule.topLevel.config; }; + _file = __curPos.file; } diff --git a/lib/build-clan/default.nix b/lib/build-clan/default.nix index b94bba48e..35a0d7753 100644 --- a/lib/build-clan/default.nix +++ b/lib/build-clan/default.nix @@ -5,14 +5,7 @@ lib, nixpkgs, }: -let - clanResultAttributes = [ - "clanInternals" - "nixosConfigurations" - ]; -in { - inherit clanResultAttributes; flakePartsModule = { imports = [ ./interface.nix @@ -25,6 +18,7 @@ in # Arguments of the first function - clan-core: Self, provided by our flake-parts module + - publicAttrs: { clan :: List Str, topLevel :: List Str } Publicly exported attribute names # Arguments of the second function (aka 'buildClan') - self: Reference to the users flake @@ -34,13 +28,13 @@ in # Returns - A module evaluation containing '.config' and '.options' - - NOTE: - The result might export all kinds of options at the '.config' top level. + Public attributes of buildClan. As specified in publicAttrs. */ buildClanWith = - { clan-core }: + { + clan-core, + publicAttrs ? import ./public.nix, + }: { ## Inputs self ? lib.warn "Argument: 'self' must be set when using 'buildClan'." null, # Reference to the current flake @@ -65,12 +59,20 @@ in inherit specialArgs; }; rest = builtins.removeAttrs attrs [ "specialArgs" ]; + result = eval { + imports = [ + rest + # implementation + ./module.nix + ]; + }; in - eval { - imports = [ - rest - # implementation - ./module.nix - ]; - }; + { + clan = lib.genAttrs publicAttrs.clan ( + name: + result.clanInternals.${name} + or (throw "Output: clanInternals.${name} not found. Check: ${result.file}") + ); + } + // lib.filterAttrs (name: _v: builtins.elem name publicAttrs.topLevel) result; } diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index 29eafe838..9311c53c4 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -208,7 +208,7 @@ in # TODO: unify this interface # We should have only clan.modules. (consistent with clan.templates) inherit (clan-core) clanModules clanLib; - modules = clan-core.modules; + modules = clan-core.clanModules; inherit inventoryFile; inventoryValuesPrios = diff --git a/lib/build-clan/public.nix b/lib/build-clan/public.nix new file mode 100644 index 000000000..493ac1cc0 --- /dev/null +++ b/lib/build-clan/public.nix @@ -0,0 +1,17 @@ +/** + Publicly exported attribute names + These are mapped from 'options.clan.{name}' into 'flake.{name}' + For example "clanInternals" will be exposed as "flake.clan.clanInternals" + This list is used to guarantee equivalent attribute sets for both flake-parts and buildClan users. +*/ +{ + # flake.clan.{name} <- clanInternals.{name} + clan = [ + "templates" + ]; + # flake.{name} <- clan.{name} + topLevel = [ + "clanInternals" + "nixosConfigurations" + ]; +}