From b3f08712c5f042291769a8b498b27862c8d2049a Mon Sep 17 00:00:00 2001 From: DavHau Date: Sat, 26 Aug 2023 23:40:29 +0200 Subject: [PATCH] templates/new-clan: auto load all machines from ./machines --- templates/new-clan/clan-flake-module.nix | 45 +++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/templates/new-clan/clan-flake-module.nix b/templates/new-clan/clan-flake-module.nix index cffd20ac..456a8022 100644 --- a/templates/new-clan/clan-flake-module.nix +++ b/templates/new-clan/clan-flake-module.nix @@ -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; }