This repository has no description
12 kB
282 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 pass show c-base/pass
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$
124 unset envelope_from_address
125 set use_envelope_from
126 set reverse_name
127 set markers=no
128
129 set sort=threads
130
131 set index_format="%4C %Z %?GI?%GI& ? %[%y-%m-%d] %-20.20a %?M?(%3M)& ? %s %> %r %g"
132
133 virtual-mailboxes "Unread" "notmuch://?query=tag:unread"
134 virtual-mailboxes "INBOX" "notmuch://?query=tag:inbox"
135 ${lib.concatMapStringsSep "\n" (
136 i: ''${" "}virtual-mailboxes "${i.name}" "notmuch://?query=tag:${i.name}"''
137 ) (lib.mapAttrsToList lib.nameValuePair mailboxes)}
138 virtual-mailboxes "TODO" "notmuch://?query=tag:TODO"
139 virtual-mailboxes "Starred" "notmuch://?query=tag:*"
140 virtual-mailboxes "Archive" "notmuch://?query=tag:archive"
141 virtual-mailboxes "Sent" "notmuch://?query=tag:sent"
142 virtual-mailboxes "Junk" "notmuch://?query=tag:junk"
143 virtual-mailboxes "All" "notmuch://?query=*"
144
145 # clan.lol IMAP - no mailboxes declaration to prevent any startup connection
146 # Select the separator entry in sidebar, folder-hook will redirect to the IMAP server
147 virtual-mailboxes "clan-infra" "notmuch://?query=tag:nonexistent-separator-tag"
148 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\`\""
149 folder-hook "notmuch://\\?query=tag:nonexistent-separator-tag" "push '<change-folder>imaps://mail.clan.lol<enter>'"
150
151 # dedede.org IMAP (Namecheap Private Email)
152 virtual-mailboxes "dedede" "notmuch://?query=tag:nonexistent-dedede-tag"
153 account-hook imaps://mail.privateemail.com "set imap_user = 'mail@dedede.org'; set imap_pass = \"\`rbw get --folder dedede mail@dedede.org\`\""
154 folder-hook "notmuch://\\?query=tag:nonexistent-dedede-tag" "push '<change-folder>imaps://mail.privateemail.com<enter>'"
155
156 tag-transforms "junk" "k" \
157 "unread" "u" \
158 "replied" "↻" \
159 "TODO" "T" \
160
161 # notmuch bindings
162 macro index \\\\ "<vfolder-from-query>"
163 macro index + "<modify-labels>+*\n<sync-mailbox>"
164 macro index - "<modify-labels>-*\n<sync-mailbox>"
165
166 # muchsync
167 bind index \Cr noop
168 macro index \Cr \
169 "<enter-command>unset wait_key<enter> \
170 <sync-mailbox> \
171 <shell-escape>${self.packages.${pkgs.system}.muchsync}/bin/muchsync -vv<enter> \
172 <sync-mailbox>
173
174 #killed
175 bind index d noop
176 bind pager d noop
177
178 bind index S noop
179 bind index s noop
180 bind pager S noop
181 bind pager s noop
182 macro index S "<modify-labels-then-hide>-inbox -unread +junk\n"
183 macro index s "<modify-labels>-junk\n"
184 macro pager S "<modify-labels-then-hide>-inbox -unread +junk\n"
185 macro pager s "<modify-labels>-junk\n"
186
187 bind index A noop
188 bind index a noop
189 bind pager A noop
190 bind pager a noop
191 macro index A "<modify-labels>+archive -unread -inbox\n"
192 macro index a "<modify-labels>-archive\n"
193 macro pager A "<modify-labels>+archive -unread -inbox\n"
194 macro pager a "<modify-labels>-archive\n"
195
196 bind index U noop
197 bind index u noop
198 bind pager U noop
199 bind pager u noop
200 macro index U "<modify-labels>+unread\n"
201 macro index u "<modify-labels>-unread\n"
202 macro pager U "<modify-labels>+unread\n"
203 macro pager u "<modify-labels>-unread\n"
204
205 bind index t noop
206 bind pager t noop
207 macro index t "<modify-labels>"
208
209 # top index bar in email view
210 set pager_index_lines=7
211 # top_index_bar toggle
212 macro pager ,@1 "<enter-command> set pager_index_lines=0; macro pager ] ,@2 'Toggle indexbar<Enter>"
213 macro pager ,@2 "<enter-command> set pager_index_lines=3; macro pager ] ,@3 'Toggle indexbar<Enter>"
214 macro pager ,@3 "<enter-command> set pager_index_lines=7; macro pager ] ,@1 'Toggle indexbar<Enter>"
215 macro pager ] ,@1 'Toggle indexbar
216
217 # scan urls from emails
218 macro index,pager \cb "<pipe-message> ${lib.getExe pkgs.urlscan}<Enter>"
219 macro attach,compose \cb "<pipe-entry> ${lib.getExe pkgs.urlscan}<Enter>"
220
221 # view HTML in graphical browser
222 macro index,pager H "<pipe-message>${viewHtmlBrowser}/bin/view-html-browser<Enter>" "View HTML in browser"
223
224 # sidebar
225 set sidebar_divider_char = '│'
226 set sidebar_delim_chars = "/"
227 set sidebar_short_path
228 set sidebar_folder_indent
229 set sidebar_visible = yes
230 set sidebar_format = '%D%?F? [%F]?%* %?N?%N/? %?S?%S?'
231 set sidebar_width = 20
232 color sidebar_new yellow red
233
234 # sidebar bindings
235 bind index <left> sidebar-prev
236 bind index <right> sidebar-next
237 bind index <space> sidebar-open
238 # sidebar toggle
239 macro index,pager ,@) "<enter-command> set sidebar_visible=no; macro index,pager [ ,@( 'Toggle sidebar'<Enter>"
240 macro index,pager ,@( "<enter-command> set sidebar_visible=yes; macro index,pager [ ,@) 'Toggle sidebar'<Enter>"
241 macro index,pager [ ,@( 'Toggle sidebar'
242
243 # forward with attachment
244 set mime_forward = no
245 set forward_attachments = yes
246
247 # better handling of long URLs
248 set wrap = 0 # don't wrap lines in the pager
249 set markers = no # don't show + markers for wrapped lines
250 set smart_wrap = yes # wrap at word boundaries when displaying
251 set pager_stop = yes # don't jump to next message at end of pager
252 '';
253
254 in
255 (self.wrapLib.makeWrapper {
256 pkgs = pkgs;
257 package = pkgs.neomutt;
258 aliases = [ "mutt" ];
259 runtimeInputs = [
260 pkgs.elinks
261 pkgs.msmtp
262 pkgs.notmuch
263 pkgs.openssh
264 pkgs.urlscan
265 ]
266 ++ lib.optionals pkgs.stdenv.isLinux [
267 pkgs.iputils
268 ];
269 env = {
270 NOTMUCH_CONFIG = notmuchConfig;
271 };
272 flags = {
273 "-F" = "${muttrc}";
274 };
275 }).overrideAttrs
276 (old: {
277 passthru = (old.passthru or { }) // {
278 inherit msmtp;
279 };
280 });
281 };
282}