1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.opensmtpd;
8 conf = pkgs.writeText "smtpd.conf" cfg.serverConfiguration;
9 args = concatStringsSep " " cfg.extraServerArgs;
10
11 sendmail = pkgs.runCommand "opensmtpd-sendmail" { preferLocalBuild = true; } ''
12 mkdir -p $out/bin
13 ln -s ${cfg.package}/sbin/smtpctl $out/bin/sendmail
14 '';
15
16in {
17
18 ###### interface
19
20 imports = [
21 (mkRenamedOptionModule [ "services" "opensmtpd" "addSendmailToSystemPath" ] [ "services" "opensmtpd" "setSendmail" ])
22 ];
23
24 options = {
25
26 services.opensmtpd = {
27
28 enable = mkOption {
29 type = types.bool;
30 default = false;
31 description = "Whether to enable the OpenSMTPD server.";
32 };
33
34 package = mkPackageOption pkgs "opensmtpd" { };
35
36 setSendmail = mkOption {
37 type = types.bool;
38 default = true;
39 description = "Whether to set the system sendmail to OpenSMTPD's.";
40 };
41
42 extraServerArgs = mkOption {
43 type = types.listOf types.str;
44 default = [];
45 example = [ "-v" "-P mta" ];
46 description = ''
47 Extra command line arguments provided when the smtpd process
48 is started.
49 '';
50 };
51
52 serverConfiguration = mkOption {
53 type = types.lines;
54 example = ''
55 listen on lo
56 accept for any deliver to lmtp localhost:24
57 '';
58 description = ''
59 The contents of the smtpd.conf configuration file. See the
60 OpenSMTPD documentation for syntax information.
61 '';
62 };
63
64 procPackages = mkOption {
65 type = types.listOf types.package;
66 default = [];
67 description = ''
68 Packages to search for filters, tables, queues, and schedulers.
69
70 Add OpenSMTPD-extras here if you want to use the filters, etc. from
71 that package.
72 '';
73 };
74 };
75
76 };
77
78
79 ###### implementation
80
81 config = mkIf cfg.enable rec {
82 users.groups = {
83 smtpd.gid = config.ids.gids.smtpd;
84 smtpq.gid = config.ids.gids.smtpq;
85 };
86
87 users.users = {
88 smtpd = {
89 description = "OpenSMTPD process user";
90 uid = config.ids.uids.smtpd;
91 group = "smtpd";
92 };
93 smtpq = {
94 description = "OpenSMTPD queue user";
95 uid = config.ids.uids.smtpq;
96 group = "smtpq";
97 };
98 };
99
100 security.wrappers.smtpctl = {
101 owner = "root";
102 group = "smtpq";
103 setuid = false;
104 setgid = true;
105 source = "${cfg.package}/bin/smtpctl";
106 };
107
108 services.mail.sendmailSetuidWrapper = mkIf cfg.setSendmail
109 (security.wrappers.smtpctl // { program = "sendmail"; });
110
111 systemd.tmpfiles.rules = [
112 "d /var/spool/smtpd 711 root - - -"
113 "d /var/spool/smtpd/offline 770 root smtpq - -"
114 "d /var/spool/smtpd/purge 700 smtpq root - -"
115 ];
116
117 systemd.services.opensmtpd = let
118 procEnv = pkgs.buildEnv {
119 name = "opensmtpd-procs";
120 paths = [ cfg.package ] ++ cfg.procPackages;
121 pathsToLink = [ "/libexec/opensmtpd" ];
122 };
123 in {
124 wantedBy = [ "multi-user.target" ];
125 after = [ "network.target" ];
126 serviceConfig.ExecStart = "${cfg.package}/sbin/smtpd -d -f ${conf} ${args}";
127 environment.OPENSMTPD_PROC_PATH = "${procEnv}/libexec/opensmtpd";
128 };
129 };
130}