Merge pull request 'refactor: buildClan output; output deferred modules instead of nixosConfig' (#3323) from hsjobeki/clan-core:lib-cleanup into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3323
This commit is contained in:
hsjobeki
2025-04-15 14:47:49 +00:00
4 changed files with 112 additions and 131 deletions

View File

@@ -1,67 +0,0 @@
{
system,
name,
pkgs,
extraConfig,
config,
clan-core,
}:
{
_class,
lib,
...
}:
let
inherit (config) directory machines pkgsForSystem;
inherit (config.clanInternals) inventoryClass;
in
{
imports = [
{
imports = builtins.filter builtins.pathExists (
[
"${directory}/machines/${name}/configuration.nix"
]
++ lib.optionals (_class == "nixos") [
"${directory}/machines/${name}/hardware-configuration.nix"
"${directory}/machines/${name}/disko.nix"
]
);
}
(lib.optionalAttrs (_class == "nixos") {
imports = [
clan-core.nixosModules.clanCore
] ++ (inventoryClass.machines.${name}.machineImports or [ ]);
config = {
clan.core.settings = {
inherit (config.inventory.meta) name icon;
inherit directory;
machine = {
inherit name;
};
};
# Inherited from clan wide settings
# TODO: remove these
};
})
extraConfig
(machines.${name} or { })
(
{
networking.hostName = lib.mkDefault name;
# For vars we need to override the system so we run vars
# generators on the machine that runs `clan vars generate`. If a
# users is using the `pkgsForSystem`, we don't set
# nixpkgs.hostPlatform it would conflict with the `nixpkgs.pkgs`
# option.
nixpkgs.hostPlatform = lib.mkIf (system != null && (pkgsForSystem system) != null) (
lib.mkForce system
);
}
// lib.optionalAttrs (pkgs != null) { nixpkgs.pkgs = lib.mkForce pkgs; }
)
];
}

View File

@@ -0,0 +1,38 @@
{
name,
directory,
meta,
}:
{
_class,
lib,
...
}:
{
imports = [
{
imports = builtins.filter builtins.pathExists (
[
"${directory}/machines/${name}/configuration.nix"
]
++ lib.optionals (_class == "nixos") [
"${directory}/machines/${name}/hardware-configuration.nix"
"${directory}/machines/${name}/disko.nix"
]
);
}
(lib.optionalAttrs (_class == "nixos") {
clan.core.settings = {
inherit (meta) name icon;
inherit directory;
machine = {
inherit name;
};
};
})
# TODO: move into nixos modules
({
networking.hostName = lib.mkDefault name;
})
];
}

View File

@@ -0,0 +1,17 @@
{
pkgs,
}:
{
lib,
...
}:
{
imports = [
({
# For vars we need to ensure that the system so we run vars generate on
# is in sync with the pkgs of the system
nixpkgs.hostPlatform = lib.mkForce pkgs.system;
nixpkgs.pkgs = lib.mkForce pkgs;
})
];
}

View File

@@ -11,10 +11,9 @@ let
directory
pkgsForSystem
specialArgs
inventory
;
inherit (config.clanInternals) inventory;
inherit (clan-core.clanLib.inventory) buildInventory;
supportedSystems = [
@@ -50,74 +49,24 @@ let
moduleSystemConstructor = {
# TODO: remove default system once we have a hardware-config mechanism
nixos =
{
system ? null,
name,
pkgs ? null,
extraConfig ? { },
}:
nixpkgs.lib.nixosSystem {
modules =
let
module = lib.modules.importApply ./inner-module.nix {
inherit
system
name
pkgs
extraConfig
config
clan-core
;
};
in
[
module
{
config.clan.core.module = module;
}
];
specialArgs = {
inherit clan-core;
} // specialArgs;
};
darwin =
{
system ? null,
name,
pkgs ? null,
extraConfig ? { },
}:
nix-darwin.lib.darwinSystem {
modules = [
(lib.modules.importApply ./inner-module.nix {
inherit
system
name
pkgs
extraConfig
config
clan-core
;
})
];
specialArgs = {
inherit clan-core;
} // specialArgs;
};
nixos = nixpkgs.lib.nixosSystem;
darwin = nix-darwin.lib.darwinSystem;
};
allMachines = inventoryClass.machines;
allMachines = inventoryClass.machines; # <- inventory.machines <- clan.machines
machineClasses = lib.mapAttrs (
name: _: inventory.machines.${name}.machineClass or "nixos"
) allMachines;
configurations = lib.mapAttrs (
name: _: moduleSystemConstructor.${machineClasses.${name}} { inherit name; }
name: _:
moduleSystemConstructor.${machineClasses.${name}} {
modules = [ (config.outputs.moduleForMachine.${name} or { }) ];
specialArgs = {
inherit clan-core;
} // specialArgs;
}
) allMachines;
nixosConfigurations = lib.filterAttrs (name: _: machineClasses.${name} == "nixos") configurations;
@@ -133,8 +82,15 @@ let
lib.mapAttrs (
name: _:
moduleSystemConstructor.${machineClasses.${name}} {
inherit name system;
pkgs = pkgsFor.${system};
modules = [
(config.outputs.moduleForMachine.${name} or { })
(lib.modules.importApply ./machineModules/overridePkgs.nix {
pkgs = pkgsFor.${system};
})
];
specialArgs = {
inherit clan-core;
} // specialArgs;
}
) allMachines
)
@@ -152,6 +108,43 @@ let
in
{
imports = [
{
options.outputs.moduleForMachine = lib.mkOption {
type = lib.types.attrsOf lib.types.deferredModule;
};
config.outputs.moduleForMachine = lib.mkMerge [
# Create one empty module for each machine such that there is a default for each machine
# See: 'staticModules' in the moduleForMachine option
# This is only necessary because clan.machines doesn't include all machines
# There can other sources: i.e. inventory
(lib.mapAttrs (
name: v:
(
{ _class, ... }:
{
imports = (v.machineImports or [ ]) ++ [
(lib.modules.importApply ./machineModules/forName.nix {
inherit (config.inventory) meta;
inherit
name
directory
;
})
# Import the correct 'core' module
# We assume either:
# - nixosModules (_class = nixos)
# - darwinModules (_class = darwin)
(lib.optionalAttrs (clan-core."${_class}Modules" ? clanCore) clan-core."${_class}Modules".clanCore)
];
}
)
) inventoryClass.machines)
# The user can define some machine config here
# i.e. 'clan.machines.jon = ...'
config.machines
];
}
# Merge the inventory file
{
inventory = _: {