fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
2.6 KiB93 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.msmtp;
8
9  msmtpAccounts =
10    filter (a: a.msmtp.enable) (attrValues config.accounts.email.accounts);
11
12  onOff = p: if p then "on" else "off";
13
14  accountStr = account:
15    with account;
16    concatStringsSep "\n" ([ "account ${name}" ]
17      ++ mapAttrsToList (n: v: n + " " + v) ({
18        host = smtp.host;
19        from = address;
20        auth = "on";
21        user = userName;
22        tls = onOff smtp.tls.enable;
23        tls_starttls = onOff smtp.tls.useStartTls;
24      } // optionalAttrs (msmtp.tls.fingerprint != null) {
25        tls_fingerprint = msmtp.tls.fingerprint;
26      } // optionalAttrs (smtp.port != null) { port = toString smtp.port; }
27        // optionalAttrs (smtp.tls.certificatesFile != null) {
28          tls_trust_file = smtp.tls.certificatesFile;
29        } // optionalAttrs (passwordCommand != null) {
30          passwordeval = toString passwordCommand;
31        } // msmtp.extraConfig) ++ optional primary "account default : ${name}"
32      ++ map (alias: ''
33
34        account ${alias} : ${name}
35        from ${alias}
36      '') aliases);
37
38  configFile = mailAccounts: ''
39    # Generated by Home Manager.
40
41    ${cfg.extraConfig}
42
43    ${concatStringsSep "\n\n" (map accountStr mailAccounts)}
44
45    ${cfg.extraAccounts}
46  '';
47
48in {
49
50  options = {
51    programs.msmtp = {
52      enable = mkEnableOption "msmtp";
53
54      extraConfig = mkOption {
55        type = types.lines;
56        default = "";
57        description = ''
58          Extra configuration lines to add to {file}`~/.msmtprc`.
59          See <https://marlam.de/msmtp/msmtprc.txt> for examples.
60
61          Note, if running msmtp fails with the error message "account default
62          was already defined" then you probably have an account command here.
63          Account commands should be placed in
64          [](#opt-accounts.email.accounts._name_.msmtp.extraConfig).
65        '';
66      };
67
68      extraAccounts = mkOption {
69        type = types.lines;
70        default = "";
71        description = ''
72          Extra configuration lines to add to the end of {file}`~/.msmtprc`.
73          See <https://marlam.de/msmtp/msmtprc.txt> for examples.
74        '';
75      };
76    };
77
78    accounts.email.accounts = mkOption {
79      type = with types; attrsOf (submodule (import ./msmtp-accounts.nix));
80    };
81  };
82
83  config = mkIf cfg.enable {
84    home.packages = [ pkgs.msmtp ];
85
86    xdg.configFile."msmtp/config".text = configFile msmtpAccounts;
87
88    home.sessionVariables = {
89      MSMTP_QUEUE = "${config.xdg.dataHome}/msmtp/queue";
90      MSMTP_LOG = "${config.xdg.dataHome}/msmtp/queue.log";
91    };
92  };
93}