2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2011-10-27 21:43:20 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.dovecot2;
|
2015-12-09 10:27:44 +01:00
|
|
|
dovecotPkg = cfg.package;
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
baseDir = "/run/dovecot2";
|
|
|
|
stateDir = "/var/lib/dovecot";
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
dovecotConf = concatStrings [
|
2011-10-27 21:43:20 +02:00
|
|
|
''
|
2015-12-09 10:27:44 +01:00
|
|
|
base_dir = ${baseDir}
|
2016-01-10 04:54:07 +01:00
|
|
|
protocols = ${concatStringsSep " " cfg.protocols}
|
2016-02-09 22:56:24 +01:00
|
|
|
sendmail_path = /var/setuid-wrappers/sendmail
|
2011-10-27 21:43:20 +02:00
|
|
|
''
|
2015-12-09 10:27:44 +01:00
|
|
|
|
|
|
|
(if isNull cfg.sslServerCert then ''
|
|
|
|
ssl = no
|
|
|
|
disable_plaintext_auth = no
|
|
|
|
'' else ''
|
2012-09-21 12:28:49 +02:00
|
|
|
ssl_cert = <${cfg.sslServerCert}
|
|
|
|
ssl_key = <${cfg.sslServerKey}
|
2015-12-09 10:27:44 +01:00
|
|
|
${optionalString (!(isNull cfg.sslCACert)) ("ssl_ca = <" + cfg.sslCACert)}
|
2012-09-21 12:26:53 +02:00
|
|
|
disable_plaintext_auth = yes
|
2011-10-27 21:43:20 +02:00
|
|
|
'')
|
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
''
|
2011-10-27 21:43:20 +02:00
|
|
|
default_internal_user = ${cfg.user}
|
2016-01-10 05:07:26 +01:00
|
|
|
${optionalString (cfg.mailUser != null) "mail_uid = ${cfg.mailUser}"}
|
|
|
|
${optionalString (cfg.mailGroup != null) "mail_gid = ${cfg.mailGroup}"}
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2012-06-10 17:07:25 +02:00
|
|
|
mail_location = ${cfg.mailLocation}
|
2011-10-27 21:43:20 +02:00
|
|
|
|
|
|
|
maildir_copy_with_hardlinks = yes
|
2015-12-09 10:27:44 +01:00
|
|
|
pop3_uidl_format = %08Xv%08Xu
|
2011-10-27 21:43:20 +02:00
|
|
|
|
|
|
|
auth_mechanisms = plain login
|
2015-12-09 10:27:44 +01:00
|
|
|
|
2011-10-27 21:43:20 +02:00
|
|
|
service auth {
|
|
|
|
user = root
|
|
|
|
}
|
2015-12-09 10:27:44 +01:00
|
|
|
''
|
|
|
|
|
|
|
|
(optionalString cfg.enablePAM ''
|
2011-10-27 21:43:20 +02:00
|
|
|
userdb {
|
2011-10-28 00:04:08 +02:00
|
|
|
driver = passwd
|
2011-10-27 21:43:20 +02:00
|
|
|
}
|
2015-12-09 10:27:44 +01:00
|
|
|
|
2011-10-27 21:43:20 +02:00
|
|
|
passdb {
|
2011-10-28 00:04:08 +02:00
|
|
|
driver = pam
|
2013-03-30 22:25:19 +01:00
|
|
|
args = ${optionalString cfg.showPAMFailure "failure_show_msg=yes"} dovecot2
|
2011-10-27 21:43:20 +02:00
|
|
|
}
|
2015-12-09 10:27:44 +01:00
|
|
|
'')
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2016-01-10 05:05:12 +01:00
|
|
|
(optionalString (cfg.sieveScripts != {}) ''
|
|
|
|
plugin {
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList (to: from: "sieve_${to} = ${stateDir}/sieve/${to}") cfg.sieveScripts)}
|
|
|
|
}
|
|
|
|
'')
|
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
cfg.extraConfig
|
|
|
|
];
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
modulesDir = pkgs.symlinkJoin "dovecot-modules"
|
2016-01-10 04:58:56 +01:00
|
|
|
(map (pkg: "${pkg}/lib/dovecot") ([ dovecotPkg ] ++ map (module: module.override { dovecot = dovecotPkg; }) cfg.modules));
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
in
|
2011-10-27 21:43:20 +02:00
|
|
|
{
|
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
options.services.dovecot2 = {
|
|
|
|
enable = mkEnableOption "Dovecot 2.x POP3/IMAP server";
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
enablePop3 = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Start the POP3 listener (when Dovecot is enabled).";
|
|
|
|
};
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
enableImap = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Start the IMAP listener (when Dovecot is enabled).";
|
|
|
|
};
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
enableLmtp = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Start the LMTP listener (when Dovecot is enabled).";
|
|
|
|
};
|
2012-09-30 00:53:50 +02:00
|
|
|
|
2016-01-10 04:54:07 +01:00
|
|
|
protocols = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
description = "Additional listeners to start when Dovecot is enabled.";
|
|
|
|
};
|
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.dovecot22;
|
2016-01-17 19:34:55 +01:00
|
|
|
defaultText = "pkgs.dovecot22";
|
2015-12-09 10:27:44 +01:00
|
|
|
description = "Dovecot package to use.";
|
|
|
|
};
|
2012-09-30 00:53:50 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "dovecot2";
|
|
|
|
description = "Dovecot user name.";
|
|
|
|
};
|
2015-07-02 23:26:49 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "dovecot2";
|
|
|
|
description = "Dovecot group name.";
|
|
|
|
};
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "";
|
|
|
|
example = "mail_debug = yes";
|
|
|
|
description = "Additional entries to put verbatim into Dovecot's config file.";
|
|
|
|
};
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
configFile = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Config file used for the whole dovecot configuration.";
|
|
|
|
apply = v: if v != null then v else pkgs.writeText "dovecot.conf" dovecotConf;
|
|
|
|
};
|
2012-09-21 16:04:46 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
mailLocation = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "maildir:/var/spool/mail/%u"; /* Same as inbox, as postfix */
|
|
|
|
example = "maildir:~/mail:INBOX=/var/spool/mail/%u";
|
|
|
|
description = ''
|
|
|
|
Location that dovecot will use for mail folders. Dovecot mail_location option.
|
|
|
|
'';
|
|
|
|
};
|
2015-01-19 10:45:20 +01:00
|
|
|
|
2016-01-10 05:07:26 +01:00
|
|
|
mailUser = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Default user to store mail for virtual users.";
|
|
|
|
};
|
|
|
|
|
|
|
|
mailGroup = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Default group to store mail for virtual users.";
|
|
|
|
};
|
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
modules = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [];
|
2016-01-17 19:34:55 +01:00
|
|
|
example = literalExample "[ pkgs.dovecot_pigeonhole ]";
|
2015-12-09 10:27:44 +01:00
|
|
|
description = ''
|
|
|
|
Symlinks the contents of lib/dovecot of every given package into
|
2016-01-10 05:00:34 +01:00
|
|
|
/etc/dovecot/modules. This will make the given modules available
|
2015-12-09 10:27:44 +01:00
|
|
|
if a dovecot package with the module_dir patch applied (like
|
|
|
|
pkgs.dovecot22, the default) is being used.
|
|
|
|
'';
|
|
|
|
};
|
2012-06-10 17:07:25 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
sslCACert = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Path to the server's CA certificate key.";
|
|
|
|
};
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
sslServerCert = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Path to the server's public key.";
|
|
|
|
};
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
sslServerKey = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Path to the server's private key.";
|
|
|
|
};
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
enablePAM = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2016-01-06 21:09:06 +01:00
|
|
|
description = "Whether to create a own Dovecot PAM service and configure PAM user logins.";
|
2011-10-27 21:43:20 +02:00
|
|
|
};
|
|
|
|
|
2016-01-10 05:05:12 +01:00
|
|
|
sieveScripts = mkOption {
|
|
|
|
type = types.attrsOf types.path;
|
|
|
|
default = {};
|
|
|
|
description = "Sieve scripts to be executed. Key is a sequence, e.g. 'before2', 'after' etc.";
|
|
|
|
};
|
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
showPAMFailure = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Show the PAM failure message on authentication error (useful for OTPW).";
|
|
|
|
};
|
2011-10-27 21:43:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
config = mkIf cfg.enable {
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
security.pam.services.dovecot2 = mkIf cfg.enablePAM {};
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2016-01-10 04:54:07 +01:00
|
|
|
services.dovecot2.protocols =
|
|
|
|
optional cfg.enableImap "imap"
|
|
|
|
++ optional cfg.enablePop3 "pop3"
|
|
|
|
++ optional cfg.enableLmtp "lmtp";
|
|
|
|
|
2011-10-27 21:43:20 +02:00
|
|
|
users.extraUsers = [
|
|
|
|
{ name = "dovenull";
|
|
|
|
uid = config.ids.uids.dovenull2;
|
|
|
|
description = "Dovecot user for untrusted logins";
|
|
|
|
group = cfg.group;
|
|
|
|
}
|
2016-01-06 23:27:06 +01:00
|
|
|
] ++ optional (cfg.user == "dovecot2")
|
|
|
|
{ name = "dovecot2";
|
|
|
|
uid = config.ids.uids.dovecot2;
|
|
|
|
description = "Dovecot user";
|
|
|
|
group = cfg.group;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.extraGroups = optional (cfg.group == "dovecot2")
|
|
|
|
{ name = "dovecot2";
|
|
|
|
gid = config.ids.gids.dovecot2;
|
|
|
|
};
|
2015-01-19 10:45:20 +01:00
|
|
|
|
2016-01-10 05:00:34 +01:00
|
|
|
environment.etc."dovecot/modules".source = modulesDir;
|
2016-01-10 05:02:24 +01:00
|
|
|
environment.etc."dovecot/dovecot.conf".source = cfg.configFile;
|
2016-01-10 05:00:34 +01:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
systemd.services.dovecot2 = {
|
|
|
|
description = "Dovecot IMAP/POP3 server";
|
|
|
|
|
|
|
|
after = [ "keys.target" "network.target" ];
|
|
|
|
wants = [ "keys.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2016-01-10 05:02:24 +01:00
|
|
|
restartTriggers = [ cfg.configFile ];
|
2015-12-09 10:27:44 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
2016-01-10 05:02:24 +01:00
|
|
|
ExecStart = "${dovecotPkg}/sbin/dovecot -F";
|
|
|
|
ExecReload = "${dovecotPkg}/sbin/doveadm reload";
|
2015-12-09 10:27:44 +01:00
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "1s";
|
|
|
|
StartLimitInterval = "1min";
|
2016-01-09 02:15:25 +01:00
|
|
|
RuntimeDirectory = [ "dovecot2" ];
|
2011-10-27 21:43:20 +02:00
|
|
|
};
|
2016-01-10 05:05:12 +01:00
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
rm -rf ${stateDir}/sieve
|
|
|
|
'' + optionalString (cfg.sieveScripts != {}) ''
|
|
|
|
mkdir -p ${stateDir}/sieve
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList (to: from: ''
|
|
|
|
if [ -d '${from}' ]; then
|
|
|
|
mkdir '${stateDir}/sieve/${to}'
|
|
|
|
cp ${from}/*.sieve '${stateDir}/sieve/${to}'
|
|
|
|
else
|
|
|
|
cp '${from}' '${stateDir}/sieve/${to}'
|
|
|
|
fi
|
|
|
|
${pkgs.dovecot_pigeonhole}/bin/sievec '${stateDir}/sieve/${to}'
|
|
|
|
'') cfg.sieveScripts)}
|
2016-01-10 05:07:26 +01:00
|
|
|
chown -R '${cfg.mailUser}:${cfg.mailGroup}' '${stateDir}/sieve'
|
2016-01-10 05:05:12 +01:00
|
|
|
'';
|
2015-12-09 10:27:44 +01:00
|
|
|
};
|
2011-10-27 21:43:20 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
environment.systemPackages = [ dovecotPkg ];
|
2011-10-28 00:04:08 +02:00
|
|
|
|
2015-12-09 10:27:44 +01:00
|
|
|
assertions = [
|
2016-01-10 04:54:07 +01:00
|
|
|
{ assertion = intersectLists cfg.protocols [ "pop3" "imap" ] != [];
|
2015-12-09 10:27:44 +01:00
|
|
|
message = "dovecot needs at least one of the IMAP or POP3 listeners enabled";
|
|
|
|
}
|
|
|
|
{ assertion = isNull cfg.sslServerCert == isNull cfg.sslServerKey
|
|
|
|
&& (!(isNull cfg.sslCACert) -> !(isNull cfg.sslServerCert || isNull cfg.sslServerKey));
|
|
|
|
message = "dovecot needs both sslServerCert and sslServerKey defined for working crypto";
|
|
|
|
}
|
|
|
|
{ assertion = cfg.showPAMFailure -> cfg.enablePAM;
|
|
|
|
message = "dovecot is configured with showPAMFailure while enablePAM is disabled";
|
|
|
|
}
|
|
|
|
];
|
2012-09-30 00:53:50 +02:00
|
|
|
|
2011-10-27 21:43:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|