|
| 1 | +{ config, lib, pkgs, ... }: |
| 2 | + |
| 3 | +with lib; |
| 4 | + |
| 5 | +{ |
| 6 | + |
| 7 | + options = { |
| 8 | + |
| 9 | + services.nullmailer = { |
| 10 | + enable = mkOption { |
| 11 | + type = types.bool; |
| 12 | + default = false; |
| 13 | + description = "Whether to enable nullmailer daemon."; |
| 14 | + }; |
| 15 | + |
| 16 | + user = mkOption { |
| 17 | + type = types.string; |
| 18 | + default = "nullmailer"; |
| 19 | + description = '' |
| 20 | + User to use to run nullmailer-send. |
| 21 | + ''; |
| 22 | + }; |
| 23 | + |
| 24 | + group = mkOption { |
| 25 | + type = types.string; |
| 26 | + default = "nullmailer"; |
| 27 | + description = '' |
| 28 | + Group to use to run nullmailer-send. |
| 29 | + ''; |
| 30 | + }; |
| 31 | + |
| 32 | + setSendmail = mkOption { |
| 33 | + type = types.bool; |
| 34 | + default = true; |
| 35 | + description = "Whether to set the system sendmail to nullmailer's."; |
| 36 | + }; |
| 37 | + |
| 38 | + config = { |
| 39 | + adminaddr = mkOption { |
| 40 | + type = types.nullOr types.str; |
| 41 | + default = null; |
| 42 | + description = '' |
| 43 | + If set, all recipients to users at either "localhost" (the literal string) |
| 44 | + or the canonical host name (from the me control attribute) are remapped to this address. |
| 45 | + This is provided to allow local daemons to be able to send email to |
| 46 | + "somebody@localhost" and have it go somewhere sensible instead of being bounced |
| 47 | + by your relay host. To send to multiple addresses, |
| 48 | + put them all on one line separated by a comma. |
| 49 | + ''; |
| 50 | + }; |
| 51 | + |
| 52 | + allmailfrom = mkOption { |
| 53 | + type = types.nullOr types.str; |
| 54 | + default = null; |
| 55 | + description = '' |
| 56 | + If set, content will override the envelope sender on all messages. |
| 57 | + ''; |
| 58 | + }; |
| 59 | + |
| 60 | + defaultdomain = mkOption { |
| 61 | + type = types.nullOr types.str; |
| 62 | + default = null; |
| 63 | + description = '' |
| 64 | + The content of this attribute is appended to any host name that |
| 65 | + does not contain a period (except localhost), including defaulthost |
| 66 | + and idhost. Defaults to the value of the me attribute, if it exists, |
| 67 | + otherwise the literal name defauldomain. |
| 68 | + ''; |
| 69 | + }; |
| 70 | + |
| 71 | + defaulthost = mkOption { |
| 72 | + type = types.nullOr types.str; |
| 73 | + default = null; |
| 74 | + description = '' |
| 75 | + The content of this attribute is appended to any address that |
| 76 | + is missing a host name. Defaults to the value of the me control |
| 77 | + attribute, if it exists, otherwise the literal name defaulthost. |
| 78 | + ''; |
| 79 | + }; |
| 80 | + |
| 81 | + doublebounceto = mkOption { |
| 82 | + type = types.nullOr types.str; |
| 83 | + default = null; |
| 84 | + description = '' |
| 85 | + If the original sender was empty (the original message was a |
| 86 | + delivery status or disposition notification), the double bounce |
| 87 | + is sent to the address in this attribute. |
| 88 | + ''; |
| 89 | + }; |
| 90 | + |
| 91 | + helohost = mkOption { |
| 92 | + type = types.nullOr types.str; |
| 93 | + default = null; |
| 94 | + description = '' |
| 95 | + Sets the environment variable $HELOHOST which is used by the |
| 96 | + SMTP protocol module to set the parameter given to the HELO command. |
| 97 | + Defaults to the value of the me configuration attribute. |
| 98 | + ''; |
| 99 | + }; |
| 100 | + |
| 101 | + idhost = mkOption { |
| 102 | + type = types.nullOr types.str; |
| 103 | + default = null; |
| 104 | + description = '' |
| 105 | + The content of this attribute is used when building the message-id |
| 106 | + string for the message. Defaults to the canonicalized value of defaulthost. |
| 107 | + ''; |
| 108 | + }; |
| 109 | + |
| 110 | + maxpause = mkOption { |
| 111 | + type = types.nullOr types.str; |
| 112 | + default = null; |
| 113 | + description = '' |
| 114 | + The maximum time to pause between successive queue runs, in seconds. |
| 115 | + Defaults to 24 hours (86400). |
| 116 | + ''; |
| 117 | + }; |
| 118 | + |
| 119 | + me = mkOption { |
| 120 | + type = types.nullOr types.str; |
| 121 | + default = null; |
| 122 | + description = '' |
| 123 | + The fully-qualifiled host name of the computer running nullmailer. |
| 124 | + Defaults to the literal name me. |
| 125 | + ''; |
| 126 | + }; |
| 127 | + |
| 128 | + pausetime = mkOption { |
| 129 | + type = types.nullOr types.str; |
| 130 | + default = null; |
| 131 | + description = '' |
| 132 | + The minimum time to pause between successive queue runs when there |
| 133 | + are messages in the queue, in seconds. Defaults to 1 minute (60). |
| 134 | + Each time this timeout is reached, the timeout is doubled to a |
| 135 | + maximum of maxpause. After new messages are injected, the timeout |
| 136 | + is reset. If this is set to 0, nullmailer-send will exit |
| 137 | + immediately after going through the queue once (one-shot mode). |
| 138 | + ''; |
| 139 | + }; |
| 140 | + |
| 141 | + remotes = mkOption { |
| 142 | + type = types.nullOr types.str; |
| 143 | + default = null; |
| 144 | + description = '' |
| 145 | + If set, content will override the envelope sender on all messages. |
| 146 | + ''; |
| 147 | + }; |
| 148 | + |
| 149 | + sendtimeout = mkOption { |
| 150 | + type = types.nullOr types.str; |
| 151 | + default = null; |
| 152 | + description = '' |
| 153 | + The time to wait for a remote module listed above to complete sending |
| 154 | + a message before killing it and trying again, in seconds. |
| 155 | + Defaults to 1 hour (3600). If this is set to 0, nullmailer-send |
| 156 | + will wait forever for messages to complete sending. |
| 157 | + ''; |
| 158 | + }; |
| 159 | + }; |
| 160 | + }; |
| 161 | + }; |
| 162 | + |
| 163 | + config = let |
| 164 | + cfg = config.services.nullmailer; |
| 165 | + in mkIf cfg.enable { |
| 166 | + |
| 167 | + environment = { |
| 168 | + systemPackages = [ pkgs.nullmailer ]; |
| 169 | + etc = let |
| 170 | + getval = attr: builtins.getAttr attr cfg.config; |
| 171 | + attrs = builtins.attrNames cfg.config; |
| 172 | + attrs' = builtins.filter (attr: ! isNull (getval attr)) attrs; |
| 173 | + in foldl' (as: attr: as // { "nullmailer/${attr}".text = getval attr; }) {} attrs'; |
| 174 | + }; |
| 175 | + |
| 176 | + users = { |
| 177 | + extraUsers = singleton { |
| 178 | + name = cfg.user; |
| 179 | + description = "Nullmailer relay-only mta user"; |
| 180 | + group = cfg.group; |
| 181 | + }; |
| 182 | + |
| 183 | + extraGroups = singleton { |
| 184 | + name = cfg.group; |
| 185 | + }; |
| 186 | + }; |
| 187 | + |
| 188 | + systemd.services.nullmailer = { |
| 189 | + description = "nullmailer"; |
| 190 | + wantedBy = [ "multi-user.target" ]; |
| 191 | + after = [ "network.target" ]; |
| 192 | + |
| 193 | + preStart = '' |
| 194 | + mkdir -p /var/spool/nullmailer/{queue,tmp} |
| 195 | + rm -f var/spool/nullmailer/trigger && mkfifo -m 660 /var/spool/nullmailer/trigger |
| 196 | + chown ${cfg.user} /var/spool/nullmailer/* |
| 197 | + ''; |
| 198 | + |
| 199 | + serviceConfig = { |
| 200 | + User = cfg.user; |
| 201 | + Group = cfg.group; |
| 202 | + PermissionsStartOnly=true; |
| 203 | + ExecStart = "${pkgs.nullmailer}/bin/nullmailer-send"; |
| 204 | + Restart = "always"; |
| 205 | + }; |
| 206 | + }; |
| 207 | + |
| 208 | + services.mail.sendmailSetuidWrapper = mkIf cfg.setSendmail { |
| 209 | + program = "sendmail"; |
| 210 | + source = "${pkgs.nullmailer}/bin/sendmail"; |
| 211 | + owner = cfg.user; |
| 212 | + group = cfg.group; |
| 213 | + setuid = true; |
| 214 | + setgid = true; |
| 215 | + }; |
| 216 | + }; |
| 217 | +} |
0 commit comments