{ lib, ... }:
{
loadHosts = hostsPath:
let
rakeLeaves = dirPath:
# Adapted from digga (https://github.com/divnix/digga/blob/main/src/importers.nix)
# This version does not support nested hosts; your host derivation must either reside in hosts/<hostname>.nix or hosts/<hostname>/default.nix
let
seive = file: type:
(type == "regular" && lib.hasSuffix ".nix" file) || (type == "directory");
collect = file: type: {
name = lib.removeSuffix ".nix" file;
value =
let
path = dirPath + "/${file}";
in
if (type == "regular") || (type == "directory" && builtins.pathExists (path + "/default.nix"))
then path
else throw "Your host must be a file named <hostname>.nix or a directory named <hostname> with a default.nix file in it";
};
files = lib.filterAttrs seive (builtins.readDir dirPath);
in
lib.filterAttrs (n: v: v != {}) (lib.mapAttrs' collect files);
in
{
nixosConfigurations = lib.mapAttrs
(name: value:
import value {
utils = import ./. {
inherit lib;
};
}
) (rakeLeaves hostsPath);
};
}