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

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

  genAccount =
    a:
    let
      mbsyncNames = (import ../lib.nix inputs).mbsyncNamesFromName a.name;

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

      full = {
        Far = ":${mbsyncNames.servers.remote}:";
        Near = ":${mbsyncNames.servers.local}:";
        Patterns = a.mbsync.patterns;
        Create = "Near";
        Expunge = "Both";
        Sync = "All";
        SyncState = "*";
      };
    in
    genSection "IMAPAccount ${mbsyncNames.base}" (
      {
        Host = a.imap.host;
        User = a.userName;
        PipelineDepth = 50;
        TLSType =
          if !a.imap.tls.enable then
            "None"
          else if a.imap.tls.useStartTls then
            "STARTTLS"
          else
            "IMAPS";
      }
      // lib.optionalAttrs (a.passwordCommand != null) {
        PassCmd = toString a.passwordCommand;
      }
      // lib.optionalAttrs (a.imap.tls.certificatesFile != null) {
        CertificateFile = toString a.imap.tls.certificatesFile;
      }
      // lib.optionalAttrs (a.imap.port != null) {
        Port = a.imap.port;
      }
    )

    + "\n"
    + genSection "IMAPStore ${mbsyncNames.servers.remote}" {
      Account = mbsyncNames.base;
    }

    + "\n"
    + genSection "MaildirStore ${mbsyncNames.servers.local}" {
      Path = "${a.maildir.absPath}/";
      Inbox = "${a.maildir.absPath}/${a.folders.inbox}";
      SubFolders = "Verbatim";
    }

    + "\n"
    + genSection "Channel ${mbsyncNames.channels.quick}" quick

    + "\n"
    + genSection "Channel ${mbsyncNames.channels.full}" full

    + "\n";

  accounts = lib.filter (a: a.enable && a.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 genAccount accounts);
  };
}