From b43476ccee5b863593358e5646ef7929d97209d7 Mon Sep 17 00:00:00 2001 From: Mohammad Rafiq Date: Sun, 6 Jul 2025 04:18:21 +0800 Subject: [PATCH] refactor(nixos): change hostSpec to define top level option --- modules/configurations/nixos.nix | 28 ++++++++++++++++++++++++++++ modules/hostSpec.nix | 3 +++ modules/hostSpec/default.nix | 29 ----------------------------- 3 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 modules/configurations/nixos.nix create mode 100644 modules/hostSpec.nix delete mode 100644 modules/hostSpec/default.nix diff --git a/modules/configurations/nixos.nix b/modules/configurations/nixos.nix new file mode 100644 index 0000000..03f5539 --- /dev/null +++ b/modules/configurations/nixos.nix @@ -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) + ]; +} diff --git a/modules/hostSpec.nix b/modules/hostSpec.nix new file mode 100644 index 0000000..1b914f3 --- /dev/null +++ b/modules/hostSpec.nix @@ -0,0 +1,3 @@ +{ + flake.hostSpec.hosts = { }; +} diff --git a/modules/hostSpec/default.nix b/modules/hostSpec/default.nix deleted file mode 100644 index 5bb06c6..0000000 --- a/modules/hostSpec/default.nix +++ /dev/null @@ -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; - }; -}