modules(pihole-container): extract helper function to lib & restructure lets

This commit is contained in:
Christopher Bacher 2022-10-15 21:06:57 +02:00
parent d15ee5c8fa
commit 41d400120d
2 changed files with 20 additions and 18 deletions

View file

@ -19,4 +19,6 @@
; ;
in _accessValueOfFragment attrs fragment in _accessValueOfFragment attrs fragment
; ;
toEnvValue = value: if builtins.isBool value then (if value then "true" else "false") else value;
} }

View file

@ -1,5 +1,5 @@
{ piholeFlake, util }: { config, pkgs, lib, ... }: with lib; with builtins; let { piholeFlake, util }: { config, pkgs, lib, ... }: with lib; with builtins; let
inherit (util) collectAttrFragments accessValueOfFragment; inherit (util) collectAttrFragments accessValueOfFragment toEnvValue;
cfg = config.services.pihole; cfg = config.services.pihole;
systemTimeZone = config.time.timeZone; systemTimeZone = config.time.timeZone;
@ -280,28 +280,27 @@ in rec {
after = [ "network-online.target" ]; after = [ "network-online.target" ];
serviceConfig = let 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 { in {
ExecStartPre = mkIf cfg.hostConfig.persistVolumes [ ExecStartPre = mkIf cfg.hostConfig.persistVolumes [
"${pkgs.coreutils}/bin/mkdir -p ${cfg.hostConfig.volumesPath}/etc-pihole" "${pkgs.coreutils}/bin/mkdir -p ${cfg.hostConfig.volumesPath}/etc-pihole"
"${pkgs.coreutils}/bin/mkdir -p ${cfg.hostConfig.volumesPath}/etc-dnsmasq.d" "${pkgs.coreutils}/bin/mkdir -p ${cfg.hostConfig.volumesPath}/etc-dnsmasq.d"
]; ];
ExecStart = let ExecStart = ''
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 ''
${pkgs.podman}/bin/podman run \ ${pkgs.podman}/bin/podman run \
--rm \ --rm \
--rmi \ --rmi \
@ -312,7 +311,8 @@ in rec {
-v ${cfg.hostConfig.volumesPath}/etc-dnsmasq.d:/etc/dnsmasq.d \ -v ${cfg.hostConfig.volumesPath}/etc-dnsmasq.d:/etc/dnsmasq.d \
'' else "" '' 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 \ -p ${toString cfg.hostConfig.web.hostInternalPort}:80/tcp \
${ ${
concatStringsSep " \\\n" concatStringsSep " \\\n"