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