systemd to module

This commit is contained in:
Robin Raymond 2017-09-02 15:08:50 +02:00
parent 9ac491f87d
commit 3eb363fc71
2 changed files with 38 additions and 38 deletions

View File

@ -252,6 +252,7 @@ in
./mail-server/users.nix
./mail-server/environment.nix
./mail-server/networking.nix
./mail-server/systemd.nix
];
config = mkIf cfg.enable {
@ -262,11 +263,5 @@ in
certificateScheme certificateFile keyFile certificateDirectory virusScanning;
};
systemd = import ./mail-server/systemd.nix {
inherit pkgs;
inherit (cfg) mailDirectory vmailGroupName certificateScheme
certificateDirectory
hostPrefix domain dkimSelector dkimKeyDirectory;
};
};
}

View File

@ -14,22 +14,23 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ pkgs, mailDirectory, vmailGroupName, certificateScheme, certificateDirectory, hostPrefix,
domain, dkimSelector, dkimKeyDirectory}:
{ config, pkgs, lib, ... }:
let
create_certificate = if certificateScheme == 2 then
cfg = config.mailserver;
create_certificate = if cfg.certificateScheme == 2 then
''
# Create certificates if they do not exist yet
dir="${certificateDirectory}"
fqdn="${hostPrefix}.${domain}"
dir="${cfg.certificateDirectory}"
fqdn="${cfg.hostPrefix}.${cfg.domain}"
case $fqdn in /*) fqdn=$(cat "$fqdn");; esac
key="''${dir}/key-${domain}.pem";
cert="''${dir}/cert-${domain}.pem";
key="''${dir}/key-${cfg.domain}.pem";
cert="''${dir}/cert-${cfg.domain}.pem";
if [ ! -f "''${key}" ] || [ ! -f "''${cert}" ]
then
mkdir -p "${certificateDirectory}"
mkdir -p "${cfg.certificateDirectory}"
(umask 077; "${pkgs.openssl}/bin/openssl" genrsa -out "''${key}" 2048) &&
"${pkgs.openssl}/bin/openssl" req -new -key "''${key}" -x509 -subj "/CN=''${fqdn}" \
-days 3650 -out "''${cert}"
@ -37,34 +38,36 @@ let
''
else "";
dkim_key = "${dkimKeyDirectory}/${dkimSelector}.private";
dkim_txt = "${dkimKeyDirectory}/${dkimSelector}.txt";
dkim_key = "${cfg.dkimKeyDirectory}/${cfg.dkimSelector}.private";
dkim_txt = "${cfg.dkimKeyDirectory}/${cfg.dkimSelector}.txt";
create_dkim_cert =
''
# Create dkim dir
mkdir -p "${dkimKeyDirectory}"
chown rmilter:rmilter "${dkimKeyDirectory}"
mkdir -p "${cfg.dkimKeyDirectory}"
chown rmilter:rmilter "${cfg.dkimKeyDirectory}"
if [ ! -f "${dkim_key}" ] || [ ! -f "${dkim_txt}" ]
then
${pkgs.opendkim}/bin/opendkim-genkey -s "${dkimSelector}" \
-d ${domain} \
--directory="${dkimKeyDirectory}"
${pkgs.opendkim}/bin/opendkim-genkey -s "${cfg.dkimSelector}" \
-d ${cfg.domain} \
--directory="${cfg.dkimKeyDirectory}"
chown rmilter:rmilter "${dkim_key}"
fi
'';
in
{
config = with cfg; lib.mkIf enable {
# Set the correct permissions for dovecot vmail folder. See
# <http://wiki2.dovecot.org/SharedMailboxes/Permissions>. We choose
# to use the systemd service to set the folder permissions whenever
# dovecot gets started.
services.dovecot2.after = [ "postfix.service" ];
systemd.services.dovecot2.after = [ "postfix.service" ];
# Check for certificate before both postfix and dovecot to make sure it
# exists.
services.postfix = {
systemd.services.postfix = {
preStart =
''
# Create mail directory and set permissions
@ -76,10 +79,12 @@ in
'';
};
services.rmilter = {
systemd.services.rmilter = {
preStart =
''
${create_dkim_cert}
'';
};
};
}