diff --git a/mail-server/dovecot.nix b/mail-server/dovecot.nix index 757dba0..5535eaa 100644 --- a/mail-server/dovecot.nix +++ b/mail-server/dovecot.nix @@ -16,7 +16,6 @@ { config, - options, pkgs, lib, ... @@ -27,7 +26,11 @@ with (import ./common.nix { inherit config pkgs lib; }); let cfg = config.mailserver; - dovecotUnitName = if lib.hasAttr "dovecot" options.systemd.services then "dovecot" else "dovecot2"; + dovecotPreStart = + '' + ${genPasswdScript} + '' + + (lib.optionalString cfg.ldap.enable setPwdInLdapConfFile); passwdDir = "/run/dovecot2"; passwdFile = "${passwdDir}/passwd"; @@ -442,11 +445,10 @@ in ''; }; - systemd.services.${dovecotUnitName}.preStart = - '' - ${genPasswdScript} - '' - + (lib.optionalString cfg.ldap.enable setPwdInLdapConfFile); + systemd.services.dovecot.preStart = lib.mkIf (config.systemd.services ? "dovecot") dovecotPreStart; + systemd.services.dovecot2.preStart = lib.mkIf ( + config.systemd.services ? "dovecot2" + ) dovecotPreStart; systemd.services.postfix.restartTriggers = [ genPasswdScript diff --git a/mail-server/systemd.nix b/mail-server/systemd.nix index 8fb0da7..5c9c670 100644 --- a/mail-server/systemd.nix +++ b/mail-server/systemd.nix @@ -30,6 +30,26 @@ let [ "mailserver-selfsigned-certificate.service" ] else [ "acme-finished-${cfg.fqdn}.target" ]; + + dovecotUnitSettings = { + wants = certificatesDeps; + after = certificatesDeps; + preStart = + let + directories = lib.strings.escapeShellArgs ( + [ cfg.mailDirectory ] ++ lib.optional (cfg.indexDir != null) cfg.indexDir + ); + in + '' + # Create mail directory and set permissions. See + # . + # Prevent world-readable paths, even temporarily. + umask 007 + mkdir -p ${directories} + chgrp "${cfg.vmailGroupName}" ${directories} + chmod 02770 ${directories} + ''; + }; in { config = lib.mkIf cfg.enable { @@ -60,25 +80,9 @@ in }; # Create maildir folder before dovecot startup - systemd.services.dovecot2 = { - wants = certificatesDeps; - after = certificatesDeps; - preStart = - let - directories = lib.strings.escapeShellArgs ( - [ cfg.mailDirectory ] ++ lib.optional (cfg.indexDir != null) cfg.indexDir - ); - in - '' - # Create mail directory and set permissions. See - # . - # Prevent world-readable paths, even temporarily. - umask 007 - mkdir -p ${directories} - chgrp "${cfg.vmailGroupName}" ${directories} - chmod 02770 ${directories} - ''; - }; + systemd.services.dovecot = dovecotUnitSettings; + # TODO: remove after 25.11 release + systemd.services.dovecot2 = dovecotUnitSettings; # Postfix requires dovecot lmtp socket, dovecot auth socket and certificate to work systemd.services.postfix = {