aboutsummaryrefslogtreecommitdiff
path: root/modules/email/mailbox-sync.module.nix
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-05-24 17:40:28 +0530
committerAkshay Nair <phenax5@gmail.com>2025-05-24 17:41:00 +0530
commitce0a366e8ba58fb967d4270e9c0e7d3ba759c759 (patch)
treee2db6bcce4eb29bcb92e8d64502d76cf6bc2b95c /modules/email/mailbox-sync.module.nix
parent5fc036f8de3c55bea6a5853bdd35ca65f168a48e (diff)
downloadnixos-config-ce0a366e8ba58fb967d4270e9c0e7d3ba759c759.tar.gz
nixos-config-ce0a366e8ba58fb967d4270e9c0e7d3ba759c759.zip
Add setup for notmuch+lieer+aerc for email
Diffstat (limited to 'modules/email/mailbox-sync.module.nix')
-rw-r--r--modules/email/mailbox-sync.module.nix70
1 files changed, 70 insertions, 0 deletions
diff --git a/modules/email/mailbox-sync.module.nix b/modules/email/mailbox-sync.module.nix
new file mode 100644
index 0000000..ad5f94e
--- /dev/null
+++ b/modules/email/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 + " (tag: " + (.tags | join(" ")) + ")") | join("\n\n")');
+
+ # Notify
+ ${notify-send} --app-name=notmuch "New mail baybey ($new_mail_count)" "$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;
+ };
+}