modules(pihole-container): add options for configuring Pi-hole's FTLDNS component

This commit is contained in:
Christopher Bacher 2022-10-31 01:13:22 +01:00
parent d72b14f17b
commit 17f09210bc
2 changed files with 34 additions and 6 deletions

View file

@ -1,12 +1,14 @@
with builtins; let
collectAttrFragments = predicate: attrs: let
collectAttrFragments = successPredicate: stopPredicate: attrs: let
_collectAttrFragments = attrs:
concatMap (key: _collectAttrFragmentsBelowKey key attrs.${key}) (attrNames attrs)
;
_collectAttrFragmentsBelowKey = key: value:
if predicate value then [ [key] ]
if successPredicate value then [ [key] ]
else if stopPredicate value then [ ]
else if isAttrs value then
map (fragment: [key] ++ fragment) (_collectAttrFragments value)
map (fragment: if length fragment > 0 then [key] ++ fragment else [ ]) (_collectAttrFragments value)
else [ ]
;
in _collectAttrFragments attrs
@ -31,7 +33,10 @@ in {
_opt = piholeOptionDeclarations;
_cfg = piholeOptionDefinitions;
_envVarFragments = collectAttrFragments (value: isAttrs value && value ? "envVar") _opt.piholeConfig;
_envVarFragments = collectAttrFragments
(value: isAttrs value && value ? "envVar")
(value: isAttrs value && value._type or "" == "option")
_opt.piholeConfig;
in filter
(envVar: envVar.value != null)
(map
@ -42,4 +47,14 @@ in {
_envVarFragments
)
;
extractContainerFTLEnvVars = piholeOptionDefinitions: let
_ftl = piholeOptionDefinitions.piholeConfig.ftl;
in map
(name: {
name = "FTL_${name}";
value = _ftl.${name};
})
(attrNames _ftl)
;
}

View file

@ -1,5 +1,5 @@
{ piholeFlake, lingerFlake }: { config, pkgs, lib, ... }: with lib; with builtins; let
inherit (import ../lib/util.nix) extractContainerEnvVars;
inherit (import ../lib/util.nix) extractContainerEnvVars extractContainerFTLEnvVars;
inherit (import ../lib/options.nix lib) mkContainerEnvOption mkHostPortsOption;
cfg = config.services.pihole;
@ -207,6 +207,18 @@ in rec {
};
};
ftl = mkOption {
type = with types; attrsOf str;
description = ''
Set any additional FTL option under this key.
You can find the different options in the pihole docs: https://docs.pi-hole.net/ftldns/configfile
The names should be exactly like in the pihole docs.
'';
example = { LOCAL_IPV4 = "192.168.0.100"; };
default = {};
};
dhcp = {
enable = mkContainerEnvOption {
type = types.bool;
@ -328,6 +340,7 @@ in rec {
serviceConfig = let
containerEnvVars = extractContainerEnvVars options.services.pihole cfg;
containerFTLEnvVars = extractContainerFTLEnvVars cfg;
in {
ExecStartPre = mkIf cfg.hostConfig.persistVolumes [
"${pkgs.coreutils}/bin/mkdir -p ${cfg.hostConfig.volumesPath}/etc-pihole"
@ -351,7 +364,7 @@ in rec {
-p ${toString cfg.hostConfig.web.hostInternalPort}:80/tcp \
${
concatStringsSep " \\\n"
(map (envVar: " -e '${envVar.name}=${toString envVar.value}'") containerEnvVars)
(map (envVar: " -e '${envVar.name}=${toString envVar.value}'") (containerEnvVars ++ containerFTLEnvVars))
} \
docker-archive:${piholeFlake.packages.${pkgs.system}.piholeImage}
'';