refactor: modularize utils
This commit is contained in:
@@ -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
8
utils/hosts.nix
Normal 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
31
utils/secrets.nix
Normal 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;
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user