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

with lib;

let

  cfg = config.programs.msmtp;

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

  onOff = p: if p then "on" else "off";

  accountStr = account: with account;
    concatStringsSep "\n" (
      [ "account ${name}" ]
      ++ mapAttrsToList (n: v: n + " " + v) (
        {
          host = smtp.host;
          from = address;
          auth = "on";
          user = userName;
          tls = onOff smtp.tls.enable;
          tls_starttls = onOff smtp.tls.useStartTls;
          tls_trust_file = smtp.tls.certificatesFile;
        }
        // optionalAttrs (msmtp.tls.fingerprint != null) {
          tls_fingerprint = msmtp.tls.fingerprint;
        }
        // optionalAttrs (smtp.port != null) {
          port = toString smtp.port;
        }
        // optionalAttrs (passwordCommand != null) {
          # msmtp requires the password to finish with a newline.
          passwordeval = ''${pkgs.bash}/bin/bash -c "${toString passwordCommand}; echo"'';
        }
        // msmtp.extraConfig
      )
      ++ optional primary "\naccount default : ${name}"
    );

  configFile = mailAccounts: ''
    # Generated by Home Manager.

    ${cfg.extraConfig}

    ${concatStringsSep "\n\n" (map accountStr mailAccounts)}
  '';

in

{

  options = {
    programs.msmtp = {
      enable = mkEnableOption "msmtp";

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Extra configuration lines to add to <filename>~/.msmtprc</filename>.
          See <link xlink:href="https://marlam.de/msmtp/msmtprc.txt"/> for examples.
        '';
      };
    };
  };

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

    xdg.configFile."msmtp/config".text = configFile msmtpAccounts;

    home.sessionVariables =  {
      MSMTP_QUEUE = "${config.xdg.dataHome}/msmtp/queue";
      MSMTP_LOG = "${config.xdg.dataHome}/msmtp/queue.log";
    };
  };
}