2021-10-22 19:34:28 -07:00
|
|
|
{ lib, config, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.danilafe-blog;
|
2021-10-23 00:42:03 -07:00
|
|
|
sslForDomain = domain: package: package.ssl;
|
|
|
|
anySsl = any id (mapAttrsToList sslForDomain 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;
|
|
|
|
}
|
2021-10-23 00:42:03 -07:00
|
|
|
(mkIf (sslForDomain domain package) {
|
2021-10-22 19:34:28 -07:00
|
|
|
addSSL = true;
|
|
|
|
enableACME = true;
|
|
|
|
acmeRoot = cfg.challengePath;
|
|
|
|
})
|
|
|
|
];
|
2021-10-22 22:50:04 -07:00
|
|
|
};
|
2021-10-23 00:42:03 -07:00
|
|
|
service = domain: package:
|
2021-10-22 22:50:04 -07:00
|
|
|
{
|
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;
|
2021-10-23 00:42:03 -07:00
|
|
|
services = mapAttrsToList service (filterAttrs sslForDomain cfg.domains);
|
2021-10-22 19:34:28 -07:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.danilafe-blog = {
|
|
|
|
enable = mkEnableOption "Daniel's blog service";
|
|
|
|
domains = mkOption {
|
|
|
|
type = types.attrsOf types.package;
|
2021-10-22 23:12:30 -07:00
|
|
|
default = {};
|
2021-10-22 19:34:28 -07:00
|
|
|
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.
|
2021-10-22 23:14:13 -07:00
|
|
|
email = "danila.fedorin@gmail.com";
|
|
|
|
acceptTerms = true;
|
2021-10-22 22:51:52 -07:00
|
|
|
};
|
2021-10-22 19:34:28 -07:00
|
|
|
}
|