2021-10-22 19:34:28 -07:00
|
|
|
{ lib, config, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.danilafe-blog;
|
|
|
|
sslForDomain = domain: (cfg.ssl == true) || (cfg.ssl."${domain}" or false);
|
|
|
|
anySsl = any (mapAttrsToList (domain: pkg: sslForDomain domain) cfg.domains);
|
2021-10-22 22:50:04 -07:00
|
|
|
virtualHost = domain: package:
|
2021-10-22 19:34:28 -07:00
|
|
|
{
|
2021-10-22 22:50:04 -07:00
|
|
|
virtualHosts."${domain}" = mkMerge [
|
2021-10-22 19:34:28 -07:00
|
|
|
{
|
|
|
|
root = package;
|
|
|
|
}
|
|
|
|
(mkIf (sslForDomain domain) {
|
|
|
|
addSSL = true;
|
|
|
|
enableACME = true;
|
|
|
|
acmeRoot = cfg.challengePath;
|
|
|
|
})
|
|
|
|
];
|
2021-10-22 22:50:04 -07:00
|
|
|
};
|
|
|
|
service = domain:
|
|
|
|
{
|
2021-10-22 19:34:28 -07:00
|
|
|
# Workaround for new configuration setting all of /var to be readonly.
|
|
|
|
# See https://github.com/NixOS/nixpkgs/issues/139310
|
2021-10-22 22:50:04 -07:00
|
|
|
"acme-${domain}".serviceConfig = {
|
2021-10-22 19:34:28 -07:00
|
|
|
ReadWritePaths = [ cfg.challengePath ];
|
|
|
|
};
|
2021-10-22 22:50:04 -07:00
|
|
|
};
|
|
|
|
virtualHosts = mapAttrsToList virtualHost cfg.domains;
|
|
|
|
services = map service (filter sslForDomain (attrNames cfg.domains));
|
2021-10-22 19:34:28 -07:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.danilafe-blog = {
|
|
|
|
enable = mkEnableOption "Daniel's blog service";
|
|
|
|
ssl = mkOption {
|
|
|
|
type = types.either types.bool (types.attrsOf types.bool);
|
|
|
|
default = false;
|
|
|
|
description = "Enable SSL and ACME for all or some domains.";
|
|
|
|
};
|
|
|
|
domains = mkOption {
|
|
|
|
type = types.attrsOf types.package;
|
|
|
|
description = "Attribute set where keys are domains and values are packages to host there.";
|
|
|
|
};
|
|
|
|
challengePath = mkOption {
|
|
|
|
type = types.str;
|
2021-10-22 19:46:56 -07:00
|
|
|
description = "The location for ACME challenges.";
|
2021-10-22 19:34:28 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-10-22 22:50:04 -07:00
|
|
|
config.services.nginx = mkIf cfg.enable (mkMerge (virtualHosts ++ [
|
2021-10-22 19:34:28 -07:00
|
|
|
{
|
|
|
|
# Always enable nginx.
|
2021-10-22 22:50:04 -07:00
|
|
|
enable = true;
|
|
|
|
recommendedGzipSettings = true;
|
2021-10-22 19:34:28 -07:00
|
|
|
}
|
|
|
|
]));
|
2021-10-22 22:50:04 -07:00
|
|
|
config.systemd.services = mkIf cfg.enable (mkMerge services);
|
|
|
|
config.security.acme = mkIf (cfg.enable && anySsl) {
|
|
|
|
# If any domain uses SSL, enable ACME and accept terms.
|
|
|
|
security.acme.email = "danila.fedorin@gmail.com";
|
|
|
|
security.acme.acceptTerms = true;
|
2021-10-22 22:51:52 -07:00
|
|
|
};
|
2021-10-22 19:34:28 -07:00
|
|
|
}
|