From 17f09210bca83ab8573da955b6269cb1154cd756 Mon Sep 17 00:00:00 2001 From: Christopher Bacher Date: Mon, 31 Oct 2022 01:13:22 +0100 Subject: [PATCH] modules(pihole-container): add options for configuring Pi-hole's FTLDNS component --- lib/util.nix | 23 +++++++++++++++++++---- modules/pihole-container.factory.nix | 17 +++++++++++++++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/util.nix b/lib/util.nix index 532f8e2..aeca2af 100644 --- a/lib/util.nix +++ b/lib/util.nix @@ -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) + ; } diff --git a/modules/pihole-container.factory.nix b/modules/pihole-container.factory.nix index 0423d03..91ec40a 100644 --- a/modules/pihole-container.factory.nix +++ b/modules/pihole-container.factory.nix @@ -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} '';