1# Configuration for `ssmtp', a trivial mail transfer agent that can
2# replace sendmail/postfix on simple systems. It delivers email
3# directly to an SMTP server defined in its configuration file, without
4# queueing mail locally.
5
6{ config, lib, pkgs, ... }:
7
8with lib;
9
10let
11 cfg = config.services.ssmtp;
12
13in
14{
15
16 imports = [
17 (mkRenamedOptionModule [ "networking" "defaultMailServer" "directDelivery" ] [ "services" "ssmtp" "enable" ])
18 (mkRenamedOptionModule [ "networking" "defaultMailServer" "hostName" ] [ "services" "ssmtp" "hostName" ])
19 (mkRenamedOptionModule [ "networking" "defaultMailServer" "domain" ] [ "services" "ssmtp" "domain" ])
20 (mkRenamedOptionModule [ "networking" "defaultMailServer" "root" ] [ "services" "ssmtp" "root" ])
21 (mkRenamedOptionModule [ "networking" "defaultMailServer" "useTLS" ] [ "services" "ssmtp" "useTLS" ])
22 (mkRenamedOptionModule [ "networking" "defaultMailServer" "useSTARTTLS" ] [ "services" "ssmtp" "useSTARTTLS" ])
23 (mkRenamedOptionModule [ "networking" "defaultMailServer" "authUser" ] [ "services" "ssmtp" "authUser" ])
24 (mkRenamedOptionModule [ "networking" "defaultMailServer" "authPassFile" ] [ "services" "ssmtp" "authPassFile" ])
25 (mkRenamedOptionModule [ "networking" "defaultMailServer" "setSendmail" ] [ "services" "ssmtp" "setSendmail" ])
26
27 (mkRemovedOptionModule [ "networking" "defaultMailServer" "authPass" ] "authPass has been removed since it leaks the clear-text password into the world-readable store. Use authPassFile instead and make sure it's not a store path")
28 (mkRemovedOptionModule [ "services" "ssmtp" "authPass" ] "authPass has been removed since it leaks the clear-text password into the world-readable store. Use authPassFile instead and make sure it's not a store path")
29 ];
30
31 options = {
32
33 services.ssmtp = {
34
35 enable = mkOption {
36 type = types.bool;
37 default = false;
38 description = ''
39 Use the trivial Mail Transfer Agent (MTA)
40 <command>ssmtp</command> package to allow programs to send
41 e-mail. If you don't want to run a “real” MTA like
42 <command>sendmail</command> or <command>postfix</command> on
43 your machine, set this option to <literal>true</literal>, and
44 set the option
45 <option>services.ssmtp.hostName</option> to the
46 host name of your preferred mail server.
47 '';
48 };
49
50 settings = mkOption {
51 type = with types; attrsOf (oneOf [ bool str ]);
52 default = {};
53 description = ''
54 <citerefentry><refentrytitle>ssmtp</refentrytitle><manvolnum>5</manvolnum></citerefentry> configuration. Refer
55 to <link xlink:href="https://linux.die.net/man/5/ssmtp.conf"/> for details on supported values.
56 '';
57 example = literalExample ''
58 {
59 Debug = true;
60 FromLineOverride = false;
61 }
62 '';
63 };
64
65 hostName = mkOption {
66 type = types.str;
67 example = "mail.example.org";
68 description = ''
69 The host name of the default mail server to use to deliver
70 e-mail. Can also contain a port number (ex: mail.example.org:587),
71 defaults to port 25 if no port is given.
72 '';
73 };
74
75 root = mkOption {
76 type = types.str;
77 default = "";
78 example = "root@example.org";
79 description = ''
80 The e-mail to which mail for users with UID < 1000 is forwarded.
81 '';
82 };
83
84 domain = mkOption {
85 type = types.str;
86 default = "";
87 example = "example.org";
88 description = ''
89 The domain from which mail will appear to be sent.
90 '';
91 };
92
93 useTLS = mkOption {
94 type = types.bool;
95 default = false;
96 description = ''
97 Whether TLS should be used to connect to the default mail
98 server.
99 '';
100 };
101
102 useSTARTTLS = mkOption {
103 type = types.bool;
104 default = false;
105 description = ''
106 Whether the STARTTLS should be used to connect to the default
107 mail server. (This is needed for TLS-capable mail servers
108 running on the default SMTP port 25.)
109 '';
110 };
111
112 authUser = mkOption {
113 type = types.str;
114 default = "";
115 example = "foo@example.org";
116 description = ''
117 Username used for SMTP auth. Leave blank to disable.
118 '';
119 };
120
121 authPassFile = mkOption {
122 type = types.nullOr types.str;
123 default = null;
124 example = "/run/keys/ssmtp-authpass";
125 description = ''
126 Path to a file that contains the password used for SMTP auth. The file
127 should not contain a trailing newline, if the password does not contain one.
128 This file should be readable by the users that need to execute ssmtp.
129 '';
130 };
131
132 setSendmail = mkOption {
133 type = types.bool;
134 default = true;
135 description = "Whether to set the system sendmail to ssmtp's.";
136 };
137
138 };
139
140 };
141
142
143 config = mkIf cfg.enable {
144
145 assertions = [
146 {
147 assertion = cfg.useSTARTTLS -> cfg.useTLS;
148 message = "services.ssmtp.useSTARTTLS has no effect without services.ssmtp.useTLS";
149 }
150 ];
151
152 services.ssmtp.settings = mkMerge [
153 ({
154 MailHub = cfg.hostName;
155 FromLineOverride = mkDefault true;
156 UseTLS = cfg.useTLS;
157 UseSTARTTLS = cfg.useSTARTTLS;
158 })
159 (mkIf (cfg.root != "") { root = cfg.root; })
160 (mkIf (cfg.domain != "") { rewriteDomain = cfg.domain; })
161 (mkIf (cfg.authUser != "") { AuthUser = cfg.authUser; })
162 (mkIf (cfg.authPassFile != null) { AuthPassFile = cfg.authPassFile; })
163 ];
164
165 # careful here: ssmtp REQUIRES all config lines to end with a newline char!
166 environment.etc."ssmtp/ssmtp.conf".text = with generators; toKeyValue {
167 mkKeyValue = mkKeyValueDefault {
168 mkValueString = value:
169 if value == true then "YES"
170 else if value == false then "NO"
171 else mkValueStringDefault {} value
172 ;
173 } "=";
174 } cfg.settings;
175
176 environment.systemPackages = [pkgs.ssmtp];
177
178 services.mail.sendmailSetuidWrapper = mkIf cfg.setSendmail {
179 program = "sendmail";
180 source = "${pkgs.ssmtp}/bin/sendmail";
181 setuid = false;
182 setgid = false;
183 };
184
185 };
186
187}