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 { proxy = mkIf cfg.enable {
enable = mkAlmostOptionDefault true;
ssl.enable = false; ssl.enable = false;
upstream = mkAlmostOptionDefault cfg.upstream; upstream = mkAlmostOptionDefault cfg.upstream;
}; };

View file

@ -1,4 +1,42 @@
let 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 locationModule = { config, nixosConfig, name, virtualHost, xvars, gensokyo-zone, lib, ... }: let
inherit (gensokyo-zone.lib) mkJustBefore mkJustAfter mkAlmostOptionDefault mapOptionDefaults coalesce parseUrl; inherit (gensokyo-zone.lib) mkJustBefore mkJustAfter mkAlmostOptionDefault mapOptionDefaults coalesce parseUrl;
inherit (lib.options) mkOption mkEnableOption; inherit (lib.options) mkOption mkEnableOption;
@ -44,6 +82,7 @@ let
type = nullOr str; type = nullOr str;
default = null; default = null;
example = "xvars.get.proxy_host"; example = "xvars.get.proxy_host";
# $upstream_last_server_name is commercial-only :<
}; };
}; };
parsed = { parsed = {
@ -304,9 +343,15 @@ in {
}: let }: let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
in { in {
options = with lib.types; { options.services.nginx = with lib.types; {
services.nginx.virtualHosts = mkOption { virtualHosts = mkOption {
type = attrsOf (submodule [hostModule]); type = attrsOf (submodule [hostModule]);
}; };
stream.servers = mkOption {
type = attrsOf (submoduleWith {
modules = [serverModule];
shorthandOnlyDefinesConfig = false;
});
};
}; };
} }

View file

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

View file

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