forked from clan/clan-core
Merge pull request 'templates/new-clan: auto load all machines from ./machines' (#192) from DavHau-main into main
This commit is contained in:
commit
df628ffd96
@ -1,9 +1,52 @@
|
|||||||
# AUTOMATICALLY GENERATED by clan
|
# 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 =
|
imports =
|
||||||
let
|
let
|
||||||
relPaths = builtins.fromJSON (builtins.readFile ./imports.json);
|
relPaths = builtins.fromJSON (builtins.readFile ./imports.json);
|
||||||
paths = map (path: ./. + path) relPaths;
|
paths = map (path: ./. + path) relPaths;
|
||||||
in
|
in
|
||||||
paths;
|
paths;
|
||||||
|
flake.nixosModules = machineModulesGenerated;
|
||||||
|
# generate nixosConfigurations for all machines under self.nixosModules.machines-{machine_name}
|
||||||
|
flake.nixosConfigurations =
|
||||||
|
lib.mapAttrs (_: mod: evalMachine mod) machineModules;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user