aboutsummaryrefslogtreecommitdiffstats
path: root/home/mail/default.nix
blob: a84f307c505b3929d7203c93ad357a812817478d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{ pkgs, config, ... }:

let
  # Common configuration for Gmail-based accounts
  mkGmailAccount =
    {
      name,
      address,
      userName,
      passwordCommand,
      primary ? false,
    }:
    let
      accountConfig = {
        inherit
          primary
          address
          userName
          passwordCommand
          ;
        realName = "Petri Hienonen";

        imap = {
          host = "imap.gmail.com";
          port = 993;
          tls.enable = true;
        };

        smtp = {
          host = "smtp.gmail.com";
          port = 587;
          tls.enable = true;
        };

        maildir = {
          path = name;
        };

        mbsync = {
          enable = true;
          create = "both";
          expunge = "both";
          remove = "both";
          patterns = [
            "*"
            "!Drafts"
            "!Sent Mail"
          ];
          extraConfig.channel = {
            CopyArrivalDate = "yes";
            SyncState = "*";
          };
        };

        notmuch.enable = true;

        himalaya = {
          enable = true;
          settings = {
            inherit primary;
            display-name = "Petri Hienonen";
            signature = "Regards,\nPetri Hienonen";
            signature-delim = "-- \n";

            "backend.type" = "notmuch";
            "backend.db-path" = "${config.home.homeDirectory}/Mail/${name}";

            "folder.aliases.inbox" = "INBOX";
            "folder.aliases.sent" = "Sent Mail";
            "folder.aliases.drafts" = "Drafts";
            "folder.aliases.trash" = "Trash";

            "message.write.headers" = [
              "From"
              "To"
              "Cc"
              "Bcc"
              "Subject"
            ];
            "message.send.save-copy" = true;
            "message.delete.style" = "folder";

            "template.new.signature-style" = "inlined";
            "template.reply.posting-style" = "top";
            "template.reply.signature-style" = "below-quote";
            "template.reply.quote-headline-fmt" = "On %d/%m/%Y %H:%M, {senders} wrote:\n";

            "message.send.backend.type" = "smtp";
            "message.send.backend.host" = "smtp.gmail.com";
            "message.send.backend.port" = 465;
            "message.send.backend.login" = userName;
            "message.send.backend.auth.type" = "password";
            "message.send.backend.auth.cmd" = passwordCommand;

            "pgp.type" = "commands";
            "pgp.encrypt-cmd" = "${pkgs.gnupg}/bin/gpg --encrypt --armor --quiet -r '%r'";
            "pgp.decrypt-cmd" = "${pkgs.gnupg}/bin/gpg --decrypt --quiet";
            "pgp.sign-cmd" = "${pkgs.gnupg}/bin/gpg --sign --armor --quiet --default-key '%s'";
            "pgp.verify-cmd" = "${pkgs.gnupg}/bin/gpg --verify --quiet";
          };
        };

        aerc = {
          enable = true;
        };

        imapnotify = {
          enable = true;
          boxes = [ "INBOX" ];
          onNotify = emailNotifyScript name;
          extraConfig.wait = 60;
        };
      };
    in
    accountConfig;

  # Simplified notification script that only extracts sender and subject
  emailNotifyScript =
    account:
    pkgs.writeShellScript "email-notify-${account}" ''
      set -euo pipefail

      # Sync this account specifically
      ${pkgs.isync}/bin/mbsync "${account}"

      # Wait a moment for notmuch to index
      sleep 2
      ${pkgs.notmuch}/bin/notmuch new

      LATEST_MSG=$(${pkgs.notmuch}/bin/notmuch search --output=messages --limit=1 "tag:unread and folder:${account}/INBOX" | head -1)

      if [ -n "$LATEST_MSG" ]; then
        # Extract sender and subject using notmuch show
        SENDER=$(${pkgs.notmuch}/bin/notmuch show --format=json "$LATEST_MSG" | ${pkgs.jq}/bin/jq -r '.[][0].headers.From // "Unknown sender"')
        SUBJECT=$(${pkgs.notmuch}/bin/notmuch show --format=json "$LATEST_MSG" | ${pkgs.jq}/bin/jq -r '.[][0].headers.Subject // "No subject"')

        # Send notification
        ${pkgs.libnotify}/bin/notify-send -i mail-unread -a "Email" \
          "New email in ${account}" \
          "From: $SENDER\nSubject: $SUBJECT" \
          -t 10000  # 10 second timeout
      fi
    '';

in
{
  accounts.email = {
    maildirBasePath = "${config.home.homeDirectory}/Mail";

    accounts = {
      gmail = mkGmailAccount {
        name = "gmail";
        primary = true;
        address = "petri.hienonen@gmail.com";
        userName = "petri.hienonen@gmail.com";
        passwordCommand = "${pkgs.coreutils}/bin/cat ${config.age.secrets.gmail.path}";
      };

      relesoft = mkGmailAccount {
        name = "relesoft";
        primary = false;
        address = "petri.hienonen@relesoft.io";
        userName = "petri.hienonen@relesoft.io";
        passwordCommand = "${pkgs.coreutils}/bin/cat ${config.age.secrets.relesoft.path}";
      };
    };
  };

  # Global settings for programs (not account-specific)
  programs.mbsync = {
    enable = true;
  };

  services.mbsync = {
    enable = true;
    frequency = "*:0/5";
  };

  programs.notmuch = {
    enable = true;
    hooks = {
      preNew = "${pkgs.isync}/bin/mbsync -a || true";
    };
    new = {
      tags = [
        "new"
        "unread"
      ];
      ignore = [
        "/.*[.](json|lock|bak)$/"
        ".uidvalidity"
        ".mbsyncstate"
        ".mbsyncstate.journal"
        ".msoepln"
        ".mbsyncstate.new"
        ".mbsyncstate.lock"
      ];
    };
  };

  programs.himalaya = {
    enable = true;
    package = pkgs.himalaya.override {
      withFeatures = [
        "notmuch"
        "pgp-gpg"
      ];
    };
  };

  # aerc configuration
  programs.aerc = {
    enable = true;
    # aerc will automatically pick up accounts from accounts.email
    # Additional aerc-wide configuration can go here
    extraConfig = {
      compose = {
        address-book-cmd = "khard email --parse --remove-first-line %q";
      };
      general = {
        unsafe-accounts-conf = true;
        # Example aerc configuration
        # ui.message-list-time-format = "2006-01-02 15:04"
        # ui.timestamp-format = "2006-01-02 15:04"
      };
    };
  };
}