| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   rspamdCfg = config.services.rspamd; | 
					
						
							| 
									
										
										
										
											2017-03-17 23:01:24 +01:00
										 |  |  |   postfixCfg = config.services.postfix; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |   cfg = config.services.rmilter; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-09 22:45:05 +02:00
										 |  |  |   inetSocket = addr: port: "inet:${addr}:${toString port}"; | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |   unixSocket = sock: "unix:${sock}"; | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |   systemdSocket = if cfg.bindSocket.type == "unix" then cfg.bindSocket.path | 
					
						
							|  |  |  |     else "${cfg.bindSocket.address}:${toString cfg.bindSocket.port}"; | 
					
						
							|  |  |  |   rmilterSocket = if cfg.bindSocket.type == "unix" then unixSocket cfg.bindSocket.path | 
					
						
							|  |  |  |     else inetSocket cfg.bindSocket.address cfg.bindSocket.port; | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |   rmilterConf = ''
 | 
					
						
							| 
									
										
										
										
											2017-01-13 15:23:34 -07:00
										 |  |  |     pidfile = /run/rmilter/rmilter.pid; | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |     bind_socket = ${if cfg.socketActivation then "fd:3" else rmilterSocket}; | 
					
						
							| 
									
										
										
										
											2017-01-13 15:23:34 -07:00
										 |  |  |     tempdir = /tmp; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |   '' + (with cfg.rspamd; if enable then '' | 
					
						
							| 
									
										
										
										
											2017-01-13 15:23:34 -07:00
										 |  |  |     spamd { | 
					
						
							|  |  |  |       servers = ${concatStringsSep ", " servers}; | 
					
						
							|  |  |  |       connect_timeout = 1s; | 
					
						
							|  |  |  |       results_timeout = 20s; | 
					
						
							|  |  |  |       error_time = 10; | 
					
						
							|  |  |  |       dead_time = 300; | 
					
						
							|  |  |  |       maxerrors = 10; | 
					
						
							|  |  |  |       reject_message = "${rejectMessage}"; | 
					
						
							|  |  |  |       ${optionalString (length whitelist != 0)  "whitelist = ${concatStringsSep ", " whitelist};"} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # rspamd_metric - metric for using with rspamd | 
					
						
							|  |  |  |       # Default: "default" | 
					
						
							|  |  |  |       rspamd_metric = "default"; | 
					
						
							|  |  |  |       ${extraConfig} | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |   '' else "") + cfg.extraConfig;
 | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   rmilterConfigFile = pkgs.writeText "rmilter.conf" rmilterConf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | in | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### interface | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     services.rmilter = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       enable = mkOption { | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |         type = types.bool; | 
					
						
							| 
									
										
										
										
											2018-10-12 17:28:48 +03:00
										 |  |  |         default = false; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |         description = "Whether to run the rmilter daemon."; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       debug = mkOption { | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |         type = types.bool; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |         default = false; | 
					
						
							|  |  |  |         description = "Whether to run the rmilter daemon in debug mode."; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       user = mkOption { | 
					
						
							|  |  |  |         type = types.string; | 
					
						
							|  |  |  |         default = "rmilter"; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           User to use when no root privileges are required. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |        }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       group = mkOption { | 
					
						
							|  |  |  |         type = types.string; | 
					
						
							|  |  |  |         default = "rmilter"; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Group to use when no root privileges are required. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |        }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |       bindSocket.type = mkOption { | 
					
						
							|  |  |  |         type = types.enum [ "unix" "inet" ]; | 
					
						
							|  |  |  |         default = "unix"; | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  |         description = ''
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |           What kind of socket rmilter should listen on. Either "unix" | 
					
						
							|  |  |  |           for an Unix domain socket or "inet" for a TCP socket. | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       bindSocket.path = mkOption { | 
					
						
							|  |  |  |        type = types.str; | 
					
						
							| 
									
										
										
										
											2018-09-10 19:43:17 +03:00
										 |  |  |        default = "/run/rmilter.sock"; | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |        description = ''
 | 
					
						
							|  |  |  |           Path to Unix domain socket to listen on. | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |       bindSocket.address = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							| 
									
										
										
										
											2018-09-09 22:45:05 +02:00
										 |  |  |         default = "[::1]"; | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |         example = "0.0.0.0"; | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  |         description = ''
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |           Inet address to listen on. | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       bindSocket.port = mkOption { | 
					
						
							|  |  |  |         type = types.int; | 
					
						
							|  |  |  |         default = 11990; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Inet port to listen on. | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       socketActivation = mkOption { | 
					
						
							|  |  |  |         type = types.bool; | 
					
						
							|  |  |  |         default = true; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Enable systemd socket activation for rmilter. | 
					
						
							| 
									
										
										
										
											2017-01-13 15:23:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           Disabling socket activation is not recommended when a Unix | 
					
						
							|  |  |  |           domain socket is used and could lead to incorrect | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |           permissions. | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       rspamd = { | 
					
						
							|  |  |  |         enable = mkOption { | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |           type = types.bool; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |           default = rspamdCfg.enable; | 
					
						
							|  |  |  |           description = "Whether to use rspamd to filter mails"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         servers = mkOption { | 
					
						
							|  |  |  |           type = types.listOf types.str; | 
					
						
							| 
									
										
										
										
											2016-07-28 06:03:01 +02:00
										 |  |  |           default = ["r:/run/rspamd/rspamd.sock"]; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |           description = ''
 | 
					
						
							|  |  |  |             Spamd socket definitions. | 
					
						
							|  |  |  |             Is server name is prefixed with r: it is rspamd server. | 
					
						
							|  |  |  |           '';
 | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         whitelist = mkOption { | 
					
						
							|  |  |  |           type = types.listOf types.str; | 
					
						
							|  |  |  |           default = [ ]; | 
					
						
							|  |  |  |           description = "list of ips or nets that should be not checked with spamd"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         rejectMessage = mkOption { | 
					
						
							|  |  |  |           type = types.str; | 
					
						
							|  |  |  |           default = "Spam message rejected; If this is not spam contact abuse"; | 
					
						
							|  |  |  |           description = "reject message for spam"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         extraConfig = mkOption { | 
					
						
							|  |  |  |           type = types.lines; | 
					
						
							|  |  |  |           default = ""; | 
					
						
							|  |  |  |           description = "Custom snippet to append to end of `spamd' section"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       extraConfig = mkOption { | 
					
						
							|  |  |  |         type = types.lines; | 
					
						
							|  |  |  |         default = ""; | 
					
						
							|  |  |  |         description = "Custom snippet to append to rmilter config"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       postfix = { | 
					
						
							|  |  |  |         enable = mkOption { | 
					
						
							|  |  |  |           type = types.bool; | 
					
						
							|  |  |  |           default = false; | 
					
						
							|  |  |  |           description = "Add rmilter to postfix main.conf"; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         configFragment = mkOption { | 
					
						
							|  |  |  |           type = types.str; | 
					
						
							|  |  |  |           description = "Addon to postfix configuration"; | 
					
						
							|  |  |  |           default = ''
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |             smtpd_milters = ${rmilterSocket} | 
					
						
							|  |  |  |             milter_protocol = 6 | 
					
						
							|  |  |  |             milter_mail_macros = i {mail_addr} {client_addr} {client_name} {auth_authen} | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |           '';
 | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### implementation | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |   config = mkMerge [ | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |     (mkIf cfg.enable { | 
					
						
							| 
									
										
										
										
											2018-09-11 06:20:55 +03:00
										 |  |  |       warnings = [ | 
					
						
							|  |  |  |         ''`config.services.rmilter' is deprecated, `rmilter' deprecated and unsupported by upstream, and will be removed from next releases. Use built-in rspamd milter instead.'' | 
					
						
							|  |  |  |       ]; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 01:58:35 +02:00
										 |  |  |       users.users = singleton { | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |         name = cfg.user; | 
					
						
							|  |  |  |         description = "rmilter daemon"; | 
					
						
							|  |  |  |         uid = config.ids.uids.rmilter; | 
					
						
							|  |  |  |         group = cfg.group; | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 01:58:35 +02:00
										 |  |  |       users.groups = singleton { | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |         name = cfg.group; | 
					
						
							|  |  |  |         gid = config.ids.gids.rmilter; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |       systemd.services.rmilter = { | 
					
						
							|  |  |  |         description = "Rmilter Service"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  |         after = [ "network.target" ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         serviceConfig = { | 
					
						
							|  |  |  |           ExecStart = "${pkgs.rmilter}/bin/rmilter ${optionalString cfg.debug "-d"} -n -c ${rmilterConfigFile}"; | 
					
						
							|  |  |  |           ExecReload = "${pkgs.coreutils}/bin/kill -USR1 $MAINPID"; | 
					
						
							|  |  |  |           User = cfg.user; | 
					
						
							|  |  |  |           Group = cfg.group; | 
					
						
							|  |  |  |           PermissionsStartOnly = true; | 
					
						
							|  |  |  |           Restart = "always"; | 
					
						
							|  |  |  |           RuntimeDirectory = "rmilter"; | 
					
						
							| 
									
										
										
										
											2017-03-17 23:01:24 +01:00
										 |  |  |           RuntimeDirectoryMode = "0750"; | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-12 18:11:40 +02:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |       systemd.sockets.rmilter = mkIf cfg.socketActivation { | 
					
						
							|  |  |  |         description = "Rmilter service socket"; | 
					
						
							|  |  |  |         wantedBy = [ "sockets.target" ]; | 
					
						
							|  |  |  |         socketConfig = { | 
					
						
							|  |  |  |           ListenStream = systemdSocket; | 
					
						
							|  |  |  |           SocketUser = cfg.user; | 
					
						
							|  |  |  |           SocketGroup = cfg.group; | 
					
						
							| 
									
										
										
										
											2017-03-17 23:01:24 +01:00
										 |  |  |           SocketMode = "0660"; | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |         }; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-17 23:01:24 +01:00
										 |  |  |     (mkIf (cfg.enable && cfg.rspamd.enable && rspamdCfg.enable) { | 
					
						
							| 
									
										
										
										
											2018-06-30 01:58:35 +02:00
										 |  |  |       users.users.${cfg.user}.extraGroups = [ rspamdCfg.group ]; | 
					
						
							| 
									
										
										
										
											2017-03-17 23:01:24 +01:00
										 |  |  |     }) | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-17 23:01:24 +01:00
										 |  |  |     (mkIf (cfg.enable && cfg.postfix.enable) { | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |       services.postfix.extraConfig = cfg.postfix.configFragment; | 
					
						
							| 
									
										
										
										
											2018-06-30 01:58:35 +02:00
										 |  |  |       users.users.${postfixCfg.user}.extraGroups = [ cfg.group ]; | 
					
						
							| 
									
										
										
										
											2017-01-28 02:46:16 +01:00
										 |  |  |     }) | 
					
						
							|  |  |  |   ]; | 
					
						
							| 
									
										
										
										
											2016-01-14 11:17:27 +02:00
										 |  |  | } |