From 65912bc6841cf420eb8c0a20e03df7cbbff5963f Mon Sep 17 00:00:00 2001
From: Linus Heckemann <git@sphalerite.org>
Date: Fri, 16 Aug 2024 11:34:29 +0200
Subject: [PATCH] imapnotify: provide an option for setting PATH

Including some useful presetting for notmuch and mbsync.
---
 modules/services/imapnotify.nix               | 21 +++++++++++++++++--
 .../services/imapnotify/imapnotify.service    |  1 +
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/modules/services/imapnotify.nix b/modules/services/imapnotify.nix
index 63e61e7c7..a52b86e97 100644
--- a/modules/services/imapnotify.nix
+++ b/modules/services/imapnotify.nix
@@ -27,8 +27,8 @@ let
           Restart = "always";
           RestartSec = 30;
           Type = "simple";
-        } // optionalAttrs account.notmuch.enable {
-          Environment =
+          Environment = [ "PATH=${cfg.path}" ]
+            ++ optional account.notmuch.enable
             "NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/default/config";
         };
 
@@ -97,6 +97,17 @@ in {
         example = literalExpression "pkgs.imapnotify";
         description = "The imapnotify package to use";
       };
+
+      path = mkOption {
+        type = types.listOf types.package;
+        apply = lib.makeBinPath;
+        default = [ ];
+        description = ''
+          List of packages to provide in PATH for the imapnotify service.
+
+          Note, this does not apply to the Darwin launchd service.
+        '';
+      };
     };
 
     accounts.email.accounts = mkOption {
@@ -122,6 +133,12 @@ in {
       (checkAccounts (a: a.userName == null) "username")
     ];
 
+    services.imapnotify.path = lib.mkMerge [
+      (lib.mkIf config.programs.notmuch.enable [ pkgs.notmuch ])
+      (lib.mkIf config.programs.mbsync.enable
+        [ config.programs.mbsync.package ])
+    ];
+
     systemd.user.services = listToAttrs (map genAccountUnit imapnotifyAccounts);
 
     launchd.agents = listToAttrs (map genAccountAgent imapnotifyAccounts);
diff --git a/tests/modules/services/imapnotify/imapnotify.service b/tests/modules/services/imapnotify/imapnotify.service
index 3e3bd9ff6..cc1ba77b8 100644
--- a/tests/modules/services/imapnotify/imapnotify.service
+++ b/tests/modules/services/imapnotify/imapnotify.service
@@ -2,6 +2,7 @@
 WantedBy=default.target
 
 [Service]
+Environment=PATH=
 Environment=NOTMUCH_CONFIG=/home/hm-user/.config/notmuch/default/config
 ExecStart=@goimapnotify@/bin/goimapnotify -conf '/nix/store/00000000000000000000000000000000-imapnotify-hm-example.com-config.json'
 Restart=always