Merge pull request 'refactor: simplify buildClan remove any filtering' (#4105) from hsjobeki/buildClan into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4105
This commit is contained in:
hsjobeki
2025-06-26 12:04:37 +00:00
3 changed files with 28 additions and 82 deletions

View File

@@ -6,7 +6,8 @@
clanLib,
...
}:
{
rec {
# TODO: rename to clanModule
flakePartsModule = {
_module.args = {
inherit clanLib;
@@ -34,53 +35,35 @@
Public attributes of buildClan. As specified in publicAttrs.
*/
buildClanWith =
{
clan-core,
# TODO: Below should be module options such that the user can override them?
nixpkgs,
publicAttrs ? import ./public.nix,
nix-darwin ? null,
}:
{
## Inputs
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
##
...
}@attrs:
}@m:
let
eval = import ./function-adapter.nix {
result = lib.evalModules {
specialArgs = {
inherit (clan-core) clanLib;
inherit
lib
self
clan-core
nixpkgs
nix-darwin
clan-core
self
;
inherit specialArgs;
};
rest = builtins.removeAttrs attrs [ "specialArgs" ];
result = eval {
imports = [
rest
# implementation
./clan/module.nix
modules = [
# buildClan arguments are equivalent to specifying a module
m
flakePartsModule
];
};
in
{
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;
result.config;
}

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