Merge pull request 'chore(lib): prepare for refactoring into clanLib' (#3141) from hsjobeki/clan-core:clan-services into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3141
This commit is contained in:
hsjobeki
2025-03-30 15:51:17 +00:00
14 changed files with 58 additions and 44 deletions

View File

@@ -13,8 +13,8 @@
# { clanCore = «derivation JSON»; clanModules = { ${name} = «derivation JSON» }; }
jsonDocs = pkgs.callPackage ./get-module-docs.nix {
inherit (self) clanModules;
evalClanModules = self.lib.evalClanModules;
modulesRolesOptions = self.lib.evalClanModulesWithRoles self.clanModules;
evalClanModules = self.lib.evalClan.evalClanModules;
modulesRolesOptions = self.lib.evalClan.evalClanModulesWithRoles self.clanModules;
};
# Frontmatter for clanModules

View File

@@ -148,7 +148,7 @@ in
clanModules = lib.mkOption { type = lib.types.raw; };
source = lib.mkOption { type = lib.types.raw; };
meta = lib.mkOption { type = lib.types.raw; };
lib = lib.mkOption { type = lib.types.raw; };
clanLib = lib.mkOption { type = lib.types.raw; };
all-machines-json = lib.mkOption { type = lib.types.raw; };
machines = lib.mkOption { type = lib.types.raw; };
machinesFunc = lib.mkOption { type = lib.types.raw; };

View File

@@ -205,7 +205,7 @@ in
inherit lib inventory;
flake = config.self;
};
inherit (clan-core) clanModules;
inherit (clan-core) clanModules clanLib;
inherit inventoryFile;
inventoryValuesPrios =
# Temporary workaround
@@ -217,9 +217,6 @@ in
templates = config.templates;
inventory = config.inventory;
meta = config.inventory.meta;
lib = {
inherit (clan-core.lib) select;
};
source = "${clan-core}";

View File

@@ -1,25 +1,35 @@
{
lib,
clan-core,
self,
nixpkgs,
...
}:
let
# Produces the
# 'clanLib' attribute set
# Wrapped with fix, so we can depend on other clanLib functions without passing the whole flake
lib.fix (clanLib: {
# TODO:
# SSome bad lib functions that depend on something in 'self'.
# We should reduce the dependency on 'self' aka the 'flake' object
# This makes it easier to test
# most of the time passing the whole flake is unnecessary
callLib = file: args: import file { inherit lib clanLib; } // args;
evalClan = import ./eval-clan-modules {
inherit clan-core lib;
inherit lib;
clan-core = self;
pkgs = nixpkgs.legacyPackages.x86_64-linux;
};
in
{
inherit (evalClan) evalClanModules evalClanModulesWithRoles;
buildClan = import ./build-clan { inherit lib nixpkgs clan-core; };
buildClan = import ./build-clan {
inherit lib nixpkgs;
clan-core = self;
};
# ------------------------------------
# Lib functions that don't depend on 'self'
inventory = clanLib.callLib ./inventory { };
modules = clanLib.callLib ./frontmatter { };
facts = import ./facts.nix { inherit lib; };
inventory = import ./inventory { inherit lib clan-core; };
values = import ./values { inherit lib; };
jsonschema = import ./jsonschema { inherit lib; };
modules = import ./frontmatter {
inherit lib;
self = clan-core;
};
select = import ./select.nix;
}
})

View File

@@ -10,7 +10,9 @@ let
pathExists
;
in
{
rec {
# We should remove this.
# It would enforce treating at least 'lib' as a module in a whole
imports = filter pathExists [
./jsonschema/flake-module.nix
./inventory/flake-module.nix
@@ -18,9 +20,10 @@ in
./values/flake-module.nix
./distributed-service/flake-module.nix
];
flake.lib = import ./default.nix {
inherit lib inputs;
flake.clanLib = import ./default.nix {
inherit lib inputs self;
inherit (inputs) nixpkgs;
clan-core = self;
};
# TODO: remove this legacy alias
flake.lib = flake.clanLib;
}

View File

@@ -1,9 +1,9 @@
{ lib, self }:
{ lib, clanLib }:
let
# Trim the .nix extension from a filename
trimExtension = name: builtins.substring 0 (builtins.stringLength name - 4) name;
jsonWithoutHeader = self.lib.jsonschema {
jsonWithoutHeader = clanLib.jsonschema {
includeDefaults = true;
header = { };
};
@@ -13,7 +13,7 @@ let
lib.mapAttrs (
_moduleName: rolesOptions:
lib.mapAttrs (_roleName: options: jsonWithoutHeader.parseOptions options { }) rolesOptions
) (self.lib.evalClanModulesWithRoles modules);
) (clanLib.evalClan.evalClanModulesWithRoles modules);
evalFrontmatter =
{

View File

@@ -1,7 +1,7 @@
{
lib,
config,
clan-core,
clanLib,
...
}:
let
@@ -43,8 +43,7 @@ let
checkService =
modulepath: serviceName:
builtins.elem "inventory"
(clan-core.lib.modules.getFrontmatter modulepath serviceName).features or [ ];
builtins.elem "inventory" (clanLib.modules.getFrontmatter modulepath serviceName).features or [ ];
compileMachine =
{ machineConfig }:
@@ -160,7 +159,7 @@ in
inherit
resolveTags
inventory
clan-core
clanLib
machineName
serviceConfigs
;

View File

@@ -3,7 +3,7 @@
config,
resolveTags,
inventory,
clan-core,
clanLib,
machineName,
serviceConfigs,
...
@@ -14,7 +14,7 @@ in
{
# Roles resolution
# : List String
supportedRoles = clan-core.lib.modules.getRoles inventory.modules serviceName;
supportedRoles = clanLib.modules.getRoles inventory.modules serviceName;
matchedRoles = builtins.attrNames (
lib.filterAttrs (_: ms: builtins.elem machineName ms) config.machinesRoles
);
@@ -56,7 +56,7 @@ in
assertions = lib.concatMapAttrs (
instanceName: resolvedRoles:
clan-core.lib.modules.checkConstraints {
clanLib.modules.checkConstraints {
moduleName = serviceName;
allModules = inventory.modules;
inherit resolvedRoles instanceName;

View File

@@ -1,6 +1,6 @@
# Generate partial NixOS configurations for every machine in the inventory
# This function is responsible for generating the module configuration for every machine in the inventory.
{ lib, clan-core }:
{ lib, clanLib }:
let
/*
Returns a set with NixOS configuration for every machine in the inventory.
@@ -11,7 +11,7 @@ let
{ inventory, directory }:
(lib.evalModules {
specialArgs = {
inherit clan-core;
inherit clanLib;
};
modules = [
./builder

View File

@@ -343,7 +343,12 @@ in
}
);
default = { };
apply = lib.warn "Inventory.instances and related features are still under development. Please use with care.";
apply =
v:
if v == { } then
v
else
lib.warn "Inventory.instances and related features are still under development. Please use with care." v;
};
services = lib.mkOption {
description = ''

View File

@@ -1,5 +1,5 @@
{ lib, clan-core }:
{ lib, clanLib }:
{
inherit (import ./build-inventory { inherit lib clan-core; }) buildInventory;
inherit (import ./build-inventory { inherit lib clanLib; }) buildInventory;
interface = ./build-inventory/interface.nix;
}

View File

@@ -2,7 +2,8 @@
let
inventory = (
import ../build-inventory {
inherit lib clan-core;
inherit lib;
clanLib = clan-core.clanLib;
}
);
inherit (inventory) buildInventory;

View File

@@ -1,10 +1,9 @@
{
lib,
config,
clan-core,
...
}:
{
# Just some random stuff
config.user.user = lib.mapAttrs clan-core.users.root;
options.test = lib.mapAttrs clan-core;
}

View File

@@ -86,7 +86,7 @@ class FlakeCacheEntry:
self.selector = {int(selectors[0])}
selector = int(selectors[0])
elif isinstance(selectors[0], str):
self.selector = {(selectors[0])}
self.selector = {selectors[0]}
selector = selectors[0]
elif isinstance(selectors[0], AllSelector):
self.selector = AllSelector()
@@ -481,7 +481,7 @@ class Flake:
flake = builtins.getFlake("path:{self.store_path}?narHash={self.hash}");
in
flake.inputs.nixpkgs.legacyPackages.{config["system"]}.writeText "clan-flake-select" (
builtins.toJSON [ ({" ".join([f"flake.clanInternals.lib.select ''{attr}'' flake" for attr in selectors])}) ]
builtins.toJSON [ ({" ".join([f"flake.clanInternals.clanLib.select ''{attr}'' flake" for attr in selectors])}) ]
)
"""
if tmp_store := nix_test_store():