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 procPackages = mkOption {
51 type = types.listOf types.path;
52 default = [];
53 description = ''
54 Packages to search for filters, tables, queues, and schedulers.
55
56 Add OpenSMTPD-extras here if you want to use the filters, etc. from
57 that package.
58 '';
59 };
60 };
61
62 };
63
64
65 ###### implementation
66
67 config = mkIf config.services.opensmtpd.enable {
68 users.extraGroups = {
69 smtpd.gid = config.ids.gids.smtpd;
70 smtpq.gid = config.ids.gids.smtpq;
71 };
72
73 users.extraUsers = {
74 smtpd = {
75 description = "OpenSMTPD process user";
76 uid = config.ids.uids.smtpd;
77 group = "smtpd";
78 };
79 smtpq = {
80 description = "OpenSMTPD queue user";
81 uid = config.ids.uids.smtpq;
82 group = "smtpq";
83 };
84 };
85
86 systemd.services.opensmtpd = let
87 procEnv = pkgs.buildEnv {
88 name = "opensmtpd-procs";
89 paths = [ opensmtpd ] ++ cfg.procPackages;
90 pathsToLink = [ "/libexec/opensmtpd" ];
91 };
92 in {
93 wantedBy = [ "multi-user.target" ];
94 wants = [ "network.target" ];
95 after = [ "network.target" ];
96 preStart = "mkdir -p /var/spool";
97 serviceConfig.ExecStart = "${opensmtpd}/sbin/smtpd -d -f ${conf} ${args}";
98 environment.OPENSMTPD_PROC_PATH = "${procEnv}/libexec/opensmtpd";
99 };
100
101 environment.systemPackages = [ (pkgs.runCommand "opensmtpd-sendmail" {} ''
102 mkdir -p $out/bin
103 ln -s ${opensmtpd}/sbin/smtpctl $out/bin/sendmail
104 '') ];
105 };
106}