From dcbc6a443359d49e52dacae1c810154d249e3a5d Mon Sep 17 00:00:00 2001
From: happysalada <raphael@megzari.com>
Date: Fri, 27 Nov 2020 11:28:26 +0900
Subject: [PATCH] nexdns: add module and tests

---
 modules/module-list.nix              |  1 +
 modules/services/nextdns/default.nix |  8 +++-----
 release.nix                          |  1 +
 tests/services-nextdns.nix           | 17 +++++++++++++++++
 4 files changed, 22 insertions(+), 5 deletions(-)
 create mode 100644 tests/services-nextdns.nix

diff --git a/modules/module-list.nix b/modules/module-list.nix
index 6375ea38..d7ba5509 100644
--- a/modules/module-list.nix
+++ b/modules/module-list.nix
@@ -57,6 +57,7 @@
   ./services/synapse-bt.nix
   ./services/synergy
   ./services/yabai
+  ./services/nextdns
   ./programs/bash
   ./programs/fish.nix
   ./programs/gnupg.nix
diff --git a/modules/services/nextdns/default.nix b/modules/services/nextdns/default.nix
index b4a87c37..23120965 100644
--- a/modules/services/nextdns/default.nix
+++ b/modules/services/nextdns/default.nix
@@ -28,12 +28,10 @@ in {
 
     environment.systemPackages = [ nextdns ];
 
-    launchd.user.agents.nextdns = {
+    launchd.daemons.nextdns = {
       path = [ nextdns ];
-      script = ''
-        "${pkgs.nextdns}/bin/nextdns run ${escapeShellArgs cfg.arguments}";
-      '';
-
+      serviceConfig.ProgramArguments =
+        [ "${pkgs.nextdns}/bin/nextdns" "run" (escapeShellArgs cfg.arguments) ];
       serviceConfig.KeepAlive = true;
       serviceConfig.RunAtLoad = true;
     };
diff --git a/release.nix b/release.nix
index 2747eb64..6dc3a523 100644
--- a/release.nix
+++ b/release.nix
@@ -118,6 +118,7 @@ let
     tests.sockets-nix-daemon = makeTest ./tests/sockets-nix-daemon.nix;
     tests.services-dnsmasq = makeTest ./tests/services-dnsmasq.nix;
     tests.services-nix-gc = makeTest ./tests/services-nix-gc.nix;
+    tests.services-nextdns = makeTest ./tests/services-nextdns.nix;
     tests.services-ofborg = makeTest ./tests/services-ofborg.nix;
     tests.services-offlineimap = makeTest ./tests/services-offlineimap.nix;
     tests.services-privoxy = makeTest ./tests/services-privoxy.nix;
diff --git a/tests/services-nextdns.nix b/tests/services-nextdns.nix
new file mode 100644
index 00000000..4d6224f2
--- /dev/null
+++ b/tests/services-nextdns.nix
@@ -0,0 +1,17 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+let nextdns = pkgs.runCommand "nextdns-0.0.0" { } "mkdir $out";
+
+in {
+  services.nextdns.enable = true;
+  services.nextdns.arguments = [ "-config" "10.0.3.0/24=abcdef" ];
+
+  test = ''
+    echo >&2 "checking nextdns service in ~/Library/LaunchDaemons"
+    grep "org.nixos.nextdns" ${config.out}/Library/LaunchDaemons/org.nixos.nextdns.plist
+    grep "/bin/nextdns" ${config.out}/Library/LaunchDaemons/org.nixos.nextdns.plist
+    grep "'-config' '10.0.3.0/24=abcdef'" ${config.out}/Library/LaunchDaemons/org.nixos.nextdns.plist
+  '';
+}