nixfiles/modules/nginx/default.nix

55 lines
1.7 KiB
Nix
Raw Normal View History

2024-04-30 21:55:41 +02:00
{ config, lib, ... }: let
2024-05-11 16:02:39 +02:00
cfg = config.kyouma.nginx;
2024-04-30 21:55:41 +02:00
extraConfig = ''
add_header Strict-Transport-Security $hsts_header;
#add_header Content-Security-Policy "script-src 'self'; object-src 'none'; base-uri 'none';" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "same-origin" always;
'';
createHost = vhostName: vhostCfg: {
extraConfig = (lib.optionalString (builtins.hasAttr "extraConfig" vhostCfg) vhostCfg.extraConfig) + "\n" + extraConfig;
forceSSL = true;
#kTLS = true;
#http3 = true;
#quic = true;
2024-05-11 16:02:39 +02:00
} //
lib.optionalAttrs (!(builtins.hasAttr "useACMEHost" vhostCfg)) {
2024-04-30 21:55:41 +02:00
enableACME = true;
2024-05-11 16:02:39 +02:00
} //
lib.optionalAttrs (builtins.hasAttr "redirectTo" vhostCfg) {
2024-04-30 21:55:41 +02:00
enableACME = false;
useACMEHost = vhostCfg.redirectTo;
globalRedirect = vhostCfg.redirectTo;
2024-05-11 16:02:39 +02:00
} //
(builtins.removeAttrs vhostCfg [ "redirectTo" "extraConfig" ]);
2024-04-30 21:55:41 +02:00
in {
options = {
kyouma.nginx.virtualHosts = lib.mkOption {
type = with lib.types; nullOr anything;
default = null;
};
2024-05-11 16:02:39 +02:00
kyouma.nginx.defaultForbidden = lib.mkOption {
type = with lib.types; nullOr str;
default = null;
};
2024-04-30 21:55:41 +02:00
};
config = {
2024-05-11 16:02:39 +02:00
services.nginx.virtualHosts = lib.optionalAttrs (cfg.virtualHosts != null) (
builtins.mapAttrs (createHost) cfg.virtualHosts) //
lib.optionalAttrs (cfg.defaultForbidden != null) {
"redirect" = {
default = true;
forceSSL = true;
reuseport = true;
useACMEHost = cfg.defaultForbidden;
extraConfig = ''
return 403;
'';
};
};
2024-04-30 21:55:41 +02:00
};
}