1{ 2 pkgs, 3 config, 4 lib, 5 ... 6}: 7 8let 9 address-book = pkgs.writeScriptBin "address-book" '' 10 #!/usr/bin/env bash 11 ${pkgs.mu}/bin/mu cfind "$1" | sed -E 's/(.*) (.*@.*)/\2\t\1/' 12 ${pkgs.ugrep}/bin/ugrep -jPh -m 100 --color=never "$1" cat ${config.accounts.email.maildirBasePath}/addressbook/cam-ldap) 13 ''; 14 sync-mail = pkgs.writeScriptBin "sync-mail" '' 15 ${pkgs.isync}/bin/mbsync "$1" || exit 1 16 ${pkgs.procps}/bin/pkill -2 -x mu 17 ${pkgs.coreutils}/bin/sleep 1 18 ${pkgs.mu}/bin/mu index 19 ''; 20 cfg = config.custom.mail; 21in 22{ 23 options.custom.mail.enable = lib.mkEnableOption "mail"; 24 25 config = lib.mkIf cfg.enable { 26 home.packages = with pkgs; [ 27 (pkgs.writeScriptBin "cam-ldap-addr" '' 28 ${pkgs.openldap}/bin/ldapsearch -xZ -H ldaps://ldap.lookup.cam.ac.uk -b "ou=people,o=University of Cambridge,dc=cam,dc=ac,dc=uk" displayName mail\ 29 | ${pkgs.gawk}/bin/awk '/^dn:/{displayName=""; mail=""; next} /^displayName:/{displayName=$2; for(i=3;i<=NF;i++) displayName=displayName " " $i; next} /^mail:/{mail=$2; next} /^$/{if(displayName!="" && mail!="") print mail "\t" displayName}'\ 30 > ${config.accounts.email.maildirBasePath}/addressbook/cam-ldap 31 '') 32 address-book 33 sync-mail 34 ]; 35 36 programs = { 37 password-store.enable = true; 38 gpg.enable = true; 39 mbsync.enable = true; 40 mu.enable = true; 41 msmtp.enable = true; 42 aerc = { 43 enable = true; 44 extraConfig = { 45 general.unsafe-accounts-conf = true; 46 general.default-save-path = "~/downloads"; 47 ui.mouse-enabled = true; 48 compose.address-book-cmd = "${address-book}/bin/address-book '%s'"; 49 compose.file-picker-cmd = "${pkgs.ranger}/bin/ranger --choosefiles=%f"; 50 compose.format-flowed = true; 51 ui.index-columns = "date<=,name<50,flags>=,subject<*"; 52 ui.column-name = "{{index (.From | persons) 0}}"; 53 "ui:folder=Sent".index-columns = "date<=,to<50,flags>=,subject<*"; 54 "ui:folder=Sent".column-to = "{{index (.To | persons) 0}}"; 55 openers."text/html" = "firefox --new-window"; 56 hooks.mail-recieved = ''notify-send "[$AERC_ACCOUNT/$AERC_FOLDER] mail from $AERC_FROM_NAME" "$AERC_SUBJECT"''; 57 filters = { 58 "text/plain" = "wrap -w 90 | colorize"; 59 "text/calendar" = "calendar"; 60 "application/ics" = "calendar"; 61 "message/delivery-status" = "colorize"; 62 "message/rfc822" = "colorize"; 63 "text/html" = "html | colorize"; 64 }; 65 }; 66 extraBinds = import ./aerc-binds.nix { inherit pkgs; }; 67 }; 68 neomutt = { 69 enable = true; 70 extraConfig = '' 71 # Macro to switch accounts 72 macro index,pager <F1> '"<change-folder> ${config.accounts.email.maildirBasePath}/ryan@freumh.org/Inbox<enter>"' 73 macro index,pager <F2> '"<change-folder> ${config.accounts.email.maildirBasePath}/ryangibb321@gmail.com/Inbox<enter>"' 74 macro index,pager <F3> '"<change-folder> ${config.accounts.email.maildirBasePath}/ryan.gibb@cl.cam.ac.uk/Inbox<enter>"' 75 76 # mutt macros for mu 77 macro index <F8> "<shell-escape>mu find --clearlinks --format=links --linksdir=${config.accounts.email.maildirBasePath}/search " \ 78 "mu find" 79 macro index <F9> "<change-folder-readonly>˜/Maildir/search" \ 80 "mu find results" 81 ''; 82 }; 83 notmuch.enable = true; 84 }; 85 86 services = { 87 imapnotify.enable = true; 88 gpg-agent.enable = true; 89 }; 90 91 accounts.email = { 92 maildirBasePath = "mail"; 93 accounts = { 94 "ryan@freumh.org" = rec { 95 primary = true; 96 realName = "Ryan Gibb"; 97 userName = "ryan@freumh.org"; 98 address = "ryan@freumh.org"; 99 passwordCommand = "${pkgs.pass}/bin/pass show email/ryan@freumh.org"; 100 imap.host = "mail.freumh.org"; 101 smtp = { 102 host = "mail.freumh.org"; 103 port = 465; 104 }; 105 folders = { 106 drafts = "Drafts"; 107 inbox = "Inbox"; 108 sent = "Sent"; 109 trash = "Trash"; 110 }; 111 imapnotify = { 112 enable = true; 113 boxes = [ "Inbox" ]; 114 onNotify = "${sync-mail}/bin/sync-mail ryan@freumh.org:INBOX"; 115 }; 116 mbsync = { 117 enable = true; 118 create = "both"; 119 expunge = "both"; 120 remove = "both"; 121 }; 122 msmtp = { 123 enable = true; 124 }; 125 aerc = { 126 enable = true; 127 extraAccounts = { 128 check-mail-cmd = "${sync-mail}/bin/sync-mail ryan@freumh.org"; 129 check-mail-timeout = "1m"; 130 check-mail = "1h"; 131 folders-sort = [ 132 "Inbox" 133 "Sent" 134 "Drafts" 135 "Archive" 136 "Spam" 137 "Trash" 138 ]; 139 folder-map = "${pkgs.writeText "folder-map" '' 140 Spam = Junk 141 Bin = Trash 142 ''}"; 143 }; 144 }; 145 neomutt = { 146 enable = true; 147 extraConfig = '' 148 bind index g noop 149 macro index gi "<change-folder>=${folders.inbox}<enter>" 150 macro index gs "<change-folder>=${folders.sent}<enter>" 151 macro index gd "<change-folder>=${folders.drafts}<enter>" 152 macro index gt "<change-folder>=${folders.trash}<enter>" 153 ''; 154 }; 155 notmuch.enable = true; 156 }; 157 "misc@freumh.org" = rec { 158 userName = "misc@freumh.org"; 159 address = "misc@freumh.org"; 160 realName = "Misc"; 161 passwordCommand = "${pkgs.pass}/bin/pass show email/misc@freumh.org"; 162 imap.host = "mail.freumh.org"; 163 smtp = { 164 host = "mail.freumh.org"; 165 port = 465; 166 }; 167 folders = { 168 drafts = "Drafts"; 169 inbox = "Inbox"; 170 sent = "Sent"; 171 trash = "Bin"; 172 }; 173 imapnotify = { 174 enable = true; 175 boxes = [ "Inbox" ]; 176 onNotify = "${sync-mail}/bin/sync-mail misc@freumh.org:INBOX"; 177 }; 178 mbsync = { 179 enable = true; 180 create = "both"; 181 expunge = "both"; 182 remove = "both"; 183 }; 184 msmtp = { 185 enable = true; 186 }; 187 neomutt = { 188 enable = true; 189 extraConfig = '' 190 bind index g noop 191 macro index gi "<change-folder>=${folders.inbox}<enter>" 192 macro index gs "<change-folder>=${folders.sent}<enter>" 193 macro index gd "<change-folder>=${folders.drafts}<enter>" 194 macro index gt "<change-folder>=${folders.trash}<enter>" 195 ''; 196 }; 197 notmuch.enable = true; 198 }; 199 "ryan.gibb@cl.cam.ac.uk" = rec { 200 userName = "rtg24@fm.cl.cam.ac.uk"; 201 address = "ryan.gibb@cl.cam.ac.uk"; 202 realName = "Ryan Gibb"; 203 passwordCommand = "${pkgs.pass}/bin/pass show email/ryan.gibb@cl.cam.ac.uk"; 204 flavor = "fastmail.com"; 205 folders = { 206 drafts = "Drafts"; 207 inbox = "Inbox"; 208 sent = "Sent"; 209 trash = "Trash"; 210 }; 211 imapnotify = { 212 enable = true; 213 boxes = [ "Inbox" ]; 214 onNotify = "${sync-mail}/bin/sync-mail ryan.gibb@cl.cam.ac.uk:INBOX"; 215 }; 216 mbsync = { 217 enable = true; 218 create = "both"; 219 expunge = "both"; 220 remove = "both"; 221 }; 222 msmtp = { 223 enable = true; 224 }; 225 aerc = { 226 enable = true; 227 extraAccounts = { 228 check-mail-cmd = "${sync-mail}/bin/sync-mail ryan.gibb@cl.cam.ac.uk"; 229 check-mail-timeout = "1m"; 230 check-mail = "1h"; 231 aliases = "rtg24@cam.ac.uk"; 232 folders-sort = [ 233 "Inbox" 234 "Sidebox" 235 "Sent" 236 "Drafts" 237 "Archive" 238 "Spam" 239 "Trash" 240 ]; 241 folder-map = "${pkgs.writeText "folder-map" '' 242 Bin = Trash 243 ''}"; 244 }; 245 }; 246 neomutt = { 247 enable = true; 248 extraConfig = '' 249 bind index g noop 250 macro index gi "<change-folder>=${folders.inbox}<enter>" 251 macro index gs "<change-folder>=${folders.sent}<enter>" 252 macro index gd "<change-folder>=${folders.drafts}<enter>" 253 macro index gt "<change-folder>=${folders.trash}<enter>" 254 ''; 255 }; 256 notmuch.enable = true; 257 }; 258 "ryangibb321@gmail.com" = rec { 259 userName = "ryangibb321@gmail.com"; 260 address = "ryangibb321@gmail.com"; 261 realName = "Ryan Gibb"; 262 passwordCommand = "${pkgs.pass}/bin/pass show email/ryangibb321@gmail.com"; 263 flavor = "gmail.com"; 264 folders = { 265 drafts = "Drafts"; 266 inbox = "Inbox"; 267 sent = "Sent Mail"; 268 trash = "Bin"; 269 }; 270 imapnotify = { 271 enable = true; 272 boxes = [ "Inbox" ]; 273 onNotify = "${sync-mail}/bin/sync-mail ryangibb321@gmail.com:INBOX"; 274 }; 275 mbsync = { 276 enable = true; 277 create = "both"; 278 expunge = "both"; 279 remove = "both"; 280 }; 281 msmtp = { 282 enable = true; 283 }; 284 aerc = { 285 enable = true; 286 extraAccounts = { 287 check-mail-cmd = "${sync-mail}/bin/sync-mail ryangibb321@gmail.com"; 288 check-mail-timeout = "1m"; 289 check-mail = "1h"; 290 folders-sort = [ 291 "Inbox" 292 "Sidebox" 293 "[Gmail]/Sent Mail" 294 "[Gmail]/Drafts" 295 "[Gmail]/All Mail" 296 "[Gmail]/Spam" 297 "[Gmail]/Trash" 298 ]; 299 copy-to = "'[Gmail]/Sent Mail'"; 300 archive = "'[Gmail]/All Mail'"; 301 postpone = "[Gmail]/Drafts"; 302 }; 303 }; 304 neomutt = { 305 enable = true; 306 extraConfig = '' 307 bind index g noop 308 macro index gi "<change-folder>=${folders.inbox}<enter>" 309 macro index gs "<change-folder>=${folders.sent}<enter>" 310 macro index gd "<change-folder>=${folders.drafts}<enter>" 311 macro index gt "<change-folder>=${folders.trash}<enter>" 312 ''; 313 }; 314 notmuch.enable = true; 315 }; 316 search = { 317 maildir.path = "search"; 318 realName = "Search Index"; 319 address = "search@local"; 320 aerc.enable = true; 321 aerc.extraAccounts = { 322 source = "maildir://~/mail/search"; 323 }; 324 aerc.extraConfig = { 325 ui = { 326 index-columns = "flags>4,date<*,to<30,name<30,subject<*"; 327 column-to = "{{(index .To 0).Address}}"; 328 }; 329 }; 330 }; 331 }; 332 }; 333 }; 334}