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