Files
nixconfig/generator.nix
T
2026-06-20 13:07:06 +02:00

91 lines
3.0 KiB
Nix
Executable File

{ inputs, mkPkgs, supportedSystems, ... }:
{
generate = { host }:
let
syscfg = import ./systems/${host}/cfg.nix;
nameValuePair = name: value: { inherit name value; };
systemType = syscfg.syscfg.type or "nixos";
defaultSystem =
{
nixos = "x86_64-linux";
macos = "x86_64-darwin";
home = "x86_64-linux";
}
.${systemType} or (throw "Unsupported system type: ${systemType}");
requestedSystem = syscfg.syscfg.system or defaultSystem;
system =
if builtins.elem requestedSystem supportedSystems then
requestedSystem
else
throw "Unsupported system '${requestedSystem}' for host '${host}'";
users = syscfg.syscfg.users or [ ];
mkLinuxHomeImports =
userConfig:
[
./modules/shared/syscfg
./modules/shared/colors
./modules/home
syscfg
{ usercfg = userConfig; }
inputs.nix-colors.homeManagerModule
inputs.sops-nix.homeManagerModules.sops
];
mkDarwinHomeImports = [ inputs.nix-colors.homeManagerModule inputs.sops-nix.homeManagerModules.sops ];
mkHomeUsers = importsForUser:
builtins.listToAttrs (
map (userConfig: nameValuePair userConfig.username { imports = importsForUser userConfig; }) users
);
mkHomeManager = importsForUser: {
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = { inherit inputs; };
home-manager.users = mkHomeUsers importsForUser;
};
sharedModules = [
./modules/shared/syscfg
./modules/shared/sops
syscfg
./systems/${host}
];
defaultUser =
builtins.head (
builtins.filter (user: user.username == syscfg.syscfg.defaultUser) users
++ [ { username = syscfg.syscfg.defaultUser; } ]
);
in
(
{
"nixos" = inputs.nixpkgs.lib.nixosSystem {
inherit system;
specialArgs = { inherit inputs; };
modules = sharedModules ++ [
./modules/nixos
inputs.sops-nix.nixosModules.sops
inputs.home-manager.nixosModules.home-manager
inputs.nixos-wsl.nixosModules.wsl
inputs.vscode-server.nixosModules.default
(mkHomeManager mkLinuxHomeImports)
];
};
"macos" = inputs.darwin.lib.darwinSystem {
inherit system;
modules = sharedModules ++ [
inputs.sops-nix.darwinModules.sops
inputs.home-manager.darwinModules.home-manager
(mkHomeManager (_: mkDarwinHomeImports))
];
};
"home" = inputs.home-manager.lib.homeManagerConfiguration {
pkgs = mkPkgs system;
extraSpecialArgs = { inherit inputs; };
modules = mkLinuxHomeImports defaultUser;
};
_ = throw "Unsupported system";
}
.${systemType}
);
}