about summary refs log tree commit diff
path: root/home/profiles/communication/default.nix
blob: bfcbfd8a41bac33191326c9f5c0c7d003ac8c0be (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
{ config, lib, pkgs, ... }:
let
  cfg = config.profiles.communication;

  poorObfuscation = y: x: "${x}@${y}";
  mapKeys = map (x:
    let
      x' = lib.splitString " " x;
    in
    {
      map = lib.splitString "," (lib.head x');
      key = lib.elemAt x' 1;
      action = lib.last x';
    });
  mbsyncCmd = if pkgs.stdenv.isLinux then "${config.programs.mbsync.package}/bin/mbsync" else "";
  # https://github.com/Mic92/sops-nix/issues/284
  fixSopsPrefix = x: y: builtins.replaceStrings [ "%r" ] [ "/run/user/${toString x}" ] y;
in
{
  imports = [ ../../../modules/nixpkgs.nix ];

  options.profiles.communication = {
    enable = lib.mkEnableOption
      "A profile that enables the system to communicate via internet.";
  };

  config = lib.mkIf cfg.enable (lib.mkMerge [
    # TODO: also configure mail in Darwin
    (lib.mkIf pkgs.stdenv.isLinux {
      sops.secrets.sef-email-password = { };

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

        accounts.sefidel-net = {
          address = poorObfuscation "sefidel.net" "contact";
          gpg = {
            key = "8BDFDFB56842239382A0441B9238BC709E05516A";
            signByDefault = true;
          };

          imap.host = "mail.exotic.sh";
          imap.tls.enable = true;
          smtp.host = "mail.exotic.sh";
          smtp.tls.enable = true;

          mbsync = {
            enable = true;
            create = "maildir";
            remove = "none";
            expunge = "both";
          };

          msmtp.enable = true;
          notmuch.enable = true;
          neomutt.enable = true;
          neomutt.mailboxName = "sefidel-net";
          neomutt.extraMailboxes = [
            # "Archive"
            "Drafts"
            "Junk"
            "Sent"
            "Trash"
          ];

          imapnotify = {
            enable = true;
            boxes = [ "Inbox" ];
            onNotify = pkgs.writeShellScript "imap-on-notify" ''
              ${mbsyncCmd} sefidel-net
              ${pkgs.notmuch}/bin/notmuch new
            '';
          };

          primary = true;
          realName = "sefidel";
          userName = poorObfuscation "sefidel.net" "contact";
          passwordCommand = "${pkgs.coreutils}/bin/cat ${fixSopsPrefix 1000 config.sops.secrets.sef-email-password.path}";
        };
        # TODO: remove
        accounts.sefidel-com = {
          address = poorObfuscation "sefidel.com" "contact";
          # gpg = {
          #   key = "";
          #   signByDefault = true;
          # };

          imap.host = "mail.exotic.sh";
          imap.tls.enable = true;
          smtp.host = "mail.exotic.sh";
          smtp.tls.enable = true;

          mbsync = {
            enable = true;
            create = "maildir";
            remove = "none";
            expunge = "both";
          };

          msmtp.enable = true;
          notmuch.enable = true;
          neomutt.enable = true;
          neomutt.mailboxName = "sefidel-com";
          neomutt.extraMailboxes = [
            "Archive"
            "Drafts"
            "Junk"
            "Sent"
            "Trash"
          ];

          imapnotify = {
            enable = true;
            boxes = [ "Inbox" ];
            onNotify = pkgs.writeShellScript "imap-on-notify" ''
              ${mbsyncCmd} sefidel-com
              ${pkgs.notmuch}/bin/notmuch new
            '';
          };

          primary = false;
          realName = "sefidel";
          userName = poorObfuscation "sefidel.com" "contact";
          passwordCommand = "${pkgs.coreutils}/bin/cat ${fixSopsPrefix 1000 config.sops.secrets.sef-email-password.path}";
        };
        accounts.exotic-sh = {
          address = poorObfuscation "exotic.sh" "sef";
          # gpg = {
          #   key = "";
          #   signByDefault = true;
          # };

          imap.host = "mail.exotic.sh";
          imap.tls.enable = true;
          smtp.host = "mail.exotic.sh";
          smtp.tls.enable = true;

          mbsync = {
            enable = true;
            create = "maildir";
            remove = "none";
            expunge = "both";
          };

          msmtp.enable = true;
          notmuch.enable = true;
          neomutt.enable = true;
          neomutt.mailboxName = "exotic-sh";
          neomutt.extraMailboxes = [
            # "Archive"
            "Drafts"
            "Junk"
            "Sent"
            # "Trash"
          ];

          imapnotify = {
            enable = true;
            boxes = [ "Inbox" ];
            onNotify = pkgs.writeShellScript "imap-on-notify" ''
              ${mbsyncCmd} exotic-sh
              ${pkgs.notmuch}/bin/notmuch new
            '';
          };

          primary = false;
          realName = "sefidel";
          userName = poorObfuscation "exotic.sh" "sef";
          passwordCommand = "${pkgs.coreutils}/bin/cat ${fixSopsPrefix 1000 config.sops.secrets.sef-email-password.path}";
        };

      };

      programs.abook.enable = true;
      programs.mbsync.enable = true;
      programs.msmtp.enable = true;
      programs.notmuch = {
        enable = true;
        hooks.preNew = "${mbsyncCmd} --all";
      };

      programs.neomutt = {
        enable = true;
        sidebar.enable = true;
        sidebar.format = "%D%?F? [%F]?%* %?N?*N/?%S";

        sort = "reverse-threads";

        settings = {
          date_format = ''"%y/%m/%d %I:%M%p"'';
          index_format = ''"%4C %Z %?X?A& ? %D %-15.15L (%-4.4c) %s"'';
          query_command = ''"abook --mutt-query '%s'"'';
          rfc2047_parameters = "yes";
          # Pause 0 seconds for informational messages
          sleep_time = "0";
          # Disables the `+` displayed at line wraps
          markers = "no";
          # Unread mail stay unread until read;
          mark_old = "no";
          # attachments are forwarded with mail;
          mime_forward = "yes";
          # mutt won't ask "press key to continue";
          wait_key = "no";
          # skip to compose when replying
          fast_reply = "yes";
          # save attachments with the body
          fcc_attach = "yes";
          # format of subject when forwarding;
          forward_format = ''"Fwd: %s"'';
          # include message in forwards
          forward_quote = "yes";
          # reply as whomever it was to
          reverse_name = "yes";
          # include message in replies
          include = "yes";

          # edit headers in editor
          autoedit = "yes";
          edit_headers = "yes";

          # fix Encrypt-to-Self, required by `pgp_self_encrypt`
          # TODO: use key from primary account (don't hardcode)
          pgp_default_key = config.accounts.email.accounts.sefidel-net.gpg.key;
        };

        binds = mapKeys [
          "index,pager i noop"
          "index,pager g noop"
          "index \\Cf noop"
          "index j next-entry"
          "index k previous-entry"
          "attach <return> view-mailcap"
          "attach l view-mailcap"
          "editor <space> noop"
          "index G last-entry"
          "index gg first-entry"
          "pager,attach h exit"
          "pager j next-line"
          "pager k previous-line"
          "pager l view-attachments"
          "index D delete-message"
          "index U undelete-message"
          "index L limit"
          "index h noop"
          "index l display-message"
          "index,query <space> tag-entry"
          "browser h goto-parent"
          "index,pager H view-raw-message"
          "browser l select-entry"
          "pager,browser gg top-page"
          "pager,browser G bottom-page"
          "index,pager,browser d half-down"
          "index,pager,browser u half-up"
          "index,pager S sync-mailbox"
          "index,pager R group-reply"
          "index \\031 previous-undeleted" # Mouse wheel
          "index \\005 next-undeleted" # Mouse wheel
          "pager \\031 previous-line" # Mouse wheel
          "pager \\005 next-line" # Mouse wheel
          "editor <Tab> complete-query"

          # Pager stuff
          "index,pager B sidebar-toggle-visible"
        ];
        macros = mapKeys [
          "index,pager \\Ck <sidebar-prev><sidebar-open>"
          "index,pager \\Cj <sidebar-next><sidebar-open>"
          "index,pager \\Cp <sidebar-prev-new><sidebar-open>"
          "index,pager \\Cn <sidebar-next-new><sidebar-open>"
          "index,pager \\Cb <pipe-message>urlscan<Enter>"
          "attach,compose \\Cb <pipe-entry>urlscan<Enter>"
        ] ++ [
          {
            map = [ "index" ];
            key = "S";
            action = "<shell-escape>systemctl start --user mbsync-oneshot &<enter>";
          }
        ];

        # TODO: Re-theme this after nightlamp -> limestone theme
        extraConfig = ''
          # Default index colors:
          color index yellow default '.*'
          color index_author red default '.*'
          color index_number blue default
          color index_subject cyan default '.*'

          # New mail is boldened:
          color index brightyellow black "~N"
          color index_author brightred black "~N"
          color index_subject brightcyan black "~N"


          # Tagged mail is highlighted:
          color index brightyellow blue "~T"
          color index_author brightred blue "~T"
          color index_subject brightcyan blue "~T"


          # Other colors and aesthetic settings:
          mono bold bold
          mono underline underline
          mono indicator reverse
          mono error bold
          color normal default default
          color indicator brightblack white
          color sidebar_highlight red default
          color sidebar_divider brightblack black
          color sidebar_flagged red black
          color sidebar_new green black
          color normal brightyellow default
          color error red default
          color tilde black default
          color message cyan default
          color markers red white
          color attachment white default
          color search brightmagenta default
          color status brightyellow black
          color hdrdefault brightgreen default
          color quoted green default
          color quoted1 blue default
          color quoted2 cyan default
          color quoted3 yellow default
          color quoted4 red default
          color quoted5 brightred default
          color signature brightgreen default
          color bold black default
          color underline black default
          color normal default default


          # Regex highlighting:
          color header blue default ".*"
          color header brightmagenta default "^(From)"
          color header brightcyan default "^(Subject)"
          color header brightwhite default "^(CC|BCC)"
          color body brightred default "[\-\.+_a-zA-Z0-9]+@[\-\.a-zA-Z0-9]+" # Email addresses
          color body brightblue default "(https?|ftp)://[\-\.,/%~_:?&=\#a-zA-Z0-9]+" # URL
          color body green default "\`[^\`]*\`" # Green text between ` and `
          color body brightblue default "^# \.*" # Headings as bold blue
          color body brightcyan default "^## \.*" # Subheadings as bold cyan
          color body brightgreen default "^### \.*" # Subsubheadings as bold green
          color body yellow default "^(\t| )*(-|\\*) \.*" # List items as yellow
          color body brightcyan default "[;:][-o][)/(|]" # emoticons
          color body brightcyan default "[;:][)(|]" # emoticons
          color body brightcyan default "[ ][*][^*]*[*][ ]?" # more emoticon?
          color body brightcyan default "[ ]?[*][^*]*[*][ ]" # more emoticon?
          color body red default "(BAD signature)"
          color body cyan default "(Good signature)"
          color body brightblack default "^gpg: Good signature .*"
          color body brightyellow default "^gpg: "
          color body brightyellow red "^gpg: BAD signature from.*"
          mono body bold "^gpg: Good signature"
          mono body bold "^gpg: BAD signature from.*"
          color body red default "([a-z][a-z0-9+-]*://(((([a-z0-9_.!~*'();:&=+$,-]|%[0-9a-f][0-9a-f])*@)?((([a-z0-9]([a-z0-9-]*[a-z0-9])?)\\.)*([a-z]([a-z0-9-]*[a-z0-9])?)\\.?|[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)(:[0-9]+)?)|([a-z0-9_.!~*'()$,;:@&=+-]|%[0-9a-f][0-9a-f])+)(/([a-z0-9_.!~*'():@&=+$,-]|%[0-9a-f][0-9a-f])*(;([a-z0-9_.!~*'():@&=+$,-]|%[0-9a-f][0-9a-f])*)*(/([a-z0-9_.!~*'():@&=+$,-]|%[0-9a-f][0-9a-f])*(;([a-z0-9_.!~*'():@&=+$,-]|%[0-9a-f][0-9a-f])*)*)*)?(\\?([a-z0-9_.!~*'();/?:@&=+$,-]|%[0-9a-f][0-9a-f])*)?(#([a-z0-9_.!~*'();/?:@&=+$,-]|%[0-9a-f][0-9a-f])*)?|(www|ftp)\\.(([a-z0-9]([a-z0-9-]*[a-z0-9])?)\\.)*([a-z]([a-z0-9-]*[a-z0-9])?)\\.?(:[0-9]+)?(/([-a-z0-9_.!~*'():@&=+$,]|%[0-9a-f][0-9a-f])*(;([-a-z0-9_.!~*'():@&=+$,]|%[0-9a-f][0-9a-f])*)*(/([-a-z0-9_.!~*'():@&=+$,]|%[0-9a-f][0-9a-f])*(;([-a-z0-9_.!~*'():@&=+$,]|%[0-9a-f][0-9a-f])*)*)*)?(\\?([-a-z0-9_.!~*'();/?:@&=+$,]|%[0-9a-f][0-9a-f])*)?(#([-a-z0-9_.!~*'();/?:@&=+$,]|%[0-9a-f][0-9a-f])*)?)[^].,:;!)? \t\r\n<>\"]"
        '';
      };

      programs.notmuch.hooks.postNew = ''
        ${pkgs.libnotify}/bin/notify-send --icon=mail-unread --app-name=notmuch \
          "Email" "Sync completed"
      '';

      services.imapnotify.enable = true;

      # Sync emails on initial login
      systemd.user.services.mbsync-oneshot = {
        Unit = {
          Description = "mbsync oneshot synchronisation";
          After = [ "sops-nix.service" ];
        };
        Service = {
          Type = "oneshot";
          ExecStart = "${mbsyncCmd} -Va";
          RemainAfterExit = false;
          TimeoutStartSec = "5min";
        };
        Install.WantedBy = [ "default.target" ];
      };

      home.file.".mailcap".text =
        let
          setsid = "${pkgs.util-linux}/bin/setsid";
          openfile = "${pkgs.writeShellScript "openfile" ''
        # Helps open a file with xdg-open from mutt in a external program without weird side effects.
        tempdir="''${TMPDIR:-$(mktemp -d)}"
        file="$tempdir/$(basename "$1")"
        [ "$(uname)" = "Darwin" ] && opener="open" || opener="${setsid} -f ${pkgs.xdg-utils}/bin/xdg-open"
        mkdir -p "$tempdir"
        cp -f "$1" "$file"
        $opener "$file" >/dev/null 2>&1
        find "''${tempdir:?}" -mtime +1 -type f -delete
      ''}";
        in
        ''
          ${lib.optionalString false "text/html; ${pkgs.w3m}/bin/w3m -dump -T text/html -I %{charset} -O utf-8 %s; copiousoutut; description=HTML Text; nametemplate=%s.html"}
          text/plain; $EDITOR %s ;
          ${lib.optionalString true "text/html; ${openfile} %s ; nametemplate=%s.html"}
          text/html; ${pkgs.lynx}/bin/lynx -assume_charset=%{charset} -display_charset=utf-8 -dump %s; nametemplate=%s.html; copiousoutput;
          image/*; ${openfile} %s ;
          application/pdf; ${openfile} %s ;
          # application/pgp-encrypted; gpg -d '%s'; copiousoutput;
        '';
    })
    {
      home.packages = with pkgs; [
        ispell
        urlscan

        discord-canary
        element-desktop
        nheko
        slack
        (weechat.override {
          configure = { availablePlugins, ... }: {
            plugins = [
              (availablePlugins.python.withPackages (ps: with ps; [
                potr
                pycrypto
              ]))
              availablePlugins.perl
              availablePlugins.lua
            ];
          };
        })
      ];

      nixpkgs.allowedUnfree = [ "slack" "discord-canary" ];

      xdg.configFile."Element/config.json".text = builtins.toJSON (import ./element-config.nix);
    }
  ]);
}