pihole-flake/lib/util.nix
Christopher Bacher c536fb7293 refactor: move support functions to ./lib; change piholeConfiguration option to piholeConfig
The first part of the refactor simplifies the logic in the module and hides functions which are only used to extract container env vars.
Second the option renaming to piholeConfig unifies the naming with the hostConfig option.
2022-12-05 23:23:19 +01:00

45 lines
1.4 KiB
Nix

with builtins; let
collectAttrFragments = predicate: attrs: let
_collectAttrFragments = attrs:
concatMap (key: _collectAttrFragmentsBelowKey key attrs.${key}) (attrNames attrs)
;
_collectAttrFragmentsBelowKey = key: value:
if predicate value then [ [key] ]
else if isAttrs value then
map (fragment: [key] ++ fragment) (_collectAttrFragments value)
else [ ]
;
in _collectAttrFragments attrs
;
accessValueOfFragment = attrs: fragment: let
_accessValueOfFragment = value: fragment:
if fragment == [] then value
else _accessValueOfFragment (value.${head fragment}) (tail fragment)
;
in _accessValueOfFragment attrs fragment
;
toEnvValue = value:
if isBool value then (if value then "true" else "false")
else if isList value then "[${concatStringSep ";" value}]"
else value
;
in {
extractContainerEnvVars = piholeOptionDeclarations: piholeOptionDefinitions: let
_opt = piholeOptionDeclarations;
_cfg = piholeOptionDefinitions;
_envVarFragments = collectAttrFragments (value: isAttrs value && value ? "envVar") _opt.piholeConfig;
in filter
(envVar: envVar.value != null)
(map
(fragment: {
name = getAttr "envVar" (accessValueOfFragment _opt.piholeConfig fragment);
value = toEnvValue (accessValueOfFragment _cfg.piholeConfig fragment);
})
_envVarFragments
)
;
}