aboutsummaryrefslogtreecommitdiff
path: root/modules/email.home/mailbox-sync.module.nix
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-05-27 11:13:06 +0530
committerAkshay Nair <phenax5@gmail.com>2025-05-27 11:13:06 +0530
commit4242a01983eb57ac44d5fcfbda75339d486ea4f1 (patch)
tree3ba2952ec94b76fcbf88f326db4abe776abeb54f /modules/email.home/mailbox-sync.module.nix
parent73a49b61fddb77107cb692fa70b8dc361952e556 (diff)
downloadnixos-config-4242a01983eb57ac44d5fcfbda75339d486ea4f1.tar.gz
nixos-config-4242a01983eb57ac44d5fcfbda75339d486ea4f1.zip
Refactor email setup + add email sender to notification
Diffstat (limited to 'modules/email.home/mailbox-sync.module.nix')
-rw-r--r--modules/email.home/mailbox-sync.module.nix70
1 files changed, 70 insertions, 0 deletions
diff --git a/modules/email.home/mailbox-sync.module.nix b/modules/email.home/mailbox-sync.module.nix
new file mode 100644
index 0000000..bb7ee9d
--- /dev/null
+++ b/modules/email.home/mailbox-sync.module.nix
@@ -0,0 +1,70 @@
+{ config, pkgs, lib, ... }:
+with lib;
+let
+ cfg = config.services.mailbox-sync;
+
+ notmuch = "${pkgs.notmuch}/bin/notmuch";
+ gmi = "${pkgs.lieer}/bin/gmi";
+ notify-send = "${pkgs.libnotify}/bin/notify-send";
+ jq = "${pkgs.jq}/bin/jq";
+
+ notify-script = ''
+ emails=$(${notmuch} search --format=json '${cfg.notify.filter}' \
+ | ${jq} -r '.[0:5] | map("— " + .subject + "\n" + .authors + "\n(tag: " + (.tags | join(" ")) + ")") | join("\n\n")');
+
+ # Notify
+ ${notify-send} --app-name=notmuch "Read your mail dumbass ($new_mail_count)" "\n$emails";
+ '';
+
+ sync-script = ''
+ #!${pkgs.bash}/bin/bash
+ set -eu -o pipefail;
+
+ ${gmi} sync;
+
+ new_mail_count=$(${notmuch} count '${cfg.notify.filter}');
+ if [ $new_mail_count = 0 ]; then
+ echo "No new mail";
+ else
+ ${if cfg.notify.enable then notify-script else ""}
+ fi
+
+ ${notmuch} tag -new -- tag:new;
+ '';
+
+ service-unit = {
+ Unit.ConditionPathExists = "${cfg.maildir}/.gmailieer.json";
+ Service = {
+ Type = "oneshot";
+ ExecStart = "${pkgs.writeScript "mailbox-sync-script" sync-script}";
+ WorkingDirectory = cfg.maildir;
+ Environment = "NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/default/config";
+ };
+ };
+
+ timer-unit = {
+ Timer = {
+ OnCalendar = cfg.sync.frequency;
+ RandomizedDelaySec = 30;
+ };
+ Install.WantedBy = [ "timers.target" ];
+ };
+in {
+ options.services.mailbox-sync = {
+ enable = mkEnableOption "mailbox sync";
+ name = mkOption { type = types.str; default = "default"; };
+ maildir = mkOption { type = types.str; };
+ sync.frequency = mkOption { type = types.str; default = "*:0/5"; };
+ notify.enable = mkEnableOption "notify on new mail";
+ notify.filter = mkOption {
+ type = types.str;
+ default = "tag:inbox and tag:unread and tag:new";
+ };
+ };
+
+ config = mkIf cfg.enable {
+ programs.lieer.enable = true;
+ systemd.user.services."mailbox-sync-${cfg.name}" = service-unit;
+ systemd.user.timers."mailbox-sync-${cfg.name}" = timer-unit;
+ };
+}