Files
nixos/modules/webserver.nix
Joakim Repomaa 71e65bf687 remove webroot
2025-02-08 15:42:01 +02:00

99 lines
2.4 KiB
Nix

{ lib, config, ... }:
let
cfg = config.modules.webserver;
nginxVhost = options: {
http2 = true;
forceSSL = true;
enableACME = true;
acmeRoot = lib.mkIf cfg.acme.dnsChallenge null;
} // options;
nginxProxy = options: {
proxyWebsockets = true;
} // options;
types = {
location = lib.types.submodule {
options = {
proxy = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
extraConfig = lib.mkOption {
type = lib.types.str;
default = "";
};
};
};
vhost = lib.types.submodule {
options = {
proxyBuffering = lib.mkOption {
type = lib.types.bool;
default = true;
};
locations = lib.mkOption {
type = lib.types.attrsOf types.location;
};
};
};
};
in
{
options.modules.webserver = {
enable = lib.mkEnableOption "Enable nginx";
acme = {
dnsChallenge = lib.mkEnableOption "Enable DNS challenge";
dnsProvider = lib.mkOption {
type = lib.types.str;
default = "hetzner";
};
environmentFile = lib.mkOption {
type = lib.types.str;
default = "/var/secrets/lego";
};
};
vHosts = lib.mkOption {
type = lib.types.attrsOf types.vhost;
default = { };
};
};
config = lib.mkIf cfg.enable {
services.nginx = {
enable = lib.mkDefault true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
recommendedBrotliSettings = true;
recommendedGzipSettings = true;
recommendedZstdSettings = true;
recommendedOptimisation = true;
virtualHosts = lib.mapAttrs
(_: { proxyBuffering, locations }: nginxVhost {
extraConfig = lib.mkIf (!proxyBuffering) ''
proxy_buffering off;
'';
locations = lib.mapAttrs
(_: { proxy, extraConfig }: lib.mergeAttrsList [
{ inherit extraConfig; }
(if (lib.isString proxy) then (nginxProxy { proxyPass = proxy; }) else { })
])
locations;
})
cfg.vHosts;
};
security.acme = {
acceptTerms = true;
defaults = {
email = "admin@j.repomaa.com";
dnsProvider = lib.mkIf cfg.acme.dnsChallenge cfg.acme.dnsProvider;
};
};
modules.firewall.allInterfaces = [ "web" ];
};
}