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

with lib;

let

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

  renderAccountConfig = account:
    with account;
    let
      passCmd = concatMapStringsSep ", " (x: "'${x}'") passwordCommand;
      renderedMailboxes =
        concatMapStringsSep ", " (x: "'${x}'") getmail.mailboxes;
      retrieverType = if imap.tls.enable then
        "SimpleIMAPSSLRetriever"
      else
        "SimpleIMAPRetriever";
      destination = if getmail.destinationCommand != null then {
        destinationType = "MDA_external";
        destinationPath = getmail.destinationCommand;
      } else {
        destinationType = "Maildir";
        destinationPath = "${maildir.absPath}/";
      };
      renderGetmailBoolean = v: if v then "true" else "false";
    in ''
      # Generated by Home-Manager.
      [retriever]
      type = ${retrieverType}
      server = ${imap.host}
      ${optionalString (imap.port != null) "port = ${toString imap.port}"}
      username = ${userName}
      password_command = (${passCmd})
      mailboxes = ( ${renderedMailboxes} )

      [destination]
      type = ${destination.destinationType}
      path = ${destination.destinationPath}

      [options]
      delete = ${renderGetmailBoolean getmail.delete}
      read_all = ${renderGetmailBoolean getmail.readAll}
    '';
  getmailEnabled = length (filter (a: a.getmail.enable) accounts) > 0;
  # Watch out! This is used by the getmail.service too!
  renderConfigFilepath = a:
    ".getmail/getmail${if a.primary then "rc" else a.name}";

in {
  options = {
    accounts.email.accounts = mkOption {
      type = with types; attrsOf (submodule (import ./getmail-accounts.nix));
    };
  };

  config = mkIf getmailEnabled {
    home.file = foldl' (a: b: a // b) { }
      (map (a: { "${renderConfigFilepath a}".text = renderAccountConfig a; })
        accounts);
  };
}