1{ config, lib, pkgs, ... }:
2
3with pkgs;
4with lib;
5
6let
7
8 cfg = config.services.opensmtpd;
9 conf = writeText "smtpd.conf" cfg.serverConfiguration;
10 args = concatStringsSep " " cfg.extraServerArgs;
11
12 sendmail = pkgs.runCommand "opensmtpd-sendmail" {} ''
13 mkdir -p $out/bin
14 ln -s ${opensmtpd}/sbin/smtpctl $out/bin/sendmail
15 '';
16
17in {
18
19 ###### interface
20
21 options = {
22
23 services.opensmtpd = {
24
25 enable = mkOption {
26 type = types.bool;
27 default = false;
28 description = "Whether to enable the OpenSMTPD server.";
29 };
30
31 addSendmailToSystemPath = mkOption {
32 type = types.bool;
33 default = true;
34 description = ''
35 Whether to add OpenSMTPD's sendmail binary to the
36 system path or not.
37 '';
38 };
39
40 extraServerArgs = mkOption {
41 type = types.listOf types.str;
42 default = [];
43 example = [ "-v" "-P mta" ];
44 description = ''
45 Extra command line arguments provided when the smtpd process
46 is started.
47 '';
48 };
49
50 serverConfiguration = mkOption {
51 type = types.string;
52 default = "";
53 example = ''
54 listen on lo
55 accept for any deliver to lmtp localhost:24
56 '';
57 description = ''
58 The contents of the smtpd.conf configuration file. See the
59 OpenSMTPD documentation for syntax information. If this option
60 is left empty, the OpenSMTPD server will not start.
61 '';
62 };
63
64 procPackages = mkOption {
65 type = types.listOf types.path;
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 {
82 users.extraGroups = {
83 smtpd.gid = config.ids.gids.smtpd;
84 smtpq.gid = config.ids.gids.smtpq;
85 };
86
87 users.extraUsers = {
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 systemd.services.opensmtpd = let
101 procEnv = pkgs.buildEnv {
102 name = "opensmtpd-procs";
103 paths = [ opensmtpd ] ++ cfg.procPackages;
104 pathsToLink = [ "/libexec/opensmtpd" ];
105 };
106 in {
107 wantedBy = [ "multi-user.target" ];
108 wants = [ "network.target" ];
109 after = [ "network.target" ];
110 preStart = "mkdir -p /var/spool";
111 serviceConfig.ExecStart = "${opensmtpd}/sbin/smtpd -d -f ${conf} ${args}";
112 environment.OPENSMTPD_PROC_PATH = "${procEnv}/libexec/opensmtpd";
113 };
114
115 environment.systemPackages = mkIf cfg.addSendmailToSystemPath [ sendmail ];
116 };
117}