templates/new-clan: auto load all machines from ./machines
All checks were successful
build / test (pull_request) Successful in 1m8s

This commit is contained in:
DavHau 2023-08-26 23:40:29 +02:00
parent c018301d1c
commit b3f08712c5

View File

@ -1,9 +1,52 @@
# AUTOMATICALLY GENERATED by clan
{ ... }: {
# TODO: move this code to a flakeModule in clan-core and import it via flake input
{ self, lib, inputs, ... }:
let
evalConfig = import (self.inputs.nixpkgs + /lib/eval-config.nix);
evalMachine = machineModule: evalConfig {
modules = [ machineModule ];
};
machinesDirs =
if builtins.pathExists ./machines
then builtins.readDir ./machines
else { };
machineSettings = machineName:
if builtins.pathExists ./machines/${machineName}/settings.json
then (builtins.fromJSON (builtins.readFile ./machines/${machineName}/settings.json))
else { };
# load all clan modules by default
machineModule = machineName: {
imports =
(lib.attrValues inputs.clan.clanModules)
++ [ (machineSettings machineName) ];
};
# autoamtically generate machine modules for all directories under ./machines
machineModulesGenerated =
lib.flip lib.mapAttrs' machinesDirs (name: _: {
name = "machine-${name}";
value = machineModule name;
});
# re-read the machine modules from the flake outputs, to allow amchines being
# defined by custom mechanisms
machineModules =
lib.filterAttrs (name: _: lib.hasPrefix "machine-" name) self.nixosModules;
in
{
imports =
let
relPaths = builtins.fromJSON (builtins.readFile ./imports.json);
paths = map (path: ./. + path) relPaths;
in
paths;
flake.nixosModules = machineModulesGenerated;
# generate nixosConfigurations for all machines under self.nixosModules.machines-{machine_name}
flake.nixosConfigurations =
lib.mapAttrs (_: mod: evalMachine mod) machineModules;
}