2009-10-12 17:36:19 +01:00
|
|
|
{ config, pkgs, ... }:
|
2009-03-06 12:27:07 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
with pkgs.lib;
|
2009-03-06 12:27:07 +00:00
|
|
|
|
2008-06-30 16:13:02 +01:00
|
|
|
let
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2008-06-30 16:13:02 +01:00
|
|
|
cfg = config.services.postfix;
|
|
|
|
user = cfg.user;
|
|
|
|
group = cfg.group;
|
|
|
|
setgidGroup = cfg.setgidGroup;
|
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
mainCf =
|
2009-10-12 17:36:19 +01:00
|
|
|
''
|
|
|
|
queue_directory = /var/postfix/queue
|
|
|
|
command_directory = ${pkgs.postfix}/sbin
|
|
|
|
daemon_directory = ${pkgs.postfix}/libexec/postfix
|
|
|
|
|
|
|
|
mail_owner = ${user}
|
|
|
|
default_privs = nobody
|
|
|
|
|
|
|
|
''
|
2012-03-11 11:12:02 +00:00
|
|
|
+ optionalString config.networking.enableIPv6 ''
|
|
|
|
inet_protocols = all
|
|
|
|
''
|
2009-10-12 17:36:19 +01:00
|
|
|
+ (if cfg.networks != null then
|
|
|
|
''
|
|
|
|
mynetworks = ${concatStringsSep ", " cfg.networks}
|
|
|
|
''
|
|
|
|
else if cfg.networksStyle != "" then
|
|
|
|
''
|
2011-09-14 19:20:50 +01:00
|
|
|
mynetworks_style = ${cfg.networksStyle}
|
2009-10-12 17:36:19 +01:00
|
|
|
''
|
|
|
|
else
|
|
|
|
# Postfix default is subnet, but let's play safe
|
|
|
|
''
|
|
|
|
mynetworks_style = host
|
|
|
|
'')
|
|
|
|
+ optionalString (cfg.hostname != "") ''
|
|
|
|
myhostname = ${cfg.hostname}
|
|
|
|
''
|
|
|
|
+ optionalString (cfg.domain != "") ''
|
|
|
|
mydomain = ${cfg.domain}
|
|
|
|
''
|
|
|
|
+ optionalString (cfg.origin != "") ''
|
|
|
|
myorigin = ${cfg.origin}
|
|
|
|
''
|
|
|
|
+ optionalString (cfg.destination != null) ''
|
|
|
|
mydestination = ${concatStringsSep ", " cfg.destination}
|
|
|
|
''
|
|
|
|
+ optionalString (cfg.relayDomains != null) ''
|
|
|
|
relay_domains = ${concatStringsSep ", " cfg.relayDomains}
|
|
|
|
''
|
|
|
|
+ ''
|
|
|
|
local_recipient_maps =
|
2011-09-14 19:20:50 +01:00
|
|
|
|
|
|
|
relayhost = ${if cfg.lookupMX || cfg.relayHost == "" then
|
|
|
|
cfg.relayHost
|
|
|
|
else
|
2009-10-12 17:36:19 +01:00
|
|
|
"[" + cfg.relayHost + "]"}
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
alias_maps = hash:/var/postfix/conf/aliases
|
|
|
|
|
|
|
|
mail_spool_directory = /var/spool/mail/
|
|
|
|
|
|
|
|
setgid_group = ${setgidGroup}
|
|
|
|
''
|
|
|
|
+ optionalString (cfg.sslCert != "") ''
|
|
|
|
|
|
|
|
smtp_tls_CAfile = ${cfg.sslCACert}
|
|
|
|
smtp_tls_cert_file = ${cfg.sslCert}
|
|
|
|
smtp_tls_key_file = ${cfg.sslKey}
|
|
|
|
|
|
|
|
smtp_use_tls = yes
|
|
|
|
|
|
|
|
smtpd_tls_CAfile = ${cfg.sslCACert}
|
|
|
|
smtpd_tls_cert_file = ${cfg.sslCert}
|
|
|
|
smtpd_tls_key_file = ${cfg.sslKey}
|
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
smtpd_use_tls = yes
|
2009-10-12 17:36:19 +01:00
|
|
|
|
|
|
|
recipientDelimiter = ${cfg.recipientDelimiter}
|
2011-01-02 18:49:11 +00:00
|
|
|
''
|
|
|
|
+ cfg.extraConfig;
|
2008-06-30 16:13:02 +01:00
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
aliases =
|
2009-10-12 17:36:19 +01:00
|
|
|
optionalString (cfg.postmasterAlias != "") ''
|
2008-06-30 16:13:02 +01:00
|
|
|
postmaster: ${cfg.postmasterAlias}
|
2009-10-12 17:36:19 +01:00
|
|
|
''
|
|
|
|
+ optionalString (cfg.rootAlias != "") ''
|
2008-06-30 16:13:02 +01:00
|
|
|
root: ${cfg.rootAlias}
|
2009-10-12 17:36:19 +01:00
|
|
|
''
|
2008-06-30 16:13:02 +01:00
|
|
|
+ cfg.extraAliases
|
|
|
|
;
|
|
|
|
|
|
|
|
aliasesFile = pkgs.writeText "postfix-aliases" aliases;
|
|
|
|
mainCfFile = pkgs.writeText "postfix-main.cf" mainCf;
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2008-06-30 16:13:02 +01:00
|
|
|
in
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
services.postfix = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "Whether to run the Postfix mail server.";
|
|
|
|
};
|
2010-10-10 11:43:28 +01:00
|
|
|
|
|
|
|
setSendmail = mkOption {
|
|
|
|
default = true;
|
|
|
|
description = "Whether to set the system sendmail to postfix's.";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
user = mkOption {
|
|
|
|
default = "postfix";
|
|
|
|
description = "What to call the Postfix user (must be used only for postfix).";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
group = mkOption {
|
|
|
|
default = "postfix";
|
|
|
|
description = "What to call the Postfix group (must be used only for postfix).";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
setgidGroup = mkOption {
|
|
|
|
default = "postdrop";
|
|
|
|
description = "
|
2011-09-14 19:20:50 +01:00
|
|
|
How to call postfix setgid group (for postdrop). Should
|
2009-10-12 17:36:19 +01:00
|
|
|
be uniquely used group.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
networks = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = ["192.168.0.1/24"];
|
|
|
|
description = "
|
2011-09-14 19:20:50 +01:00
|
|
|
Net masks for trusted - allowed to relay mail to third parties -
|
|
|
|
hosts. Leave empty to use mynetworks_style configuration or use
|
2009-10-12 17:36:19 +01:00
|
|
|
default (localhost-only).
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
networksStyle = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = "
|
|
|
|
Name of standard way of trusted network specification to use,
|
2011-09-14 19:20:50 +01:00
|
|
|
leave blank if you specify it explicitly or if you want to use
|
2009-10-12 17:36:19 +01:00
|
|
|
default (localhost-only).
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
hostname = mkOption {
|
|
|
|
default = "";
|
|
|
|
description ="
|
|
|
|
Hostname to use. Leave blank to use just the hostname of machine.
|
|
|
|
It should be FQDN.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
domain = mkOption {
|
|
|
|
default = "";
|
|
|
|
description ="
|
|
|
|
Domain to use. Leave blank to use hostname minus first component.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
origin = mkOption {
|
|
|
|
default = "";
|
|
|
|
description ="
|
|
|
|
Origin to use in outgoing e-mail. Leave blank to use hostname.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
destination = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = ["localhost"];
|
|
|
|
description = "
|
2011-09-14 19:20:50 +01:00
|
|
|
Full (!) list of domains we deliver locally. Leave blank for
|
2009-10-12 17:36:19 +01:00
|
|
|
acceptable Postfix default.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
relayDomains = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = ["localdomain"];
|
|
|
|
description = "
|
2011-09-14 19:20:50 +01:00
|
|
|
List of domains we agree to relay to. Default is the same as
|
2009-10-12 17:36:19 +01:00
|
|
|
destination.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
relayHost = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = "
|
|
|
|
Mail relay for outbound mail.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
lookupMX = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "
|
|
|
|
Whether relay specified is just domain whose MX must be used.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
postmasterAlias = mkOption {
|
|
|
|
default = "root";
|
|
|
|
description = "Who should receive postmaster e-mail.";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
rootAlias = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = "
|
|
|
|
Who should receive root e-mail. Blank for no redirection.
|
|
|
|
";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
extraAliases = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = "
|
|
|
|
Additional entries to put verbatim into aliases file.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2011-01-02 18:49:11 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = "
|
|
|
|
Extra configuration, will be added verbatim to the configuration file.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
sslCert = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = "SSL certificate to use.";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
sslCACert = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = "SSL certificate of CA.";
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
sslKey = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = "SSL key to use.";
|
|
|
|
};
|
|
|
|
|
|
|
|
recipientDelimiter = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "+";
|
|
|
|
description = "
|
|
|
|
Delimiter for address extension: so mail to user+test can be handled by ~user/.forward+test
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2008-06-30 16:13:02 +01:00
|
|
|
|
2009-03-06 12:27:07 +00:00
|
|
|
};
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf config.services.postfix.enable {
|
|
|
|
|
2010-10-10 11:43:28 +01:00
|
|
|
environment = {
|
|
|
|
etc = singleton
|
|
|
|
{ source = "/var/postfix/conf";
|
|
|
|
target = "postfix";
|
|
|
|
};
|
|
|
|
|
|
|
|
# This makes comfortable for root to run 'postqueue' for example.
|
|
|
|
systemPackages = [ pkgs.postfix ];
|
|
|
|
};
|
|
|
|
|
|
|
|
services.mail.sendmailSetuidWrapper = mkIf config.services.postfix.setSendmail {
|
|
|
|
program = "sendmail";
|
|
|
|
source = "${pkgs.postfix}/bin/sendmail";
|
|
|
|
owner = "nobody";
|
|
|
|
group = "postdrop";
|
|
|
|
setuid = false;
|
|
|
|
setgid = true;
|
|
|
|
};
|
2009-10-12 17:36:19 +01:00
|
|
|
|
|
|
|
users.extraUsers = singleton
|
2009-03-06 12:27:07 +00:00
|
|
|
{ name = user;
|
|
|
|
description = "Postfix mail server user";
|
2009-05-29 15:25:56 +01:00
|
|
|
uid = config.ids.uids.postfix;
|
2009-03-06 12:27:07 +00:00
|
|
|
group = group;
|
2009-10-12 17:36:19 +01:00
|
|
|
};
|
2009-03-06 12:27:07 +00:00
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
users.extraGroups =
|
2011-09-14 19:20:50 +01:00
|
|
|
[ { name = group;
|
2009-10-12 17:36:19 +01:00
|
|
|
gid = config.ids.gids.postfix;
|
|
|
|
}
|
2011-09-14 19:20:50 +01:00
|
|
|
{ name = setgidGroup;
|
2009-10-12 17:36:19 +01:00
|
|
|
gid = config.ids.gids.postdrop;
|
|
|
|
}
|
|
|
|
];
|
2009-03-06 12:27:07 +00:00
|
|
|
|
2009-10-12 19:09:34 +01:00
|
|
|
jobs.postfix =
|
2011-09-14 19:20:50 +01:00
|
|
|
# I copy _lots_ of shipped configuration filed
|
2009-03-06 12:27:07 +00:00
|
|
|
# that can be left as is. I am afraid the exact
|
2011-09-14 19:20:50 +01:00
|
|
|
# will list slightly change in next Postfix
|
|
|
|
# release, so listing them all one-by-one in an
|
2009-03-06 12:27:07 +00:00
|
|
|
# accurate way is unlikely to be better.
|
2009-10-12 17:36:19 +01:00
|
|
|
{ description = "Postfix mail server";
|
2009-03-06 12:27:07 +00:00
|
|
|
|
2012-03-04 18:44:42 +00:00
|
|
|
startOn = "started networking";
|
2010-03-09 14:37:59 +00:00
|
|
|
|
2010-04-13 23:47:34 +01:00
|
|
|
daemonType = "none";
|
2009-03-06 12:27:07 +00:00
|
|
|
|
2010-06-11 22:44:06 +01:00
|
|
|
respawn = true;
|
|
|
|
|
2011-07-07 20:18:40 +01:00
|
|
|
environment.TZ = config.time.timeZone;
|
|
|
|
|
2010-06-11 22:44:06 +01:00
|
|
|
script = ''
|
|
|
|
while ${pkgs.procps}/bin/ps `${pkgs.coreutils}/bin/cat /var/postfix/queue/pid/master.pid` |
|
|
|
|
grep -q postfix
|
|
|
|
do
|
|
|
|
${pkgs.coreutils}/bin/sleep 1m
|
|
|
|
done
|
|
|
|
'';
|
2010-04-13 23:47:34 +01:00
|
|
|
|
|
|
|
preStart =
|
2009-10-12 17:36:19 +01:00
|
|
|
''
|
2009-03-06 12:27:07 +00:00
|
|
|
if ! [ -d /var/spool/postfix ]; then
|
|
|
|
${pkgs.coreutils}/bin/mkdir -p /var/spool/mail /var/postfix/conf /var/postfix/queue
|
|
|
|
fi
|
2011-09-14 19:20:50 +01:00
|
|
|
|
|
|
|
${pkgs.coreutils}/bin/chown -R ${user}.${group} /var/postfix
|
|
|
|
${pkgs.coreutils}/bin/chown -R ${user}.${setgidGroup} /var/postfix/queue
|
|
|
|
${pkgs.coreutils}/bin/chmod -R ug+rwX /var/postfix/queue
|
2009-03-06 12:27:07 +00:00
|
|
|
${pkgs.coreutils}/bin/chown root.root /var/spool/mail
|
|
|
|
${pkgs.coreutils}/bin/chmod a+rwxt /var/spool/mail
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-03-06 12:27:07 +00:00
|
|
|
ln -sf ${pkgs.postfix}/share/postfix/conf/* /var/postfix/conf
|
|
|
|
|
|
|
|
ln -sf ${aliasesFile} /var/postfix/conf/aliases
|
|
|
|
ln -sf ${mainCfFile} /var/postfix/conf/main.cf
|
|
|
|
|
|
|
|
${pkgs.postfix}/sbin/postalias -c /var/postfix/conf /var/postfix/conf/aliases
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2010-03-09 14:37:59 +00:00
|
|
|
exec ${pkgs.postfix}/sbin/postfix -c /var/postfix/conf start
|
2009-10-12 17:36:19 +01:00
|
|
|
''; # */
|
|
|
|
|
2010-04-13 23:47:34 +01:00
|
|
|
preStop = ''
|
|
|
|
exec ${pkgs.postfix}/sbin/postfix -c /var/postfix/conf stop
|
|
|
|
'';
|
|
|
|
|
2009-10-12 17:36:19 +01:00
|
|
|
};
|
2009-03-06 12:27:07 +00:00
|
|
|
|
|
|
|
};
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2008-06-30 16:13:02 +01:00
|
|
|
}
|