fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMove platform check into modules5f433eb1

master
2.0 KiB66 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  accounts =
8    filter (a: a.getmail.enable) (attrValues config.accounts.email.accounts);
9
10  renderAccountConfig = account:
11    with account;
12    let
13      passCmd = concatMapStringsSep ", " (x: "'${x}'") passwordCommand;
14      renderedMailboxes = concatMapStrings (x: "'${x}', ") getmail.mailboxes;
15      retrieverType = if imap.tls.enable then
16        "SimpleIMAPSSLRetriever"
17      else
18        "SimpleIMAPRetriever";
19      destination = if getmail.destinationCommand != null then {
20        destinationType = "MDA_external";
21        destinationPath = getmail.destinationCommand;
22      } else {
23        destinationType = "Maildir";
24        destinationPath = "${maildir.absPath}/";
25      };
26      renderGetmailBoolean = v: if v then "true" else "false";
27    in ''
28      # Generated by Home-Manager.
29      [retriever]
30      type = ${retrieverType}
31      server = ${imap.host}
32      ${optionalString (imap.port != null) "port = ${toString imap.port}"}
33      username = ${userName}
34      password_command = (${passCmd})
35      mailboxes = ( ${renderedMailboxes} )
36
37      [destination]
38      type = ${destination.destinationType}
39      path = ${destination.destinationPath}
40
41      [options]
42      delete = ${renderGetmailBoolean getmail.delete}
43      read_all = ${renderGetmailBoolean getmail.readAll}
44    '';
45  getmailEnabled = length (filter (a: a.getmail.enable) accounts) > 0;
46  # Watch out! This is used by the getmail.service too!
47  renderConfigFilepath = a:
48    ".getmail/getmail${if a.primary then "rc" else a.name}";
49
50in {
51  options = {
52    accounts.email.accounts = mkOption {
53      type = with types; attrsOf (submodule (import ./getmail-accounts.nix));
54    };
55  };
56
57  config = mkIf getmailEnabled {
58    assertions = [
59      (hm.assertions.assertPlatform "programs.getmail" pkgs platforms.linux)
60    ];
61
62    home.file = foldl' (a: b: a // b) { }
63      (map (a: { "${renderConfigFilepath a}".text = renderAccountConfig a; })
64        accounts);
65  };
66}