aboutsummaryrefslogtreecommitdiffstats
path: root/modules/communication/neomutt/mail/mbsync.nix
blob: 7bc72ce960a59c34e45340f741fb4f97069ea842 (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
# Mailbox synchronization
{
  config,
  lib,
  ...
}@inputs:
let
  mbsyncValue =
    value:
    if lib.isList value then
      lib.concatStringsSep " " (map mbsyncValue value)
    else if lib.isBool value then
      lib.hm.booleans.yesNo value
    else if lib.isInt value then
      toString value
    else if lib.isString value then
      if builtins.match ".* .*" value != null then ''"${lib.escape [ ''"'' ] value}"'' else value
    else
      throw "Unsupported mbsync value";

  mbsyncSection = name: attrs: ''
    ${name}
    ${lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v: "${n} ${mbsyncValue v}") attrs)}
  '';

  mbsyncAccount =
    account:
    let
      mbsyncNames = (import ../lib.nix inputs).mbsyncNamesFromName account.name;
    in
    lib.concatStringsSep "\n" [
      (mbsyncSection "IMAPAccount ${mbsyncNames.base}" (
        {
          Host = account.imap.host;
          User = account.userName;
          PipelineDepth = 50;
          TLSType =
            if !account.imap.tls.enable then
              "None"
            else if account.imap.tls.useStartTls then
              "STARTTLS"
            else
              "IMAPS";
        }
        // lib.optionalAttrs (account.passwordCommand != null) {
          PassCmd = toString account.passwordCommand;
        }
        // lib.optionalAttrs (account.imap.tls.certificatesFile != null) {
          CertificateFile = toString account.imap.tls.certificatesFile;
        }
        // lib.optionalAttrs (account.imap.port != null) {
          Port = account.imap.port;
        }
      ))

      (mbsyncSection "IMAPStore ${mbsyncNames.servers.remote}" {
        Account = mbsyncNames.base;
      })

      (mbsyncSection "MaildirStore ${mbsyncNames.servers.local}" {
        Path = "${account.maildir.absPath}/";
        Inbox = "${account.maildir.absPath}/${account.folders.inbox}";
        SubFolders = "Verbatim";
      })

      (mbsyncSection "Channel ${mbsyncNames.channels.quick}" {
        Far = ":${mbsyncNames.servers.remote}:\"INBOX\"";
        Near = ":${mbsyncNames.servers.local}:\"INBOX\"";
        Create = "Near";
        Expunge = "None";
        Sync = [
          "Pull"
          "New"
        ];
        SyncState = "*";
      })

      (mbsyncSection "Channel ${mbsyncNames.channels.full}" {
        Far = ":${mbsyncNames.servers.remote}:";
        Near = ":${mbsyncNames.servers.local}:";
        Patterns = account.mbsync.patterns;
        Create = "Near";
        Expunge = "Both";
        Sync = "All";
        SyncState = "*";
      })
    ];

  accounts = lib.filter (
    account: account.enable && account.mbsync.enable
  ) lib.attrValues config.accounts.email.accounts;
in
{
  config = lib.mkIf config.communication.neomutt.enable {
    programs.mbsync.enable = true;

    xdg.configFile."mbsync/.mbsyncrc".text = lib.concatStringsSep "\n" (map mbsyncAccount accounts);
  };
}