Commit d99f58b9112f

Vincent Demeester <vincent@sbr.pm>
2026-01-14 20:20:09
feat(mail): Add imapfilter rules auto-updater and fix mu lock issues
- Create timer to pull imapfilter rules every 15min on athena - Fix mu database lock conflicts in goimapnotify - Use emacsclient when mu4e is running, mu index when not - Prevent "already locked" errors during mail sync
1 parent 96fe19d
Changed files (3)
home/common/services/goimapnotify.nix
@@ -26,13 +26,14 @@ let
         - INBOX
 
       # Sync mail when new messages arrive
+      # Smart indexing: use emacsclient if mu4e is running (to avoid lock conflicts)
       onNewMail: |
         ${pkgs.isync}/bin/mbsync ${accountName}
-        ${pkgs.mu}/bin/mu index --quiet
-
-      # Notify Emacs after indexing (if running)
-      onNewMailPost: |
-        ${pkgs.emacs}/bin/emacsclient --eval '(mu4e-update-index)' 2>/dev/null || true
+        if ${pkgs.procps}/bin/pgrep -u $UID mu >/dev/null 2>&1; then
+          ${pkgs.emacs}/bin/emacsclient --eval '(mu4e-update-index)' 2>/dev/null || true
+        else
+          ${pkgs.mu}/bin/mu index --quiet
+        fi
     '';
 
   # Get enabled email accounts that have mbsync enabled
home/common/services/imapfilter-rules-updater.nix
@@ -0,0 +1,36 @@
+{ pkgs, ... }:
+{
+  # Systemd service to update imapfilter rules from git repository
+  systemd.user.services.imapfilter-rules-update = {
+    Unit = {
+      Description = "Update imapfilter rules from git repository";
+    };
+
+    Service = {
+      Type = "oneshot";
+      Environment = [
+        "GIT_SSH_COMMAND=${pkgs.openssh}/bin/ssh -F %h/.ssh/config"
+      ];
+      ExecStart = "${pkgs.git}/bin/git -C %h/.local/share/imapfilter-rules pull --quiet";
+      # Optionally send SIGUSR1 to imapfilter to reload rules (if supported)
+      # ExecStartPost = "${pkgs.systemd}/bin/systemctl --user kill -s SIGUSR1 imapfilter.service || true";
+    };
+  };
+
+  # Timer to run the update every 15 minutes
+  systemd.user.timers.imapfilter-rules-update = {
+    Unit = {
+      Description = "Update imapfilter rules every 15 minutes";
+    };
+
+    Timer = {
+      OnBootSec = "2min";
+      OnUnitActiveSec = "15min";
+      Unit = "imapfilter-rules-update.service";
+    };
+
+    Install = {
+      WantedBy = [ "timers.target" ];
+    };
+  };
+}
systems/athena/home.nix
@@ -4,6 +4,7 @@
     ../../home/common/desktop/passage.nix
     ../../home/common/services/goimapnotify.nix
     ../../home/common/services/imapfilter.nix
+    ../../home/common/services/imapfilter-rules-updater.nix
   ];
 
   systemd.user.services.syncthing.Install.WantedBy = lib.mkForce [ "multi-user.target" ];