refactor: modularize utils

This commit is contained in:
2026-04-14 19:15:35 +02:00
parent 949917a809
commit fce3aa45ec
3 changed files with 45 additions and 35 deletions

View File

@@ -1,41 +1,12 @@
{ lib }: { lib }:
let
hosts = import ./hosts.nix;
secrets = import ./secrets.nix { inherit lib; };
in
{ {
dirNames = dirNames =
path: builtins.attrNames (lib.filterAttrs (_: type: type == "directory") (builtins.readDir path)); path: builtins.attrNames (lib.filterAttrs (_: type: type == "directory") (builtins.readDir path));
hostMeta =
hostDir:
if builtins.pathExists (hostDir + "/meta.nix") then
import (hostDir + "/meta.nix")
else
throw "meta.nix required in ${hostDir}";
mkSopsSecrets =
sopsDir: group: names: extraOpts:
let
file = "${group}.yaml";
in
lib.foldl' lib.mergeAttrs { } (
map (name: {
"${group}/${name}" = {
sopsFile = "${sopsDir}/${file}";
key = name;
}
// extraOpts;
}) names
);
sopsAvailability =
config: osConfig:
let
osSopsAvailable = osConfig != null && osConfig ? sops && osConfig.sops ? secrets;
hmSopsAvailable = config ? sops && config.sops ? secrets;
preferOs = osSopsAvailable;
in
{
available = osSopsAvailable || hmSopsAvailable;
secrets = if preferOs then osConfig.sops.secrets else config.sops.secrets;
templates = if preferOs then osConfig.sops.templates else config.sops.templates;
};
} }
// hosts
// secrets

8
utils/hosts.nix Normal file
View File

@@ -0,0 +1,8 @@
{
hostMeta =
hostDir:
if builtins.pathExists (hostDir + "/meta.nix") then
import (hostDir + "/meta.nix")
else
throw "meta.nix required in ${hostDir}";
}

31
utils/secrets.nix Normal file
View File

@@ -0,0 +1,31 @@
{ lib }:
{
mkSopsSecrets =
sopsDir: group: names: extraOpts:
let
file = "${group}.yaml";
in
lib.foldl' lib.mergeAttrs { } (
map (name: {
"${group}/${name}" = {
sopsFile = "${sopsDir}/${file}";
key = name;
}
// extraOpts;
}) names
);
sopsAvailability =
config: osConfig:
let
osSopsAvailable = osConfig != null && osConfig ? sops && osConfig.sops ? secrets;
hmSopsAvailable = config ? sops && config.sops ? secrets;
preferOs = osSopsAvailable;
in
{
available = osSopsAvailable || hmSopsAvailable;
secrets = if preferOs then osConfig.sops.secrets else config.sops.secrets;
templates = if preferOs then osConfig.sops.templates else config.sops.templates;
};
}