| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | with pkgs; | 
					
						
							| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | with lib; | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cfg = config.services.opensmtpd; | 
					
						
							|  |  |  |   conf = writeText "smtpd.conf" cfg.serverConfiguration; | 
					
						
							|  |  |  |   args = concatStringsSep " " cfg.extraServerArgs; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 16:33:57 +01:00
										 |  |  |   sendmail = pkgs.runCommand "opensmtpd-sendmail" {} ''
 | 
					
						
							|  |  |  |     mkdir -p $out/bin | 
					
						
							|  |  |  |     ln -s ${opensmtpd}/sbin/smtpctl $out/bin/sendmail | 
					
						
							|  |  |  |   '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  | in { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### interface | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     services.opensmtpd = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       enable = mkOption { | 
					
						
							|  |  |  |         type = types.bool; | 
					
						
							|  |  |  |         default = false; | 
					
						
							|  |  |  |         description = "Whether to enable the OpenSMTPD server."; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 16:33:57 +01:00
										 |  |  |       addSendmailToSystemPath = mkOption { | 
					
						
							|  |  |  |         type = types.bool; | 
					
						
							|  |  |  |         default = true; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Whether to add OpenSMTPD's sendmail binary to the | 
					
						
							|  |  |  |           system path or not. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  |       extraServerArgs = mkOption { | 
					
						
							| 
									
										
										
										
											2015-06-15 18:18:46 +02:00
										 |  |  |         type = types.listOf types.str; | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  |         default = []; | 
					
						
							|  |  |  |         example = [ "-v" "-P mta" ]; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Extra command line arguments provided when the smtpd process | 
					
						
							|  |  |  |           is started. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       serverConfiguration = mkOption { | 
					
						
							|  |  |  |         type = types.string; | 
					
						
							|  |  |  |         default = ""; | 
					
						
							|  |  |  |         example = ''
 | 
					
						
							|  |  |  |           listen on lo | 
					
						
							|  |  |  |           accept for any deliver to lmtp localhost:24 | 
					
						
							|  |  |  |         ''; 
 | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           The contents of the smtpd.conf configuration file. See the | 
					
						
							|  |  |  |           OpenSMTPD documentation for syntax information. If this option | 
					
						
							|  |  |  |           is left empty, the OpenSMTPD server will not start. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2015-11-14 12:32:51 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       procPackages = mkOption { | 
					
						
							|  |  |  |         type = types.listOf types.path; | 
					
						
							|  |  |  |         default = []; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           Packages to search for filters, tables, queues, and schedulers. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           Add OpenSMTPD-extras here if you want to use the filters, etc. from | 
					
						
							|  |  |  |           that package. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### implementation | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 16:33:57 +01:00
										 |  |  |   config = mkIf cfg.enable { | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  |     users.extraGroups = { | 
					
						
							|  |  |  |       smtpd.gid = config.ids.gids.smtpd; | 
					
						
							|  |  |  |       smtpq.gid = config.ids.gids.smtpq; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     users.extraUsers = { | 
					
						
							|  |  |  |       smtpd = { | 
					
						
							|  |  |  |         description = "OpenSMTPD process user"; | 
					
						
							|  |  |  |         uid = config.ids.uids.smtpd; | 
					
						
							|  |  |  |         group = "smtpd"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       smtpq = { | 
					
						
							|  |  |  |         description = "OpenSMTPD queue user"; | 
					
						
							|  |  |  |         uid = config.ids.uids.smtpq; | 
					
						
							|  |  |  |         group = "smtpq"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-14 12:32:51 -08:00
										 |  |  |     systemd.services.opensmtpd = let | 
					
						
							|  |  |  |       procEnv = pkgs.buildEnv { | 
					
						
							|  |  |  |         name = "opensmtpd-procs"; | 
					
						
							|  |  |  |         paths = [ opensmtpd ] ++ cfg.procPackages; | 
					
						
							|  |  |  |         pathsToLink = [ "/libexec/opensmtpd" ]; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     in { | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  |       wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  |       wants = [ "network.target" ]; | 
					
						
							|  |  |  |       after = [ "network.target" ]; | 
					
						
							| 
									
										
										
										
											2016-05-22 21:22:39 +01:00
										 |  |  |       preStart = ''
 | 
					
						
							|  |  |  |         mkdir -p /var/spool/smtpd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         mkdir -p /var/spool/smtpd/offline | 
					
						
							|  |  |  |         chown root.smtpq /var/spool/smtpd/offline | 
					
						
							|  |  |  |         chmod 770 /var/spool/smtpd/offline | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         mkdir -p /var/spool/smtpd/purge | 
					
						
							|  |  |  |         chmod 700 /var/spool/smtpd/purge | 
					
						
							|  |  |  |       '';
 | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  |       serviceConfig.ExecStart = "${opensmtpd}/sbin/smtpd -d -f ${conf} ${args}"; | 
					
						
							| 
									
										
										
										
											2015-11-14 12:32:51 -08:00
										 |  |  |       environment.OPENSMTPD_PROC_PATH = "${procEnv}/libexec/opensmtpd"; | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2014-02-20 06:17:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 16:33:57 +01:00
										 |  |  |     environment.systemPackages = mkIf cfg.addSendmailToSystemPath [ sendmail ]; | 
					
						
							| 
									
										
										
										
											2013-07-30 10:20:56 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | } |