summary refs log tree commit diff
diff options
context:
space:
mode:
authorCRTified <carl.schneider+github@ruhr-uni-bochum.de>2021-12-24 18:52:39 +0100
committerCRTified <carl.schneider+github@ruhr-uni-bochum.de>2021-12-27 20:03:52 +0100
commit24b8c37281cf339d1fab1098dd700674ca3cb9e7 (patch)
tree9aa889cc2565c4b4d0a9740e9d839d3b50a7ba5b
parentea0f1cd8004053ce6fa0304405fd4ffc160e7149 (diff)
downloadnixpkgs-24b8c37281cf339d1fab1098dd700674ca3cb9e7.tar
nixpkgs-24b8c37281cf339d1fab1098dd700674ca3cb9e7.tar.gz
nixpkgs-24b8c37281cf339d1fab1098dd700674ca3cb9e7.tar.bz2
nixpkgs-24b8c37281cf339d1fab1098dd700674ca3cb9e7.tar.lz
nixpkgs-24b8c37281cf339d1fab1098dd700674ca3cb9e7.tar.xz
nixpkgs-24b8c37281cf339d1fab1098dd700674ca3cb9e7.tar.zst
nixpkgs-24b8c37281cf339d1fab1098dd700674ca3cb9e7.zip
nixos/adguardhome: Add settings option
This commit introduces `services.adguardhome.settings` and
`services.adguardhome.mutableSettings`.

The first option allows declarative configuration of
AdGuard Home, while the second one controls whether changes
made in the web interface are kept between service restarts.

Co-authored-by: Aaron Andersen <aaron@fosslib.net>
-rw-r--r--nixos/modules/services/networking/adguardhome.nix54
1 files changed, 50 insertions, 4 deletions
diff --git a/nixos/modules/services/networking/adguardhome.nix b/nixos/modules/services/networking/adguardhome.nix
index 03f9b9f9bad..05713adbd83 100644
--- a/nixos/modules/services/networking/adguardhome.nix
+++ b/nixos/modules/services/networking/adguardhome.nix
@@ -10,12 +10,20 @@ let
     "--pidfile /run/AdGuardHome/AdGuardHome.pid"
     "--work-dir /var/lib/AdGuardHome/"
     "--config /var/lib/AdGuardHome/AdGuardHome.yaml"
-    "--host ${cfg.host}"
-    "--port ${toString cfg.port}"
   ] ++ cfg.extraArgs);
 
-in
-{
+  baseConfig = {
+    bind_host = cfg.host;
+    bind_port = cfg.port;
+  };
+
+  configFile = pkgs.writeTextFile {
+    name = "AdGuardHome.yaml";
+    text = builtins.toJSON (recursiveUpdate cfg.settings baseConfig);
+    checkPhase = "${pkgs.adguardhome}/bin/adguardhome -c $out --check-config";
+  };
+
+in {
   options.services.adguardhome = with types; {
     enable = mkEnableOption "AdGuard Home network-wide ad blocker";
 
@@ -44,6 +52,31 @@ in
       '';
     };
 
+    mutableSettings = mkOption {
+      default = true;
+      type = bool;
+      description = ''
+        Allow changes made on the AdGuard Home web interface to persist between
+        service restarts.
+      '';
+    };
+
+    settings = mkOption {
+      type = (pkgs.formats.yaml { }).type;
+      default = { };
+      description = ''
+        AdGuard Home configuration. Refer to
+        <link xlink:href="https://github.com/AdguardTeam/AdGuardHome/wiki/Configuration#configuration-file"/>
+        for details on supported values.
+
+        <note><para>
+          On start and if <option>mutableSettings</option> is <literal>true</literal>,
+          these options are merged into the configuration file on start, taking
+          precedence over configuration changes made on the web interface.
+        </para></note>
+      '';
+    };
+
     extraArgs = mkOption {
       default = [ ];
       type = listOf str;
@@ -62,6 +95,19 @@ in
         StartLimitIntervalSec = 5;
         StartLimitBurst = 10;
       };
+
+      preStart = ''
+        if    [ -e "$STATE_DIRECTORY/AdGuardHome.yaml" ] \
+           && [ "${toString cfg.mutableSettings}" = "1" ]; then
+          # Writing directly to AdGuardHome.yaml results in empty file
+          ${pkgs.yaml-merge}/bin/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
+      '';
+
       serviceConfig = {
         DynamicUser = true;
         ExecStart = "${pkgs.adguardhome}/bin/adguardhome ${args}";