diff --git a/nixos/kubeadm/README.md b/nixos/kubeadm/README.md index 4c3c391..38f5303 100644 --- a/nixos/kubeadm/README.md +++ b/nixos/kubeadm/README.md @@ -12,6 +12,7 @@ This folder defines role-based NixOS configs for a kubeadm cluster. - Shared Kubernetes/node prerequisites in `modules/k8s-common.nix` - Shared cluster defaults in `modules/k8s-cluster-settings.nix` - Role-specific settings for control planes and workers +- Generated per-node host configs from `flake.nix` (no duplicated host files) - Bootstrap helper commands: - `th-kubeadm-init` - `th-kubeadm-join-control-plane` @@ -20,7 +21,7 @@ This folder defines role-based NixOS configs for a kubeadm cluster. ## Hardware config files -Each host file optionally imports `hosts/hardware/.nix` if present. +The flake automatically imports `hosts/hardware/.nix` if present. Copy each node's generated hardware config into this folder: ```bash diff --git a/nixos/kubeadm/flake.nix b/nixos/kubeadm/flake.nix index d3dacef..eca29ac 100644 --- a/nixos/kubeadm/flake.nix +++ b/nixos/kubeadm/flake.nix @@ -8,19 +8,31 @@ outputs = { nixpkgs, ... }: let system = "x86_64-linux"; - mkHost = hostModules: + lib = nixpkgs.lib; + + mkNode = { name, role }: + let + roleModule = if role == "control-plane" then ./modules/k8s-control-plane.nix else ./modules/k8s-worker.nix; + hardwarePath = ./hosts/hardware + "/${name}.nix"; + in nixpkgs.lib.nixosSystem { inherit system; - modules = hostModules; + modules = [ + ./modules/k8s-cluster-settings.nix + ./modules/k8s-common.nix + roleModule + ({ lib, ... }: { + imports = lib.optional (builtins.pathExists hardwarePath) hardwarePath; + networking.hostName = name; + system.stateVersion = "25.05"; + }) + ]; }; in { - nixosConfigurations = { - cp-1 = mkHost [ ./hosts/cp-1.nix ]; - cp-2 = mkHost [ ./hosts/cp-2.nix ]; - cp-3 = mkHost [ ./hosts/cp-3.nix ]; - wk-1 = mkHost [ ./hosts/wk-1.nix ]; - wk-2 = mkHost [ ./hosts/wk-2.nix ]; - wk-3 = mkHost [ ./hosts/wk-3.nix ]; - }; + nixosConfigurations = lib.genAttrs [ "cp-1" "cp-2" "cp-3" "wk-1" "wk-2" "wk-3" ] (name: + mkNode { + inherit name; + role = if lib.hasPrefix "cp-" name then "control-plane" else "worker"; + }); }; } diff --git a/nixos/kubeadm/hosts/cp-1.nix b/nixos/kubeadm/hosts/cp-1.nix deleted file mode 100644 index 3de50fc..0000000 --- a/nixos/kubeadm/hosts/cp-1.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ lib, ... }: - -{ - imports = - [ - ../modules/k8s-cluster-settings.nix - ../modules/k8s-common.nix - ../modules/k8s-control-plane.nix - ] - ++ lib.optional (builtins.pathExists ./hardware/cp-1.nix) ./hardware/cp-1.nix; - - networking.hostName = "cp-1"; - - system.stateVersion = "25.05"; -} diff --git a/nixos/kubeadm/hosts/cp-2.nix b/nixos/kubeadm/hosts/cp-2.nix deleted file mode 100644 index 03b6fb7..0000000 --- a/nixos/kubeadm/hosts/cp-2.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ lib, ... }: - -{ - imports = - [ - ../modules/k8s-cluster-settings.nix - ../modules/k8s-common.nix - ../modules/k8s-control-plane.nix - ] - ++ lib.optional (builtins.pathExists ./hardware/cp-2.nix) ./hardware/cp-2.nix; - - networking.hostName = "cp-2"; - - system.stateVersion = "25.05"; -} diff --git a/nixos/kubeadm/hosts/cp-3.nix b/nixos/kubeadm/hosts/cp-3.nix deleted file mode 100644 index 40acfd4..0000000 --- a/nixos/kubeadm/hosts/cp-3.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ lib, ... }: - -{ - imports = - [ - ../modules/k8s-cluster-settings.nix - ../modules/k8s-common.nix - ../modules/k8s-control-plane.nix - ] - ++ lib.optional (builtins.pathExists ./hardware/cp-3.nix) ./hardware/cp-3.nix; - - networking.hostName = "cp-3"; - - system.stateVersion = "25.05"; -} diff --git a/nixos/kubeadm/hosts/wk-1.nix b/nixos/kubeadm/hosts/wk-1.nix deleted file mode 100644 index d102e4c..0000000 --- a/nixos/kubeadm/hosts/wk-1.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ lib, ... }: - -{ - imports = - [ - ../modules/k8s-cluster-settings.nix - ../modules/k8s-common.nix - ../modules/k8s-worker.nix - ] - ++ lib.optional (builtins.pathExists ./hardware/wk-1.nix) ./hardware/wk-1.nix; - - networking.hostName = "wk-1"; - - system.stateVersion = "25.05"; -} diff --git a/nixos/kubeadm/hosts/wk-2.nix b/nixos/kubeadm/hosts/wk-2.nix deleted file mode 100644 index 78954db..0000000 --- a/nixos/kubeadm/hosts/wk-2.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ lib, ... }: - -{ - imports = - [ - ../modules/k8s-cluster-settings.nix - ../modules/k8s-common.nix - ../modules/k8s-worker.nix - ] - ++ lib.optional (builtins.pathExists ./hardware/wk-2.nix) ./hardware/wk-2.nix; - - networking.hostName = "wk-2"; - - system.stateVersion = "25.05"; -} diff --git a/nixos/kubeadm/hosts/wk-3.nix b/nixos/kubeadm/hosts/wk-3.nix deleted file mode 100644 index 1d21f36..0000000 --- a/nixos/kubeadm/hosts/wk-3.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ lib, ... }: - -{ - imports = - [ - ../modules/k8s-cluster-settings.nix - ../modules/k8s-common.nix - ../modules/k8s-worker.nix - ] - ++ lib.optional (builtins.pathExists ./hardware/wk-3.nix) ./hardware/wk-3.nix; - - networking.hostName = "wk-3"; - - system.stateVersion = "25.05"; -}