Preliminary multi-domain support

This commit is contained in:
John Boehr 2017-11-09 13:13:27 -08:00
parent 3d2f41dedc
commit ebd0f656ed
No known key found for this signature in database
GPG key ID: 73B8EFB60708F699
6 changed files with 51 additions and 27 deletions

View file

@ -28,8 +28,14 @@ in
domain = mkOption { domain = mkOption {
type = types.str; type = types.str;
example = "example.com"; example = "[ example.com ]";
description = "The domain that this mail server serves. So far only one domain is supported"; description = "The primary domain that this mail server serves.";
};
extraDomains = mkOption {
type = types.listOf types.str;
example = "[ example.com ]";
description = "Extra domains that this mail server serves.";
}; };
hostPrefix = mkOption { hostPrefix = mkOption {

View file

@ -21,23 +21,34 @@ with (import ./common.nix { inherit config; });
let let
cfg = config.mailserver; cfg = config.mailserver;
allDomains = [ cfg.domain ] ++ cfg.extraDomains;
acmeRoot = "/var/lib/acme/acme-challenge";
in in
{ {
config = with cfg; lib.mkIf (certificateScheme == 3) { config = with cfg; lib.mkIf (certificateScheme == 3) {
services.nginx = { services.nginx = {
enable = true; enable = true;
virtualHosts = { virtualHosts = genAttrs allDomains (domain: {
domain = { serverName = "${hostPrefix}.${domain}";
serverName = "${hostPrefix}.${domain}"; forceSSL = true;
forceSSL = true; enableACME = true;
enableACME = true; locations."/" = {
locations."/" = { root = "/var/www";
root = "/var/www"; };
}; acmeRoot = acmeRoot;
acmeRoot = "/var/lib/acme/acme-challenge"; });
}; };
}; security.acme.certs."${hostPrefix}.${domain}" = {
# @todo what user/group should this run as?
user = "postfix"; # cfg.user;
group = "postfix"; # lib.mkDefault cfg.group;
domain = "${hostPrefix}.${domain}";
extraDomains = map (domain: "${hostPrefix}.${domain}") extraDomains;
webroot = acmeRoot;
# @todo should we reload postfix here?
postRun = ''
systemctl reload nginx
'';
}; };
}; };
} }

View file

@ -19,17 +19,19 @@
with (import ./common.nix { inherit config; }); with (import ./common.nix { inherit config; });
let let
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 "${from}@${cfg.domain} ${to}@${cfg.domain}") in "${from} ${to}")
(builtins.attrNames cfg.virtualAliases); (builtins.attrNames cfg.virtualAliases);
# accountToIdentity :: User -> String # accountToIdentity :: User -> String
accountToIdentity = account: "${account.name}@${cfg.domain} ${account.name}@${cfg.domain}"; 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);
@ -38,7 +40,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" cfg.domain; vhosts_file = builtins.toFile "vhosts" (concatStringsSep ", " allDomains);
# vaccounts_file :: Path # vaccounts_file :: Path
# see # see

View file

@ -30,7 +30,7 @@ let
# accountsToUser :: String -> UserRecord # accountsToUser :: String -> UserRecord
accountsToUser = account: { accountsToUser = account: {
name = account.name + "@" + domain; name = account.name;
isNormalUser = false; isNormalUser = false;
group = vmailGroupName; group = vmailGroupName;
inherit (account) hashedPassword; inherit (account) hashedPassword;

View file

@ -11,17 +11,22 @@
mailserver = { mailserver = {
enable = true; enable = true;
domain = "example.com"; domain = "example.com";
extraDomains = [ "example2.com" ];
hostPrefix = "mail"; 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"; "user1@example2.com" = "user1@example.com";
postmaster = "user1"; "info@example.com" = "user1@example.com";
abuse = "user1"; "postmaster@example.com" = "user1@example.com";
"abuse@example.com" = "user1@example.com";
"info@example2.com" = "user1@example.com";
"postmaster@example2.com" = "user1@example.com";
"abuse@example2.com" = "user1@example.com";
}; };
}; };
}; };