refactor(nixos): change hostSpec to define top level option

This commit is contained in:
Mohammad Rafiq 2025-07-06 04:18:21 +08:00
parent 6aa5096172
commit b43476ccee
No known key found for this signature in database
3 changed files with 31 additions and 29 deletions

View file

@ -0,0 +1,28 @@
{ config, lib, ... }:
let
inherit (lib.trivial) pipe;
inherit (lib.attrsets) filterAttrs mapAttrs';
inherit (lib.strings) removePrefix hasPrefix;
prefix = "nixos/";
hosts = config.flake.hostSpec.hosts or { };
mkSystem =
name: value:
let
hostName = removePrefix prefix name;
in
{
name = hostName;
value = lib.nixosSystem {
modules = [
value
{ networking = { inherit hostName; }; }
];
};
};
in
{
flake.nixosConfigurations = pipe hosts [
(filterAttrs (name: _: hasPrefix prefix name))
(mapAttrs' mkSystem)
];
}

3
modules/hostSpec.nix Normal file
View file

@ -0,0 +1,3 @@
{
flake.hostSpec.hosts = { };
}

View file

@ -1,29 +0,0 @@
{
lib,
config,
inputs,
...
}:
let
inherit (builtins) mapAttrs;
inherit (lib) mkOption;
inherit (lib.types) lazyAttrsOf submodule;
cfg = config.hostSpec;
hostOptions = submodule { };
mkSystem =
hostName: _hostConfig:
inputs.nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [ { networking = { inherit hostName; }; } ];
};
in
{
options.hostSpec.hosts = mkOption {
type = lazyAttrsOf hostOptions;
default = { };
};
config = {
flake.nixosConfigurations = mapAttrs mkSystem cfg.hosts;
};
}