Change domain to fqdn and extraDomains to domains

This commit is contained in:
John Boehr 2017-11-11 09:44:45 +00:00 committed by John Boehr
parent a745abaa8e
commit 16fb41de01
11 changed files with 51 additions and 78 deletions

View file

@ -26,26 +26,17 @@ in
options.mailserver = { options.mailserver = {
enable = mkEnableOption "nixos-mailserver"; enable = mkEnableOption "nixos-mailserver";
domain = mkOption { fqdn = mkOption {
type = types.str; type = types.str;
example = "[ example.com ]"; example = "[ example.com ]";
description = "The primary domain that this mail server serves."; description = "The fully qualified domain name of the mail server.";
}; };
extraDomains = mkOption { domains = mkOption {
type = types.listOf types.str; type = types.listOf types.str;
example = "[ example.com ]"; example = "[ example.com ]";
default = []; default = [];
description = "Extra domains that this mail server serves."; description = "The domains that this mail server serves.";
};
hostPrefix = mkOption {
type = types.str;
default = "mail";
description = ''
The prefix of the FQDN of the server. In this example the FQDN of the server
is given by 'mail.example.com'
'';
}; };
loginAccounts = mkOption { loginAccounts = mkOption {

View file

@ -14,34 +14,27 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/> # along with this program. If not, see <http://www.gnu.org/licenses/>
{ config, lib }: { config }:
let let
cfg = config.mailserver; cfg = config.mailserver;
inherit (lib.strings) stringToCharacters;
in in
{ {
# cert :: PATH # cert :: PATH
certificatePath = if cfg.certificateScheme == 1 certificatePath = if cfg.certificateScheme == 1
then cfg.certificateFile then cfg.certificateFile
else if cfg.certificateScheme == 2 else if cfg.certificateScheme == 2
then "${cfg.certificateDirectory}/cert-${cfg.domain}.pem" then "${cfg.certificateDirectory}/cert-${cfg.fqdn}.pem"
else if cfg.certificateScheme == 3 else if cfg.certificateScheme == 3
then "/var/lib/acme/mailserver/fullchain.pem" then "/var/lib/acme/${cfg.fqdn}/fullchain.pem"
else throw "Error: Certificate Scheme must be in { 1, 2, 3 }"; else throw "Error: Certificate Scheme must be in { 1, 2, 3 }";
# key :: PATH # key :: PATH
keyPath = if cfg.certificateScheme == 1 keyPath = if cfg.certificateScheme == 1
then cfg.keyFile then cfg.keyFile
else if cfg.certificateScheme == 2 else if cfg.certificateScheme == 2
then "${cfg.certificateDirectory}/key-${cfg.domain}.pem" then "${cfg.certificateDirectory}/key-${cfg.fqdn}.pem"
else if cfg.certificateScheme == 3 else if cfg.certificateScheme == 3
then "/var/lib/acme/mailserver/key.pem" then "/var/lib/acme/${cfg.fqdn}/key.pem"
else throw "Error: Certificate Scheme must be in { 1, 2, 3 }"; else throw "Error: Certificate Scheme must be in { 1, 2, 3 }";
# appends cfg.domain to argument if it does not contain "@"
qualifyUser = user: (
if (builtins.any (c: c == "@") (stringToCharacters user))
then user
else "${user}@${cfg.domain}");
} }

View file

@ -16,7 +16,7 @@
{ config, pkgs, lib, ... }: { config, pkgs, lib, ... }:
with (import ./common.nix { inherit config lib; }); with (import ./common.nix { inherit config; });
let let
cfg = config.mailserver; cfg = config.mailserver;

View file

@ -20,35 +20,29 @@
with (import ./common.nix { inherit config; }); with (import ./common.nix { inherit config; });
let let
inherit (lib.attrsets) genAttrs;
cfg = config.mailserver; cfg = config.mailserver;
allDomains = [ cfg.domain ] ++ cfg.extraDomains;
acmeRoot = "/var/lib/acme/acme-challenge"; acmeRoot = "/var/lib/acme/acme-challenge";
in in
{ {
config = lib.mkIf (cfg.certificateScheme == 3) { config = lib.mkIf (cfg.certificateScheme == 3) {
services.nginx = { services.nginx = {
enable = true; enable = true;
virtualHosts = genAttrs (map (domain: "${cfg.hostPrefix}.${domain}") allDomains) (domain: { virtualHosts."${cfg.fqdn}" = {
serverName = "${domain}"; serverName = cfg.fqdn;
forceSSL = true; forceSSL = true;
enableACME = true; enableACME = true;
locations."/" = { acmeRoot = acmeRoot;
root = "/var/www"; };
};
acmeRoot = acmeRoot;
});
}; };
security.acme.certs."mailserver" = { security.acme.certs."${cfg.fqdn}".postRun = #{
domain = "${cfg.hostPrefix}.${cfg.domain}"; # domain = "${cfg.fqdn}";
extraDomains = genAttrs (map (domain: "${cfg.hostPrefix}.${domain}") cfg.extraDomains) (domain: null); # webroot = acmeRoot;
webroot = acmeRoot; # postRun =
# @todo should we reload postfix here? ''
postRun = ''
systemctl reload nginx systemctl reload nginx
systemctl reload postfix systemctl reload postfix
systemctl reload dovecot2 systemctl reload dovecot2
''; '';
}; # };
}; };
} }

View file

@ -16,22 +16,21 @@
{ config, pkgs, lib, ... }: { config, pkgs, lib, ... }:
with (import ./common.nix { inherit config lib; }); with (import ./common.nix { inherit config; });
let let
inherit (lib.strings) concatStringsSep; inherit (lib.strings) concatStringsSep;
cfg = config.mailserver; cfg = config.mailserver;
allDomains = [ cfg.domain ] ++ cfg.extraDomains;
# valiases_postfix :: [ String ] # valiases_postfix :: [ String ]
valiases_postfix = map valiases_postfix = map
(from: (from:
let to = cfg.virtualAliases.${from}; let to = cfg.virtualAliases.${from};
in "${qualifyUser from} ${qualifyUser to}") in "${from} ${to}")
(builtins.attrNames cfg.virtualAliases); (builtins.attrNames cfg.virtualAliases);
# accountToIdentity :: User -> String # accountToIdentity :: User -> String
accountToIdentity = account: "${qualifyUser account.name} ${qualifyUser account.name}"; accountToIdentity = account: "${account.name} ${account.name}";
# vaccounts_identity :: [ String ] # vaccounts_identity :: [ String ]
vaccounts_identity = map accountToIdentity (lib.attrValues cfg.loginAccounts); vaccounts_identity = map accountToIdentity (lib.attrValues cfg.loginAccounts);
@ -40,7 +39,7 @@ let
valiases_file = builtins.toFile "valias" (lib.concatStringsSep "\n" valiases_postfix); valiases_file = builtins.toFile "valias" (lib.concatStringsSep "\n" valiases_postfix);
# vhosts_file :: Path # vhosts_file :: Path
vhosts_file = builtins.toFile "vhosts" (concatStringsSep ", " allDomains); vhosts_file = builtins.toFile "vhosts" (concatStringsSep "\n" cfg.domains);
# vaccounts_file :: Path # vaccounts_file :: Path
# see # see

View file

@ -24,14 +24,14 @@ let
cert = if cfg.certificateScheme == 1 cert = if cfg.certificateScheme == 1
then cfg.certificateFile then cfg.certificateFile
else if cfg.certificateScheme == 2 else if cfg.certificateScheme == 2
then "${cfg.certificateDirectory}/cert-${cfg.domain}.pem" then "${cfg.certificateDirectory}/cert-${cfg.fqdn.pem"
else ""; else "";
# key :: PATH # key :: PATH
key = if cfg.certificateScheme == 1 key = if cfg.certificateScheme == 1
then cfg.keyFile then cfg.keyFile
else if cfg.certificateScheme == 2 else if cfg.certificateScheme == 2
then "${cfg.certificateDirectory}/key-${cfg.domain}.pem" then "${cfg.certificateDirectory}/key-${cfg.fqdn}.pem"
else ""; else "";
in in
{ {

View file

@ -23,10 +23,10 @@ let
'' ''
# Create certificates if they do not exist yet # Create certificates if they do not exist yet
dir="${cfg.certificateDirectory}" dir="${cfg.certificateDirectory}"
fqdn="${cfg.hostPrefix}.${cfg.domain}" fqdn="${cfg.fqdn}"
case $fqdn in /*) fqdn=$(cat "$fqdn");; esac case $fqdn in /*) fqdn=$(cat "$fqdn");; esac
key="''${dir}/key-${cfg.domain}.pem"; key="''${dir}/key-${cfg.fqdn}.pem";
cert="''${dir}/cert-${cfg.domain}.pem"; cert="''${dir}/cert-${cfg.fqdn}.pem";
if [ ! -f "''${key}" ] || [ ! -f "''${cert}" ] if [ ! -f "''${key}" ] || [ ! -f "''${cert}" ]
then then
@ -50,7 +50,7 @@ let
then then
${pkgs.opendkim}/bin/opendkim-genkey -s "${cfg.dkimSelector}" \ ${pkgs.opendkim}/bin/opendkim-genkey -s "${cfg.dkimSelector}" \
-d ${cfg.domain} \ -d ${cfg.fqdn} \
--directory="${cfg.dkimKeyDirectory}" --directory="${cfg.dkimKeyDirectory}"
chown rmilter:rmilter "${dkim_key}" chown rmilter:rmilter "${dkim_key}"
fi fi

View file

@ -19,8 +19,6 @@
with config.mailserver; with config.mailserver;
let let
qualifyUser = (import ./common.nix { inherit config lib; }).qualifyUser;
vmail_user = { vmail_user = {
name = vmailUserName; name = vmailUserName;
isNormalUser = false; isNormalUser = false;
@ -32,14 +30,14 @@ let
# accountsToUser :: String -> UserRecord # accountsToUser :: String -> UserRecord
accountsToUser = account: { accountsToUser = account: {
name = (qualifyUser account.name); name = account.name;
isNormalUser = false; isNormalUser = false;
group = vmailGroupName; group = vmailGroupName;
inherit (account) hashedPassword; inherit (account) hashedPassword;
}; };
# mail_users :: { [String]: UserRecord } # mail_users :: { [String]: UserRecord }
mail_users = lib.foldl (prev: next: prev // { "${qualifyUser next.name}" = next; }) {} mail_users = lib.foldl (prev: next: prev // { "${next.name}" = next; }) {}
(map accountsToUser (lib.attrValues loginAccounts)); (map accountsToUser (lib.attrValues loginAccounts));
in in

View file

@ -10,23 +10,21 @@
mailserver = { mailserver = {
enable = true; enable = true;
domain = "example.com"; fqdn = "mail.example.com";
extraDomains = [ "example2.com" ]; domains = [ "example.com", "example2.com" ];
hostPrefix = "mail";
loginAccounts = { loginAccounts = {
"user1" = { "user1@example.com" = {
hashedPassword = "$6$/z4n8AQl6K$kiOkBTWlZfBd7PvF5GsJ8PmPgdZsFGN1jPGZufxxr60PoR0oUsrvzm2oQiflyz5ir9fFJ.d/zKm/NgLXNUsNX/"; hashedPassword = "$6$/z4n8AQl6K$kiOkBTWlZfBd7PvF5GsJ8PmPgdZsFGN1jPGZufxxr60PoR0oUsrvzm2oQiflyz5ir9fFJ.d/zKm/NgLXNUsNX/";
}; };
}; };
virtualAliases = { virtualAliases = {
"info" = "user1"; "info@example.com" = "user1@example.com";
"postmaster" = "user1"; "postmaster@example.com" = "user1@example.com";
"abuse" = "user1"; "abuse@example.com" = "user1@example.com";
"user1@example2.com" = "user1"; "user1@example2.com" = "user1@example.com";
"info@example2.com" = "user1"; "info@example2.com" = "user1@example.com";
"postmaster@example2.com" = "user1"; "postmaster@example2.com" = "user1@example.com";
"abuse@example2.com" = "user1"; "abuse@example2.com" = "user1@example.com";
}; };
}; };
}; };

View file

@ -25,14 +25,14 @@ import <nixpkgs/nixos/tests/make-test.nix> {
mailserver = { mailserver = {
enable = true; enable = true;
domain = "example.com"; fqdn = "mail.example.com";
domains = [ "example.com" ];
hostPrefix = "mail";
loginAccounts = { loginAccounts = {
user1 = { "user1@example.com" = {
hashedPassword = "$6$/z4n8AQl6K$kiOkBTWlZfBd7PvF5GsJ8PmPgdZsFGN1jPGZufxxr60PoR0oUsrvzm2oQiflyz5ir9fFJ.d/zKm/NgLXNUsNX/"; hashedPassword = "$6$/z4n8AQl6K$kiOkBTWlZfBd7PvF5GsJ8PmPgdZsFGN1jPGZufxxr60PoR0oUsrvzm2oQiflyz5ir9fFJ.d/zKm/NgLXNUsNX/";
}; };
user2 = { "user2@example.com" = {
hashedPassword = "$6$u61JrAtuI0a$nGEEfTP5.eefxoScUGVG/Tl0alqla2aGax4oTd85v3j3xSmhv/02gNfSemv/aaMinlv9j/ZABosVKBrRvN5Qv0"; hashedPassword = "$6$u61JrAtuI0a$nGEEfTP5.eefxoScUGVG/Tl0alqla2aGax4oTd85v3j3xSmhv/02gNfSemv/aaMinlv9j/ZABosVKBrRvN5Qv0";
}; };
}; };

View file

@ -25,11 +25,11 @@ import <nixpkgs/nixos/tests/make-test.nix> {
mailserver = { mailserver = {
enable = true; enable = true;
domain = "example.com"; fqdn = "mail.example.com";
domains = [ "example.com" ];
hostPrefix = "mail";
loginAccounts = { loginAccounts = {
user1 = { "user1@example.com" = {
hashedPassword = "$6$/z4n8AQl6K$kiOkBTWlZfBd7PvF5GsJ8PmPgdZsFGN1jPGZufxxr60PoR0oUsrvzm2oQiflyz5ir9fFJ.d/zKm/NgLXNUsNX/"; hashedPassword = "$6$/z4n8AQl6K$kiOkBTWlZfBd7PvF5GsJ8PmPgdZsFGN1jPGZufxxr60PoR0oUsrvzm2oQiflyz5ir9fFJ.d/zKm/NgLXNUsNX/";
}; };
}; };