1
0
Fork 0
forked from emily/nixfiles
nixfiles-emily/modules/nginx/default.nix

60 lines
2 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;
2024-11-04 18:51:13 +01:00
add_header Alt-Svc 'h3=":443"; ma=7776000; persist=1, h2=":443"; ma=7776000; persist=1';
2024-11-04 21:19:57 +01:00
#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;
2024-04-30 21:55:41 +02:00
'';
createHost = vhostName: vhostCfg: {
2024-11-04 18:51:13 +01:00
extraConfig = lib.optionalString (builtins.hasAttr "extraConfig" vhostCfg) (vhostCfg.extraConfig + "\n" + extraConfig);
2024-04-30 21:55:41 +02:00
forceSSL = true;
#kTLS = true;
2024-11-04 18:51:13 +01:00
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" = {
2024-11-04 21:19:57 +01:00
quic = true;
http3 = true;
# reuseport has to be specified on the quic listener
# when using worker_processes auto;
reuseport = true;
2024-05-11 16:02:39 +02:00
default = true;
forceSSL = true;
useACMEHost = cfg.defaultForbidden;
extraConfig = ''
return 403;
'';
};
};
2024-04-30 21:55:41 +02:00
};
}