mirror of
https://github.com/gensokyo-zone/infrastructure.git
synced 2026-02-09 04:19:19 -08:00
181 lines
5.2 KiB
Nix
181 lines
5.2 KiB
Nix
let
|
|
xHeadersProxied = { xvars }: ''
|
|
${xvars.init "forwarded_for" "$proxy_add_x_forwarded_for"}
|
|
if ($http_x_forwarded_proto) {
|
|
${xvars.init "scheme" "$http_x_forwarded_proto"}
|
|
}
|
|
${xvars.init "https" ""}
|
|
if (${xvars.get.scheme} = https) {
|
|
${xvars.init "https" "on"}
|
|
}
|
|
if ($http_x_real_ip) {
|
|
${xvars.init "remote_addr" "$http_x_real_ip"}
|
|
}
|
|
if ($http_x_forwarded_host) {
|
|
${xvars.init "host" "$http_x_forwarded_host"}
|
|
}
|
|
if ($http_x_forwarded_server) {
|
|
${xvars.init "forwarded_server" "$http_x_forwarded_server"}
|
|
}
|
|
'';
|
|
locationModule = { config, virtualHost, xvars, gensokyo-zone, lib, ... }: let
|
|
inherit (gensokyo-zone.lib) mkJustBefore mkAlmostOptionDefault;
|
|
inherit (lib.options) mkOption;
|
|
inherit (lib.modules) mkIf mkMerge mkOptionDefault;
|
|
cfg = config.proxied;
|
|
in {
|
|
options = with lib.types; {
|
|
proxied = {
|
|
enable = mkOption {
|
|
type = enum [ false true "cloudflared" ];
|
|
default = false;
|
|
};
|
|
enabled = mkOption {
|
|
type = bool;
|
|
readOnly = true;
|
|
};
|
|
};
|
|
};
|
|
config = let
|
|
emitVars = cfg.enabled && !virtualHost.proxied.enabled;
|
|
in {
|
|
proxied = {
|
|
enabled = mkOptionDefault (virtualHost.proxied.enabled || cfg.enable != false);
|
|
};
|
|
proxy = {
|
|
headers = {
|
|
enableRecommended = mkIf cfg.enabled (mkAlmostOptionDefault true);
|
|
rewriteReferer.enable = mkIf cfg.enabled (mkAlmostOptionDefault true);
|
|
};
|
|
redirect = mkIf cfg.enabled {
|
|
enable = mkAlmostOptionDefault true;
|
|
fromScheme = mkAlmostOptionDefault xvars.get.proxy_scheme;
|
|
};
|
|
};
|
|
fastcgi = {
|
|
passHeaders = {
|
|
X-Accel-Buffering = mkOptionDefault true;
|
|
};
|
|
};
|
|
xvars.enable = mkIf cfg.enabled true;
|
|
extraConfig = mkMerge [
|
|
(mkIf emitVars (
|
|
mkJustBefore (xHeadersProxied { inherit xvars; })
|
|
))
|
|
];
|
|
};
|
|
};
|
|
hostModule = { config, nixosConfig, xvars, gensokyo-zone, lib, ... }: let
|
|
inherit (gensokyo-zone.lib) mkAlmostOptionDefault orderJustBefore unmerged;
|
|
inherit (lib.options) mkOption;
|
|
inherit (lib.modules) mkIf mkOrder mkDefault;
|
|
inherit (nixosConfig.services) nginx;
|
|
cfg = config.proxied;
|
|
in {
|
|
options = with lib.types; {
|
|
proxied = {
|
|
enable = mkOption {
|
|
type = enum [ false true "cloudflared" ];
|
|
default = false;
|
|
};
|
|
enabled = mkOption {
|
|
type = bool;
|
|
default = cfg.enable != false;
|
|
};
|
|
cloudflared = {
|
|
ingressSettings = mkOption {
|
|
type = unmerged.types.attrs;
|
|
};
|
|
getIngress = mkOption {
|
|
type = functionTo unspecified;
|
|
};
|
|
};
|
|
};
|
|
locations = mkOption {
|
|
type = attrsOf (submoduleWith {
|
|
modules = [ locationModule ];
|
|
shorthandOnlyDefinesConfig = true;
|
|
});
|
|
};
|
|
};
|
|
|
|
config = let
|
|
listenProxied = cfg.enabled;
|
|
in {
|
|
proxied = {
|
|
cloudflared = let
|
|
listen = config.listen'.proxied;
|
|
scheme = if listen.ssl then "https" else "http";
|
|
in mkIf (cfg.enable == "cloudflared") {
|
|
ingressSettings.${config.serverName} = {
|
|
service = "${scheme}://localhost:${toString listen.port}";
|
|
originRequest.${if scheme == "https" then "noTLSVerify" else null} = true;
|
|
};
|
|
getIngress = {}: unmerged.mergeAttrs cfg.cloudflared.ingressSettings;
|
|
};
|
|
};
|
|
xvars.enable = mkIf cfg.enabled true;
|
|
local.denyGlobal = mkIf listenProxied (mkDefault true);
|
|
listen' = mkIf listenProxied {
|
|
proxied = {
|
|
addr = "[::]";
|
|
port = mkAlmostOptionDefault nginx.proxied.listenPort;
|
|
};
|
|
};
|
|
extraConfig = mkIf (cfg.enabled && config.xvars.enable) (
|
|
mkOrder (orderJustBefore + 25) (xHeadersProxied { inherit xvars; })
|
|
);
|
|
};
|
|
};
|
|
in {
|
|
config,
|
|
system,
|
|
lib,
|
|
...
|
|
}: let
|
|
inherit (lib.options) mkOption;
|
|
inherit (lib.modules) mkIf mkOptionDefault;
|
|
inherit (lib.attrsets) attrValues;
|
|
inherit (lib.lists) any;
|
|
inherit (config.services) nginx;
|
|
cfg = nginx.proxied;
|
|
in {
|
|
options.services.nginx = with lib.types; {
|
|
proxied = {
|
|
enabled = mkOption {
|
|
type = bool;
|
|
};
|
|
listenPort = mkOption {
|
|
type = port;
|
|
default = 9080;
|
|
};
|
|
};
|
|
virtualHosts = mkOption {
|
|
type = attrsOf (submodule [hostModule]);
|
|
};
|
|
};
|
|
config = {
|
|
services.nginx = let
|
|
hasProxiedHosts = any (virtualHost: virtualHost.enable && virtualHost.proxied.enabled) (attrValues nginx.virtualHosts);
|
|
in {
|
|
proxied = {
|
|
enabled = mkOptionDefault hasProxiedHosts;
|
|
};
|
|
upstreams' = {
|
|
nginx'proxied = mkIf cfg.enabled {
|
|
servers.local = {
|
|
accessService = {
|
|
system = system.name;
|
|
name = "nginx";
|
|
port = "proxied";
|
|
};
|
|
};
|
|
};
|
|
};
|
|
# TODO: virtualHosts.fallback'proxied.reuseport = true;
|
|
};
|
|
networking.firewall.interfaces.lan = mkIf nginx.enable {
|
|
allowedTCPPorts = mkIf cfg.enabled [ cfg.listenPort ];
|
|
};
|
|
};
|
|
}
|