refactor: simplify buildClan remove any filtering

This commit is contained in:
Johannes Kirschbauer
2025-06-26 13:56:14 +02:00
parent 6c460db016
commit 1568950410
3 changed files with 28 additions and 82 deletions

View File

@@ -6,7 +6,8 @@
clanLib, clanLib,
... ...
}: }:
{ rec {
# TODO: rename to clanModule
flakePartsModule = { flakePartsModule = {
_module.args = { _module.args = {
inherit clanLib; inherit clanLib;
@@ -34,53 +35,35 @@
Public attributes of buildClan. As specified in publicAttrs. Public attributes of buildClan. As specified in publicAttrs.
*/ */
buildClanWith = buildClanWith =
{ {
clan-core, clan-core,
# TODO: Below should be module options such that the user can override them? # TODO: Below should be module options such that the user can override them?
nixpkgs, nixpkgs,
publicAttrs ? import ./public.nix,
nix-darwin ? null, nix-darwin ? null,
}: }:
{ {
## Inputs ## Inputs
self ? lib.warn "Argument: 'self' must be set when using 'buildClan'." null, # Reference to the current flake self ? lib.warn "Argument: 'self' must be set when using 'buildClan'." null, # Reference to the current flake
# allows to include machine-specific modules i.e. machines.${name} = { ... }
# A map from arch to pkgs, if specified this nixpkgs will be only imported once for each system.
# This improves performance, but all nipxkgs.* options will be ignored.
# deadnix: skip
inventory ? { },
## Special inputs (not passed to the module system as config)
specialArgs ? { }, # Extra arguments to pass to nixosSystem i.e. useful to make self available specialArgs ? { }, # Extra arguments to pass to nixosSystem i.e. useful to make self available
##
... ...
}@attrs: }@m:
let let
eval = import ./function-adapter.nix { result = lib.evalModules {
inherit specialArgs = {
lib inherit (clan-core) clanLib;
nixpkgs inherit
nix-darwin self
clan-core clan-core
self nixpkgs
; nix-darwin
inherit specialArgs; ;
}; };
rest = builtins.removeAttrs attrs [ "specialArgs" ]; modules = [
result = eval { # buildClan arguments are equivalent to specifying a module
imports = [ m
rest flakePartsModule
# implementation
./clan/module.nix
]; ];
}; };
in in
{ result.config;
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;
} }

View File

@@ -1,28 +0,0 @@
{
lib,
nixpkgs,
nix-darwin ? null,
clan-core,
self,
specialArgs ? { },
}:
# Returns a function that takes self, which should point to the directory of the flake
module:
(lib.evalModules {
specialArgs = {
inherit (clan-core) clanLib;
inherit
self
clan-core
nixpkgs
nix-darwin
;
};
modules = [
./clan/default.nix
module
{
inherit specialArgs;
}
];
}).config

View File

@@ -1,58 +1,49 @@
{ {
lib, lib,
nixpkgs,
clan-core, clan-core,
buildClan,
... ...
}: }:
let let
evalClan = import ./function-adapter.nix {
inherit lib nixpkgs clan-core;
self = ./.;
};
# Shallowly force all attribute values to be evaluated. # Shallowly force all attribute values to be evaluated.
shallowForceAllAttributes = lib.foldlAttrs ( shallowForceAllAttributes = lib.foldlAttrs (
_acc: _name: value: _acc: _name: value:
lib.seq value true lib.seq value true
) true; ) true;
inherit (clan-core.clanLib) buildClan;
in in
####### #######
{ {
test_missing_self = test_missing_self =
let let
config = buildClan { clan = buildClan {
meta.name = "test"; meta.name = "test";
imports = [ ./clan/module.nix ]; directory = ./.;
}; };
in in
{ {
expr = shallowForceAllAttributes config; inherit clan;
expectedError = { expr = shallowForceAllAttributes clan;
type = "ThrownError"; expected = true;
msg = "A definition for option `directory' is not of type `absolute path*";
};
}; };
test_only_required = test_only_required =
let let
config = evalClan { clan = buildClan {
self = { self = {
inputs = { }; inputs = { };
outPath = ./.; outPath = ./.;
}; };
meta.name = "test"; meta.name = "test";
imports = [ ./clan/module.nix ];
}; };
in in
{ {
expr = shallowForceAllAttributes config; expr = shallowForceAllAttributes clan;
expected = true; expected = true;
}; };
test_all_simple = test_all_simple =
let let
config = evalClan { config = buildClan {
self = { self = {
inputs = { }; inputs = { };
}; };
@@ -70,7 +61,7 @@ in
test_outputs_clanInternals = test_outputs_clanInternals =
let let
config = evalClan { config = buildClan {
self = { self = {
inputs = { }; inputs = { };
}; };