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, ... }:
config,
lib,
inputs,
...
}:
let let
inherit (lib.trivial) pipe; inherit (cfg.lib) extractConfigurations;
inherit (lib.attrsets) filterAttrs mapAttrs';
inherit (lib.strings) removePrefix hasPrefix;
cfg = config.flake; cfg = config.flake;
prefix = "nixos/";
hosts = cfg.hostSpec.hosts or { }; 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 in
{ {
flake.nixosConfigurations = pipe hosts [ flake.nixosConfigurations = extractConfigurations "nixos/" hosts;
(filterAttrs (name: _: hasPrefix prefix name))
(mapAttrs' mkSystem)
];
} }

View file

@ -1,9 +1,46 @@
{ lib, ... }: {
lib,
config,
inputs,
...
}:
let 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 in
{ {
flake.lib = { flake.lib = {
flattenAttrs = attrset: concatMapAttrs (_: v: v) attrset; flattenAttrs = attrset: concatMapAttrs (_: v: v) attrset;
extractConfigurations =
prefix: hosts:
pipe hosts [
(filterAttrs (name: _: hasPrefix prefix name))
(mapAttrs' (mkSystem prefix))
];
}; };
} }