refactor(nixos): move nixosSystem config to lib

Moves the nixosSystem configuration logic to a library function for reuse.
This commit is contained in:
Mohammad Rafiq 2025-07-06 18:27:28 +08:00
parent c9636b0bfa
commit 90899b5d37
No known key found for this signature in database
2 changed files with 42 additions and 40 deletions

View file

@ -1,44 +1,9 @@
{
config,
lib,
inputs,
...
}:
{ config, ... }:
let
inherit (lib.trivial) pipe;
inherit (lib.attrsets) filterAttrs mapAttrs';
inherit (lib.strings) removePrefix hasPrefix;
inherit (cfg.lib) extractConfigurations;
cfg = config.flake;
prefix = "nixos/";
hosts = cfg.hostSpec.hosts or { };
mkSystem =
name: value:
let
hostName = removePrefix prefix name;
hostConfig = value;
flakeConfig = config;
in
{
name = hostName;
value = lib.nixosSystem {
specialArgs = {
inherit
inputs
hostName
hostConfig
flakeConfig
;
};
modules = [
cfg.profiles.nixos.common
(value.extraCfg or { })
] ++ (value.profiles or [ ]);
};
};
in
{
flake.nixosConfigurations = pipe hosts [
(filterAttrs (name: _: hasPrefix prefix name))
(mapAttrs' mkSystem)
];
flake.nixosConfigurations = extractConfigurations "nixos/" hosts;
}

View file

@ -1,9 +1,46 @@
{ lib, ... }:
{
lib,
config,
inputs,
...
}:
let
inherit (lib.attrsets) concatMapAttrs;
inherit (lib.trivial) pipe;
inherit (lib.strings) removePrefix hasPrefix;
inherit (lib.attrsets) concatMapAttrs mapAttrs' filterAttrs;
mkSystem =
prefix: name: value:
let
hostName = removePrefix prefix name;
hostConfig = value;
flakeConfig = config;
in
{
name = hostName;
value = lib.nixosSystem {
specialArgs = {
inherit
inputs
hostName
hostConfig
flakeConfig
;
};
modules = [
config.flake.profiles.nixos.common
(value.extraCfg or { })
] ++ (value.profiles or [ ]);
};
};
in
{
flake.lib = {
flattenAttrs = attrset: concatMapAttrs (_: v: v) attrset;
extractConfigurations =
prefix: hosts:
pipe hosts [
(filterAttrs (name: _: hasPrefix prefix name))
(mapAttrs' (mkSystem prefix))
];
};
}