This repository has no description
12 kB
286 lines
1{ self, ... }:
2{
3 perSystem =
4 {
5 pkgs,
6 lib,
7 system,
8 ...
9 }:
10 {
11 packages.mutt =
12 let
13 # Mailboxes configuration from mail.nix
14 mailboxes = self.packages.${system}.notmuch.mailboxes;
15 notmuchConfig = self.packages.${system}.notmuch.passthru.config.configFile.path;
16
17 mailcap = pkgs.writeText "mailcap" ''
18 text/html; ${pkgs.elinks}/bin/elinks -dump ; copiousoutput;
19 '';
20
21 # Script to view HTML emails in browser with embedded images
22 viewHtmlBrowser = pkgs.writeShellScriptBin "view-html-browser" ''
23 set -euo pipefail
24 tmpfile=$(mktemp --suffix=.html)
25 trap 'rm -f "$tmpfile"' EXIT
26
27 ${pkgs.python3}/bin/python3 -c '
28 import email, sys, base64
29
30 msg = email.message_from_file(sys.stdin)
31
32 # Build map of Content-ID -> base64 data URI
33 cid_map = {}
34 for part in msg.walk():
35 cid = part.get("Content-ID", "").strip("<>")
36 if cid and part.get_content_maintype() == "image":
37 data = part.get_payload(decode=True)
38 if data:
39 mime = part.get_content_type()
40 cid_map[cid] = f"data:{mime};base64,{base64.b64encode(data).decode()}"
41
42 # Find HTML part
43 html = None
44 for part in msg.walk():
45 if part.get_content_type() == "text/html":
46 payload = part.get_payload(decode=True)
47 charset = part.get_content_charset() or "utf-8"
48 html = payload.decode(charset, errors="replace")
49 break
50
51 if html:
52 for cid, data_uri in cid_map.items():
53 html = html.replace(f"cid:{cid}", data_uri)
54 print(html)
55 else:
56 print("<html><body><p>No HTML content found.</p></body></html>")
57 ' > "$tmpfile"
58
59 ${if pkgs.stdenv.isDarwin then "open" else "xdg-open"} "$tmpfile"
60 sleep 2
61 '';
62
63 msmtprc = pkgs.writeText "msmtprc" ''
64 defaults
65 logfile ~/.msmtp.log
66 account prism
67 host neoprism.r
68 account c-base
69 from lassulus@c-base.org
70 host c-mail.c-base.org
71 port 465
72 tls on
73 tls_starttls off
74 auth on
75 user lassulus
76 passwordeval rbw get --folder c-base login --field password
77 account default: prism
78 '';
79
80 msmtp = pkgs.writeShellScriptBin "msmtp" ''
81 export NOTMUCH_CONFIG="${notmuchConfig}"
82
83 # Check if using c-base account (either explicit -a c-base or via From: header)
84 using_cbase=0
85 for arg in "$@"; do
86 if [[ "$arg" == "c-base" ]] || [[ "$arg" =~ @c-base\.org ]]; then
87 using_cbase=1
88 break
89 fi
90 done
91
92 # For c-base account, always send directly
93 if [ "$using_cbase" -eq 1 ]; then
94 ${pkgs.coreutils}/bin/tee >(${pkgs.notmuch}/bin/notmuch insert +sent) | \
95 ${pkgs.msmtp}/bin/msmtp -C ${msmtprc} "$@"
96 # For default account, try neoprism.r first, then SSH
97 elif ping -W2 -c1 neoprism.r >/dev/null 2>&1; then
98 ${pkgs.coreutils}/bin/tee >(${pkgs.notmuch}/bin/notmuch insert +sent) | \
99 ${pkgs.msmtp}/bin/msmtp -C ${msmtprc} "$@"
100 else
101 # Fall back to SSH tunnel to neoprism, submit via SMTP to local daemon
102 echo "neoprism.r not reachable, sending via SSH..." >&2
103 ${pkgs.coreutils}/bin/tee >(${pkgs.notmuch}/bin/notmuch insert +sent) | \
104 ${pkgs.openssh}/bin/ssh -p 45621 neoprism.lassul.us 'msmtp -C /dev/null --host=localhost --port=25 --read-envelope-from --read-recipients'
105 fi
106 '';
107
108 muttrc = pkgs.writeText "muttrc" ''
109 # read html mails
110 auto_view text/html
111 set mailcap_path = ${mailcap}
112
113 # notmuch
114 set folder="$HOME/Maildir"
115 set nm_default_uri = "notmuch://$HOME/Maildir"
116 set nm_record = yes
117 set nm_record_tags = "-inbox me archive"
118 set spoolfile = +Inbox
119 set virtual_spoolfile = yes
120
121 set sendmail="${msmtp}/bin/msmtp"
122 set from="lassulus@lassul.us"
123 alternates ^.*@lassul\.us$ ^.*@.*\.r$ ^.*@c-base\.org$
124 unset envelope_from_address
125 set use_envelope_from
126 set reverse_name
127 set markers=no
128
129 # Automatically use c-base email when sending/replying to c-base addresses
130 send-hook "." "unmy_hdr From"
131 send-hook "~C .*@c-base\.org" "my_hdr From: lassulus@c-base.org"
132
133 set sort=threads
134
135 set index_format="%4C %Z %?GI?%GI& ? %[%y-%m-%d] %-20.20a %?M?(%3M)& ? %s %> %r %g"
136
137 virtual-mailboxes "Unread" "notmuch://?query=tag:unread"
138 virtual-mailboxes "INBOX" "notmuch://?query=tag:inbox"
139 ${lib.concatMapStringsSep "\n" (
140 i: ''${" "}virtual-mailboxes "${i.name}" "notmuch://?query=tag:${i.name}"''
141 ) (lib.mapAttrsToList lib.nameValuePair mailboxes)}
142 virtual-mailboxes "TODO" "notmuch://?query=tag:TODO"
143 virtual-mailboxes "Starred" "notmuch://?query=tag:*"
144 virtual-mailboxes "Archive" "notmuch://?query=tag:archive"
145 virtual-mailboxes "Sent" "notmuch://?query=tag:sent"
146 virtual-mailboxes "Junk" "notmuch://?query=tag:junk"
147 virtual-mailboxes "All" "notmuch://?query=*"
148
149 # clan.lol IMAP - no mailboxes declaration to prevent any startup connection
150 # Select the separator entry in sidebar, folder-hook will redirect to the IMAP server
151 virtual-mailboxes "clan-infra" "notmuch://?query=tag:nonexistent-separator-tag"
152 account-hook imaps://mail.clan.lol "set imap_user = 'infra@clan.lol'; set imap_pass = \"\`SOPS_AGE_KEY=\$(rbw get --folder clan.lol clan-infra --field age-key) nix develop git+https://git.clan.lol/clan/clan-infra -c clan vars get --flake git+https://git.clan.lol/clan/clan-infra web01 infra-mail/infra-password\`\""
153 folder-hook "notmuch://\\?query=tag:nonexistent-separator-tag" "push '<change-folder>imaps://mail.clan.lol<enter>'"
154
155 # dedede.org IMAP (Namecheap Private Email)
156 virtual-mailboxes "dedede" "notmuch://?query=tag:nonexistent-dedede-tag"
157 account-hook imaps://mail.privateemail.com "set imap_user = 'mail@dedede.org'; set imap_pass = \"\`rbw get --folder dedede mail@dedede.org\`\""
158 folder-hook "notmuch://\\?query=tag:nonexistent-dedede-tag" "push '<change-folder>imaps://mail.privateemail.com<enter>'"
159
160 tag-transforms "junk" "k" \
161 "unread" "u" \
162 "replied" "↻" \
163 "TODO" "T" \
164
165 # notmuch bindings
166 macro index \\\\ "<vfolder-from-query>"
167 macro index + "<modify-labels>+*\n<sync-mailbox>"
168 macro index - "<modify-labels>-*\n<sync-mailbox>"
169
170 # muchsync
171 bind index \Cr noop
172 macro index \Cr \
173 "<enter-command>unset wait_key<enter> \
174 <sync-mailbox> \
175 <shell-escape>${self.packages.${pkgs.system}.muchsync}/bin/muchsync -vv<enter> \
176 <sync-mailbox>
177
178 #killed
179 bind index d noop
180 bind pager d noop
181
182 bind index S noop
183 bind index s noop
184 bind pager S noop
185 bind pager s noop
186 macro index S "<modify-labels-then-hide>-inbox -unread +junk\n"
187 macro index s "<modify-labels>-junk\n"
188 macro pager S "<modify-labels-then-hide>-inbox -unread +junk\n"
189 macro pager s "<modify-labels>-junk\n"
190
191 bind index A noop
192 bind index a noop
193 bind pager A noop
194 bind pager a noop
195 macro index A "<modify-labels>+archive -unread -inbox\n"
196 macro index a "<modify-labels>-archive\n"
197 macro pager A "<modify-labels>+archive -unread -inbox\n"
198 macro pager a "<modify-labels>-archive\n"
199
200 bind index U noop
201 bind index u noop
202 bind pager U noop
203 bind pager u noop
204 macro index U "<modify-labels>+unread\n"
205 macro index u "<modify-labels>-unread\n"
206 macro pager U "<modify-labels>+unread\n"
207 macro pager u "<modify-labels>-unread\n"
208
209 bind index t noop
210 bind pager t noop
211 macro index t "<modify-labels>"
212
213 # top index bar in email view
214 set pager_index_lines=7
215 # top_index_bar toggle
216 macro pager ,@1 "<enter-command> set pager_index_lines=0; macro pager ] ,@2 'Toggle indexbar<Enter>"
217 macro pager ,@2 "<enter-command> set pager_index_lines=3; macro pager ] ,@3 'Toggle indexbar<Enter>"
218 macro pager ,@3 "<enter-command> set pager_index_lines=7; macro pager ] ,@1 'Toggle indexbar<Enter>"
219 macro pager ] ,@1 'Toggle indexbar
220
221 # scan urls from emails
222 macro index,pager \cb "<pipe-message> ${lib.getExe pkgs.urlscan}<Enter>"
223 macro attach,compose \cb "<pipe-entry> ${lib.getExe pkgs.urlscan}<Enter>"
224
225 # view HTML in graphical browser
226 macro index,pager H "<pipe-message>${viewHtmlBrowser}/bin/view-html-browser<Enter>" "View HTML in browser"
227
228 # sidebar
229 set sidebar_divider_char = '│'
230 set sidebar_delim_chars = "/"
231 set sidebar_short_path
232 set sidebar_folder_indent
233 set sidebar_visible = yes
234 set sidebar_format = '%D%?F? [%F]?%* %?N?%N/? %?S?%S?'
235 set sidebar_width = 20
236 color sidebar_new yellow red
237
238 # sidebar bindings
239 bind index <left> sidebar-prev
240 bind index <right> sidebar-next
241 bind index <space> sidebar-open
242 # sidebar toggle
243 macro index,pager ,@) "<enter-command> set sidebar_visible=no; macro index,pager [ ,@( 'Toggle sidebar'<Enter>"
244 macro index,pager ,@( "<enter-command> set sidebar_visible=yes; macro index,pager [ ,@) 'Toggle sidebar'<Enter>"
245 macro index,pager [ ,@( 'Toggle sidebar'
246
247 # forward with attachment
248 set mime_forward = no
249 set forward_attachments = yes
250
251 # better handling of long URLs
252 set wrap = 0 # don't wrap lines in the pager
253 set markers = no # don't show + markers for wrapped lines
254 set smart_wrap = yes # wrap at word boundaries when displaying
255 set pager_stop = yes # don't jump to next message at end of pager
256 '';
257
258 in
259 (self.wrapLib.makeWrapper {
260 pkgs = pkgs;
261 package = pkgs.neomutt;
262 aliases = [ "mutt" ];
263 runtimeInputs = [
264 pkgs.elinks
265 pkgs.msmtp
266 pkgs.notmuch
267 pkgs.openssh
268 pkgs.urlscan
269 ]
270 ++ lib.optionals pkgs.stdenv.isLinux [
271 pkgs.iputils
272 ];
273 env = {
274 NOTMUCH_CONFIG = notmuchConfig;
275 };
276 flags = {
277 "-F" = "${muttrc}";
278 };
279 }).overrideAttrs
280 (old: {
281 passthru = (old.passthru or { }) // {
282 inherit msmtp;
283 };
284 });
285 };
286}