aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/offlineimap.nix
blob: 4ce12ec0a610aa9a475b3b0932ac296e3ca158ab (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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.offlineimap;

  accounts = filter (a: a.offlineimap.enable)
    (attrValues config.accounts.email.accounts);

  toIni = generators.toINI {
    mkKeyValue = key: value:
      let
        value' = if isBool value then
          (if value then "yes" else "no")
        else
          toString value;
      in "${key} = ${value'}";
  };

  # Generates a script to fetch only a specific account.
  #
  # Note, these scripts are not actually created and installed at the
  # moment. It will need some thinking on whether this is a good idea
  # and whether other modules should have some similar functionality.
  #
  # Perhaps have a single tool `email` that wraps the command?
  # Something like
  #
  #     $ email <account name> <program name> <program args>
  genOfflineImapScript = account:
    with account;
    pkgs.writeShellScriptBin "offlineimap-${name}" ''
      exec ${pkgs.offlineimap}/bin/offlineimap -a${account.name} "$@"
    '';

  accountStr = account:
    with account;
    let
      postSyncHook = optionalAttrs (offlineimap.postSyncHookCommand != "") {
        postsynchook = pkgs.writeShellScriptBin "postsynchook"
          offlineimap.postSyncHookCommand + "/bin/postsynchook";
      };

      localType =
        if account.flavor == "gmail.com" then "GmailMaildir" else "Maildir";

      remoteType = if account.flavor == "gmail.com" then "Gmail" else "IMAP";

      remoteHost =
        optionalAttrs (imap.host != null) { remotehost = imap.host; };

      remotePort =
        optionalAttrs ((imap.port or null) != null) { remoteport = imap.port; };

      ssl = if imap.tls.enable then {
        ssl = true;
        sslcacertfile = imap.tls.certificatesFile;
        starttls = imap.tls.useStartTls;
      } else {
        ssl = false;
      };

      remotePassEval =
        let arglist = concatMapStringsSep "," (x: "'${x}'") passwordCommand;
        in optionalAttrs (passwordCommand != null) {
          remotepasseval = ''get_pass("${name}", [${arglist}])'';
        };
    in toIni {
      "Account ${name}" = {
        localrepository = "${name}-local";
        remoterepository = "${name}-remote";
      } // postSyncHook // offlineimap.extraConfig.account;

      "Repository ${name}-local" = {
        type = localType;
        localfolders = maildir.absPath;
      } // offlineimap.extraConfig.local;

      "Repository ${name}-remote" = {
        type = remoteType;
        remoteuser = userName;
      } // remoteHost // remotePort // remotePassEval // ssl
        // offlineimap.extraConfig.remote;
    };

  extraConfigType = with types; attrsOf (either (either str int) bool);

in {
  options = {
    programs.offlineimap = {
      enable = mkEnableOption "OfflineIMAP";

      pythonFile = mkOption {
        type = types.lines;
        default = ''
          import subprocess

          def get_pass(service, cmd):
              return subprocess.check_output(cmd, )
        '';
        description = ''
          Python code that can then be used in other parts of the
          configuration.
        '';
      };

      extraConfig.general = mkOption {
        type = extraConfigType;
        default = { };
        example = {
          maxage = 30;
          ui = "blinkenlights";
        };
        description = ''
          Extra configuration options added to the
          <option>general</option> section.
        '';
      };

      extraConfig.default = mkOption {
        type = extraConfigType;
        default = { };
        example = { gmailtrashfolder = "[Gmail]/Papierkorb"; };
        description = ''
          Extra configuration options added to the
          <option>DEFAULT</option> section.
        '';
      };

      extraConfig.mbnames = mkOption {
        type = extraConfigType;
        default = { };
        example = literalExample ''
          {
            filename = "~/.config/mutt/mailboxes";
            header = "'mailboxes '";
            peritem = "'+%(accountname)s/%(foldername)s'";
            sep = "' '";
            footer = "'\\n'";
          }
        '';
        description = ''
          Extra configuration options added to the
          <code>mbnames</code> section.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    home.packages = [ pkgs.offlineimap ];

    xdg.configFile."offlineimap/get_settings.py".text = cfg.pythonFile;

    xdg.configFile."offlineimap/config".text = ''
      # Generated by Home Manager.
      # See https://github.com/OfflineIMAP/offlineimap/blob/master/offlineimap.conf
      # for an exhaustive list of options.
    '' + toIni ({
      general = {
        accounts = concatMapStringsSep "," (a: a.name) accounts;
        pythonfile = "${config.xdg.configHome}/offlineimap/get_settings.py";
        metadata = "${config.xdg.dataHome}/offlineimap";
      } // cfg.extraConfig.general;
    } // optionalAttrs (cfg.extraConfig.mbnames != { }) {
      mbnames = { enabled = true; } // cfg.extraConfig.mbnames;
    } // optionalAttrs (cfg.extraConfig.default != { }) {
      DEFAULT = cfg.extraConfig.default;
    }) + "\n" + concatStringsSep "\n" (map accountStr accounts);
  };
}