summary refs log tree commit diff
diff options
context:
space:
mode:
authorCarl Richard Theodor Schneider <dev.github@crtified.me>2022-10-30 18:34:01 +0100
committerWinter <winter@winter.cafe>2022-11-15 23:43:15 -0500
commit647ed242dcfbebd62b5f5e6880da347ac705544f (patch)
treeb55452ef13720619659935bf89478dced133b07d
parent9d1ebafb7b23cd446d774ebfbb88533df66d5de2 (diff)
downloadnixpkgs-647ed242dcfbebd62b5f5e6880da347ac705544f.tar
nixpkgs-647ed242dcfbebd62b5f5e6880da347ac705544f.tar.gz
nixpkgs-647ed242dcfbebd62b5f5e6880da347ac705544f.tar.bz2
nixpkgs-647ed242dcfbebd62b5f5e6880da347ac705544f.tar.lz
nixpkgs-647ed242dcfbebd62b5f5e6880da347ac705544f.tar.xz
nixpkgs-647ed242dcfbebd62b5f5e6880da347ac705544f.tar.zst
nixpkgs-647ed242dcfbebd62b5f5e6880da347ac705544f.zip
nixos/adguardhome: allow for empty/unmanaged configs
This commit fixes broken non-declarative configs by
making the assertions more relaxed.
It also allows to remove the forced configuration merge by making
`settings` `null`able (now the default).

Both cases (trivial non-declarative config and `null`able config) are
verified with additional tests.

Fixes #198665
-rw-r--r--nixos/modules/services/networking/adguardhome.nix20
-rw-r--r--nixos/tests/adguardhome.nix16
2 files changed, 28 insertions, 8 deletions
diff --git a/nixos/modules/services/networking/adguardhome.nix b/nixos/modules/services/networking/adguardhome.nix
index eaeaeaeb6a7..bda99cb7942 100644
--- a/nixos/modules/services/networking/adguardhome.nix
+++ b/nixos/modules/services/networking/adguardhome.nix
@@ -51,8 +51,8 @@ in
     };
 
     settings = mkOption {
-      default = { };
-      type = submodule {
+      default = null;
+      type = nullOr (submodule {
         freeformType = (pkgs.formats.yaml { }).type;
         options = {
           schema_version = mkOption {
@@ -79,7 +79,7 @@ in
             '';
           };
         };
-      };
+      });
       description = lib.mdDoc ''
         AdGuard Home configuration. Refer to
         <https://github.com/AdguardTeam/AdGuardHome/wiki/Configuration#configuration-file>
@@ -89,6 +89,10 @@ in
         On start and if {option}`mutableSettings` is `true`,
         these options are merged into the configuration file on start, taking
         precedence over configuration changes made on the web interface.
+
+        Set this to `null` (default) for a non-declarative configuration without any
+        Nix-supplied values.
+        Declarative configurations are supplied with a default `schema_version`, `bind_host`, and `bind_port`.
         :::
       '';
     };
@@ -105,15 +109,15 @@ in
   config = mkIf cfg.enable {
     assertions = [
       {
-        assertion = cfg.settings != { }
-          -> (hasAttrByPath [ "dns" "bind_host" ] cfg.settings)
+        assertion = cfg.settings != null -> cfg.mutableSettings
+          || (hasAttrByPath [ "dns" "bind_host" ] cfg.settings)
           || (hasAttrByPath [ "dns" "bind_hosts" ] cfg.settings);
         message =
           "AdGuard setting dns.bind_host or dns.bind_hosts needs to be configured for a minimal working configuration";
       }
       {
-        assertion = cfg.settings != { }
-          -> hasAttrByPath [ "dns" "bootstrap_dns" ] cfg.settings;
+        assertion = cfg.settings != null -> cfg.mutableSettings
+          || hasAttrByPath [ "dns" "bootstrap_dns" ] cfg.settings;
         message =
           "AdGuard setting dns.bootstrap_dns needs to be configured for a minimal working configuration";
       }
@@ -128,7 +132,7 @@ in
         StartLimitBurst = 10;
       };
 
-      preStart = optionalString (cfg.settings != { }) ''
+      preStart = optionalString (cfg.settings != null) ''
         if    [ -e "$STATE_DIRECTORY/AdGuardHome.yaml" ] \
            && [ "${toString cfg.mutableSettings}" = "1" ]; then
           # Writing directly to AdGuardHome.yaml results in empty file
diff --git a/nixos/tests/adguardhome.nix b/nixos/tests/adguardhome.nix
index 5be69e22e53..ec1cc1e497b 100644
--- a/nixos/tests/adguardhome.nix
+++ b/nixos/tests/adguardhome.nix
@@ -2,6 +2,15 @@
   name = "adguardhome";
 
   nodes = {
+    nullConf = { ... }: { services.adguardhome = { enable = true; }; };
+
+    emptyConf = { lib, ... }: {
+      services.adguardhome = {
+        enable = true;
+        settings = {};
+      };
+    };
+
     declarativeConf = { ... }: {
       services.adguardhome = {
         enable = true;
@@ -34,6 +43,13 @@
   };
 
   testScript = ''
+    with subtest("Minimal (settings = null) config test"):
+        nullConf.wait_for_unit("adguardhome.service")
+
+    with subtest("Default config test"):
+        emptyConf.wait_for_unit("adguardhome.service")
+        emptyConf.wait_for_open_port(3000)
+
     with subtest("Declarative config test, DNS will be reachable"):
         declarativeConf.wait_for_unit("adguardhome.service")
         declarativeConf.wait_for_open_port(53)