diff --git a/nixos/modules/services/networking/adguardhome.nix b/nixos/modules/services/networking/adguardhome.nix index 1df503146b54..caab4ba0c953 100644 --- a/nixos/modules/services/networking/adguardhome.nix +++ b/nixos/modules/services/networking/adguardhome.nix @@ -176,22 +176,32 @@ in StartLimitBurst = 10; }; - preStart = lib.optionalString (settings != null) '' - if [ -e "$STATE_DIRECTORY/AdGuardHome.yaml" ] \ - && [ "${toString cfg.mutableSettings}" = "1" ]; then - # First run a schema_version update on the existing configuration - # This ensures that both the new config and the existing one have the same schema_version - # Note: --check-config has the side effect of modifying the file at rest! - ${lib.getExe cfg.package} -c "$STATE_DIRECTORY/AdGuardHome.yaml" --check-config + preStart = + let + installFresh = '' + cp --force "${configFile}" "$STATE_DIRECTORY/AdGuardHome.yaml" + chmod 600 "$STATE_DIRECTORY/AdGuardHome.yaml" + ''; + in + lib.optionalString (settings != null) ( + if cfg.mutableSettings then + '' + if [ -e "$STATE_DIRECTORY/AdGuardHome.yaml" ]; then + # First run a schema_version update on the existing configuration + # This ensures that both the new config and the existing one have the same schema_version + # Note: --check-config has the side effect of modifying the file at rest! + ${lib.getExe cfg.package} -c "$STATE_DIRECTORY/AdGuardHome.yaml" --check-config - # Writing directly to AdGuardHome.yaml results in empty file - ${lib.getExe pkgs.yaml-merge} "$STATE_DIRECTORY/AdGuardHome.yaml" "${configFile}" > "$STATE_DIRECTORY/AdGuardHome.yaml.tmp" - mv "$STATE_DIRECTORY/AdGuardHome.yaml.tmp" "$STATE_DIRECTORY/AdGuardHome.yaml" - else - cp --force "${configFile}" "$STATE_DIRECTORY/AdGuardHome.yaml" - chmod 600 "$STATE_DIRECTORY/AdGuardHome.yaml" - fi - ''; + # Writing directly to AdGuardHome.yaml results in empty file + ${lib.getExe pkgs.yaml-merge} "$STATE_DIRECTORY/AdGuardHome.yaml" "${configFile}" > "$STATE_DIRECTORY/AdGuardHome.yaml.tmp" + mv "$STATE_DIRECTORY/AdGuardHome.yaml.tmp" "$STATE_DIRECTORY/AdGuardHome.yaml" + else + ${installFresh} + fi + '' + else + installFresh + ); serviceConfig = { DynamicUser = true;