modularize hosts function

This commit is contained in:
Sean Buckley 2021-06-05 02:22:38 -04:00
parent ed1020974a
commit 361fe957c6
11 changed files with 48 additions and 40 deletions

View file

@ -2,10 +2,12 @@
inputs.nixpkgs.url = "nixpkgs/nixos-21.05"; inputs.nixpkgs.url = "nixpkgs/nixos-21.05";
inputs.unstable.url = "nixpkgs/nixos-unstable"; inputs.unstable.url = "nixpkgs/nixos-unstable";
outputs = { self, unstable, nixpkgs }: { outputs = { self, ... }@inputs:
nixosModule = { ... }: { {
imports = [ ./. ]; nixosModule = { ... }: { imports = [ ./. ]; };
};
nixosConfigurations = import ./hosts { modules = [ ./. ]; inherit unstable nixpkgs; }; nixosConfigurations = self.lib.getHosts inputs ./hosts;
lib.getHosts = import lib/hosts.nix;
}; };
} }

View file

@ -1,35 +0,0 @@
{ modules, nixpkgs, unstable }:
let
hostMetadata =
let
fs = builtins.readDir ./.;
inherit (builtins) concatMap attrNames;
hostNames = concatMap (x: if fs.${x} == "directory" then [ x ] else [ ]) (attrNames fs);
in
builtins.listToAttrs (map
(hn: { name = hn; value = import (./. + "/${hn}"); })
hostNames);
hardwareModule = hardware: (
{
qemu = (x: { imports = [ "${x.modulesPath}/profiles/qemu-guest.nix" ]; });
physical = (x: { imports = [ "${x.modulesPath}/installer/scan/not-detected.nix" ]; });
}
).${hardware};
in
builtins.mapAttrs
(n: v:
let pkgs = { inherit nixpkgs unstable; }.${v.pkgs};
in
pkgs.lib.nixosSystem {
inherit (v) system;
modules = modules ++ [
(./. + "/${n}/configuration.nix")
(hardwareModule v.hardware)
({ ... }: { networking.hostName = n; })
];
}
)
hostMetadata

View file

@ -3,4 +3,6 @@
services.qemuGuest.enable = true; services.qemuGuest.enable = true;
sconfig.profile = "server"; sconfig.profile = "server";
services.getty.autologinUser = "root"; services.getty.autologinUser = "root";
fileSystems."/" = { device = "/dev/vda1"; fsType = "ext4"; };
boot.loader.grub.device = "nodev";
} }

View file

@ -2,4 +2,5 @@
pkgs = "unstable"; pkgs = "unstable";
system = "x86_64-linux"; system = "x86_64-linux";
hardware = "qemu"; hardware = "qemu";
module = ./configuration.nix;
} }

View file

@ -4,4 +4,6 @@
sconfig.profile = "desktop"; sconfig.profile = "desktop";
sconfig.gnome = true; sconfig.gnome = true;
services.getty.autologinUser = "root"; services.getty.autologinUser = "root";
fileSystems."/" = { device = "/dev/vda1"; fsType = "ext4"; };
boot.loader.grub.device = "nodev";
} }

View file

@ -2,4 +2,5 @@
pkgs = "unstable"; pkgs = "unstable";
system = "x86_64-linux"; system = "x86_64-linux";
hardware = "qemu"; hardware = "qemu";
module = ./configuration.nix;
} }

View file

@ -2,4 +2,5 @@
pkgs = "nixpkgs"; pkgs = "nixpkgs";
system = "x86_64-linux"; system = "x86_64-linux";
hardware = "physical"; hardware = "physical";
module = ./configuration.nix;
} }

View file

@ -2,4 +2,5 @@
pkgs = "nixpkgs"; pkgs = "nixpkgs";
system = "x86_64-linux"; system = "x86_64-linux";
hardware = "physical"; hardware = "physical";
module = ./configuration.nix;
} }

View file

@ -2,4 +2,5 @@
pkgs = "nixpkgs"; pkgs = "nixpkgs";
system = "x86_64-linux"; system = "x86_64-linux";
hardware = "physical"; hardware = "physical";
module = ./configuration.nix;
} }

View file

@ -2,4 +2,5 @@
pkgs = "nixpkgs"; pkgs = "nixpkgs";
system = "x86_64-linux"; system = "x86_64-linux";
hardware = "physical"; hardware = "physical";
module = ./configuration.nix;
} }

31
lib/hosts.nix Normal file
View file

@ -0,0 +1,31 @@
callerInputs: hostsPath:
let
hostMetadata = builtins.mapAttrs
(name: _: import (hostsPath + "/${name}"))
(builtins.readDir hostsPath);
hardwareModules =
{
physical = (x: { imports = [ "${x.modulesPath}/installer/scan/not-detected.nix" ]; });
vmware = (x: { virtualisation.vmware.guest.enable = true; });
qemu = (x: {
services.qemuGuest.enable = true;
imports = [ "${x.modulesPath}/profiles/qemu-guest.nix" ];
});
};
getHostConfig = hostName: hostMeta:
callerInputs.${hostMeta.pkgs}.lib.nixosSystem
{
inherit (hostMeta) system;
modules = [
(callerInputs.self.nixosModule)
(hostMeta.module)
(hardwareModules.${hostMeta.hardware})
(_: { networking.hostName = hostName; })
];
};
in
builtins.mapAttrs getHostConfig hostMetadata