chore(nginx): move proxy logic out of stream.nix

This commit is contained in:
arcnmx 2024-04-23 13:31:22 -07:00
parent b0a3da835c
commit f286ff4c72
4 changed files with 50 additions and 22 deletions

View file

@ -38,6 +38,7 @@ let
'';
};
proxy = mkIf cfg.enable {
enable = mkAlmostOptionDefault true;
ssl.enable = false;
upstream = mkAlmostOptionDefault cfg.upstream;
};

View file

@ -1,4 +1,42 @@
let
serverModule = {config, name, options, gensokyo-zone, lib, ...}: let
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.modules) mkIf mkMerge mkAfter;
cfg = config.proxy;
in {
options = with lib.types; {
proxy = {
enable = mkEnableOption "proxy_pass";
transparent.enable = mkEnableOption "proxy_bind transparent";
ssl = {
enable = mkEnableOption "ssl upstream";
verify = mkEnableOption "proxy_ssl_verify";
};
url = mkOption {
type = str;
};
};
};
config = let
warnProxy = lib.warnIf (!cfg.enable && options.proxy.url.isDefined) "nginx.stream.servers.${name}.proxy.url set without proxy.enable";
in {
streamConfig = warnProxy (mkMerge [
(mkIf cfg.transparent.enable ''
proxy_bind $remote_addr transparent;
'')
(mkIf cfg.ssl.enable
"proxy_ssl on;"
)
(mkIf (cfg.ssl.enable && cfg.ssl.verify)
"proxy_ssl_verify on;"
)
(mkIf cfg.enable (mkAfter
"proxy_pass ${cfg.url};"
))
]);
};
};
locationModule = { config, nixosConfig, name, virtualHost, xvars, gensokyo-zone, lib, ... }: let
inherit (gensokyo-zone.lib) mkJustBefore mkJustAfter mkAlmostOptionDefault mapOptionDefaults coalesce parseUrl;
inherit (lib.options) mkOption mkEnableOption;
@ -44,6 +82,7 @@ let
type = nullOr str;
default = null;
example = "xvars.get.proxy_host";
# $upstream_last_server_name is commercial-only :<
};
};
parsed = {
@ -304,9 +343,15 @@ in {
}: let
inherit (lib.options) mkOption;
in {
options = with lib.types; {
services.nginx.virtualHosts = mkOption {
options.services.nginx = with lib.types; {
virtualHosts = mkOption {
type = attrsOf (submodule [hostModule]);
};
stream.servers = mkOption {
type = attrsOf (submoduleWith {
modules = [serverModule];
shorthandOnlyDefinesConfig = false;
});
};
};
}

View file

@ -5,7 +5,7 @@
...
}: let
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.modules) mkIf mkMerge mkAfter mkOptionDefault;
inherit (lib.modules) mkIf mkMerge mkOptionDefault;
inherit (lib.attrsets) mapAttrsToList;
cfg = config.services.nginx.stream;
serverModule = {config, ...}: {
@ -25,30 +25,11 @@
type = lines;
internal = true;
};
proxy = {
ssl = {
enable = mkEnableOption "ssl upstream";
verify = mkEnableOption "proxy_ssl_verify";
};
url = mkOption {
type = nullOr str;
default = null;
};
};
};
config = {
streamConfig = mkMerge [
config.extraConfig
(mkIf config.proxy.ssl.enable
"proxy_ssl on;"
)
(mkIf (config.proxy.ssl.enable && config.proxy.ssl.verify)
"proxy_ssl_verify on;"
)
(mkIf (config.proxy.url != null) (mkAfter
"proxy_pass ${config.proxy.url};"
))
];
serverBlock = mkOptionDefault ''
server {

View file

@ -225,6 +225,7 @@ let
else assert proxyUpstream.enable; proxyUpstream.name;
in {
proxy = {
enable = mkIf (config.proxy.upstream != null) true;
url = mkIf (config.proxy.upstream != null) (mkAlmostOptionDefault proxyPass);
ssl.enable = mkIf (hasUpstream && proxyUpstream.ssl.enable) (mkAlmostOptionDefault true);
};