{ pkgs, lib, config, ... }:
let
  inherit (pkgs.lib) unlinesValues;
  inherit (config.services) dovecot2;
  stateDir = "/var/lib/dovecot";
in
{
  config = lib.mkIf dovecot2.enable {
    systemd.services.dovecot2 = {
      preStart = unlinesValues {
        installDomains = ''
          # SEE: http://wiki2.dovecot.org/SharedMailboxes/Permissions
          install -D -d -m 0771 \
           -o "${dovecot2.user}" \
           -g "${dovecot2.group}" \
           ${stateDir}/mail
        '';
      };
    };
  };
}