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
12in {
13
14 ###### interface
15
16 options = {
17
18 services.opensmtpd = {
19
20 enable = mkOption {
21 type = types.bool;
22 default = false;
23 description = "Whether to enable the OpenSMTPD server.";
24 };
25
26 extraServerArgs = mkOption {
27 type = types.listOf types.str;
28 default = [];
29 example = [ "-v" "-P mta" ];
30 description = ''
31 Extra command line arguments provided when the smtpd process
32 is started.
33 '';
34 };
35
36 serverConfiguration = mkOption {
37 type = types.string;
38 default = "";
39 example = ''
40 listen on lo
41 accept for any deliver to lmtp localhost:24
42 '';
43 description = ''
44 The contents of the smtpd.conf configuration file. See the
45 OpenSMTPD documentation for syntax information. If this option
46 is left empty, the OpenSMTPD server will not start.
47 '';
48 };
49 };
50
51 };
52
53
54 ###### implementation
55
56 config = mkIf config.services.opensmtpd.enable {
57 users.extraGroups = {
58 smtpd.gid = config.ids.gids.smtpd;
59 smtpq.gid = config.ids.gids.smtpq;
60 };
61
62 users.extraUsers = {
63 smtpd = {
64 description = "OpenSMTPD process user";
65 uid = config.ids.uids.smtpd;
66 group = "smtpd";
67 };
68 smtpq = {
69 description = "OpenSMTPD queue user";
70 uid = config.ids.uids.smtpq;
71 group = "smtpq";
72 };
73 };
74
75 systemd.services.opensmtpd = {
76 wantedBy = [ "multi-user.target" ];
77 wants = [ "network.target" ];
78 after = [ "network.target" ];
79 preStart = "mkdir -p /var/spool";
80 serviceConfig.ExecStart = "${opensmtpd}/sbin/smtpd -d -f ${conf} ${args}";
81 };
82
83 environment.systemPackages = [ (pkgs.runCommand "opensmtpd-sendmail" {} ''
84 mkdir -p $out/bin
85 ln -s ${opensmtpd}/sbin/smtpctl $out/bin/sendmail
86 '') ];
87 };
88}