Commit 1789e76450a0

Vincent Demeester <vincent@sbr.pm>
2026-01-14 22:20:22
feat(mail): Add ntfy notifications for mail service failures
- Send push notifications via ntfy when goimapnotify or imapfilter fail - Log failures to ~/.local/share/mail-service-failures.log - Configure OnFailure hooks in Unit section (not Service) - Use passage to securely retrieve ntfy token - Gracefully handle missing ntfy token
1 parent 8eb8241
Changed files (3)
home/common/services/goimapnotify.nix
@@ -58,6 +58,7 @@ in
           Description = "goimapnotify for ${accountName} IMAP IDLE";
           After = [ "network-online.target" ];
           Wants = [ "network-online.target" ];
+          OnFailure = [ "mail-service-failure@%n.service" ];
         };
 
         Service = {
home/common/services/imapfilter.nix
@@ -9,6 +9,7 @@
       Description = "imapfilter - IMAP mail filtering with IDLE support";
       After = [ "network-online.target" ];
       Wants = [ "network-online.target" ];
+      OnFailure = [ "mail-service-failure@%n.service" ];
     };
 
     Service = {
home/common/services/mail-monitor.nix
@@ -175,6 +175,23 @@ let
       -u "goimapnotify-*.service" | \
       grep -i --line-buffered "mbsync\|index\|new mail\|IDLE\|message"
   '';
+
+  # Script to handle service failures
+  mail-failure-handler = pkgs.writeShellScript "mail-failure-handler" ''
+    SERVICE_NAME="$1"
+    LOG_FILE="$HOME/.local/share/mail-service-failures.log"
+
+    # Log the failure
+    echo "Mail service $SERVICE_NAME failed at $(date)" >> "$LOG_FILE"
+
+    # Send ntfy notification if token is available
+    if TOKEN=$(${pkgs.passage}/bin/passage show ntfy/token 2>/dev/null); then
+      ${pkgs.ntfy-sh}/bin/ntfy publish \
+        --token "$TOKEN" \
+        infrastructure.sbr.pm \
+        "⚠️ Mail service $SERVICE_NAME failed" || true
+    fi
+  '';
 in
 {
   home.packages = [
@@ -186,12 +203,9 @@ in
 
   # Optional: Enable failure notifications via systemd
   # This will log to journal when services fail
-  systemd.user.services.imapfilter.Service.OnFailure = [ "mail-service-failure@%n.service" ];
-  systemd.user.services."goimapnotify-icloud".Service.OnFailure = [
-    "mail-service-failure@%n.service"
-  ];
+  # OnFailure configuration is done per-service in imapfilter.nix and goimapnotify.nix
 
-  # Service to handle failures
+  # Service to handle failures - sends ntfy notifications
   systemd.user.services."mail-service-failure@" = {
     Unit = {
       Description = "Mail service failure notification for %i";
@@ -199,10 +213,12 @@ in
 
     Service = {
       Type = "oneshot";
-      # Log the failure
-      ExecStart = "${pkgs.coreutils}/bin/echo 'Mail service %i failed at $(date)' >> %h/.local/share/mail-service-failures.log";
-      # Could add notification here (ntfy, email, etc.)
-      # ExecStart = "${pkgs.ntfy-sh}/bin/ntfy publish --token $(passage show ntfy/token) topic 'Mail service %i failed'";
+      Environment = [
+        "PATH=${pkgs.bash}/bin:${pkgs.coreutils}/bin:${pkgs.ntfy-sh}/bin:${pkgs.passage}/bin"
+        "PASSAGE_DIR=/home/vincent/.local/share/passage"
+        "PASSAGE_IDENTITIES_FILE=/home/vincent/.local/share/passage/identities"
+      ];
+      ExecStart = "${mail-failure-handler} %i";
     };
   };
 }