From 41d400120d085d024a240c057729c16f20095830 Mon Sep 17 00:00:00 2001 From: Christopher Bacher Date: Sat, 15 Oct 2022 21:06:57 +0200 Subject: [PATCH] modules(pihole-container): extract helper function to lib & restructure lets --- lib/util.nix | 2 ++ modules/pihole-container.factory.nix | 36 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/util.nix b/lib/util.nix index ee4b79c..4b751fa 100644 --- a/lib/util.nix +++ b/lib/util.nix @@ -19,4 +19,6 @@ ; in _accessValueOfFragment attrs fragment ; + + toEnvValue = value: if builtins.isBool value then (if value then "true" else "false") else value; } diff --git a/modules/pihole-container.factory.nix b/modules/pihole-container.factory.nix index 74562e1..28c234f 100644 --- a/modules/pihole-container.factory.nix +++ b/modules/pihole-container.factory.nix @@ -1,5 +1,5 @@ { piholeFlake, util }: { config, pkgs, lib, ... }: with lib; with builtins; let - inherit (util) collectAttrFragments accessValueOfFragment; + inherit (util) collectAttrFragments accessValueOfFragment toEnvValue; cfg = config.services.pihole; systemTimeZone = config.time.timeZone; @@ -280,28 +280,27 @@ in rec { after = [ "network-online.target" ]; serviceConfig = let + optPihole = options.services.pihole; + + containerEnvVars = let + envVarFragments = collectAttrFragments (value: isAttrs value && value ? "envVar") opt.piholeConfiguration; + in filter + (envVar: envVar.value != null) + (map + (fragment: { + name = getAttr "envVar" (accessValueOfFragment opt.piholeConfiguration fragment); + value = toEnvValue (accessValueOfFragment cfg.piholeConfiguration fragment); + }) + envVarFragments + ) + ; in { ExecStartPre = mkIf cfg.hostConfig.persistVolumes [ "${pkgs.coreutils}/bin/mkdir -p ${cfg.hostConfig.volumesPath}/etc-pihole" "${pkgs.coreutils}/bin/mkdir -p ${cfg.hostConfig.volumesPath}/etc-dnsmasq.d" ]; - ExecStart = let - containerEnvVars = let - envVarFragments = collectAttrFragments (value: isAttrs value && value ? "envVar") options.services.pihole.piholeConfiguration; - in filter - (envVar: envVar.value != null) - (map - (fragment: { - name = getAttr "envVar" (accessValueOfFragment options.services.pihole.piholeConfiguration fragment); - value = let - _value = accessValueOfFragment cfg.piholeConfiguration fragment; - in if isBool _value then (if _value then "true" else "false") else _value; - }) - envVarFragments - ) - ; - in '' + ExecStart = '' ${pkgs.podman}/bin/podman run \ --rm \ --rmi \ @@ -312,7 +311,8 @@ in rec { -v ${cfg.hostConfig.volumesPath}/etc-dnsmasq.d:/etc/dnsmasq.d \ '' else "" } \ - -p ${toString cfg.hostConfig.dns.hostInternalPort}:53/tcp -p ${toString cfg.hostConfig.dns.hostInternalPort}:53/udp \ + -p ${toString cfg.hostConfig.dns.hostInternalPort}:53/tcp \ + -p ${toString cfg.hostConfig.dns.hostInternalPort}:53/udp \ -p ${toString cfg.hostConfig.web.hostInternalPort}:80/tcp \ ${ concatStringsSep " \\\n"