refactor(reverse-proxy): use route registry

This commit is contained in:
2026-08-23 23:44:22 -04:00
parent c267a2ec5c
commit ed018806d8
8 changed files with 89 additions and 106 deletions

View File

@@ -35,16 +35,11 @@ let
) (builtins.readDir dir)
);
specialArgs =
let
networkFleetEndpoints = self.lib.networkEndpoints.collectFleetEndpoints self.nixosConfigurations;
in
{
inherit inputs networkFleetEndpoints self;
my = self.lib;
registry = self.lib.registry.collect { hosts = self.nixosConfigurations; };
reverseProxyFleetRoutes = self.lib.reverseProxyRoutes.collectFleetRoutes self.nixosConfigurations networkFleetEndpoints;
};
specialArgs = {
inherit inputs self;
my = self.lib;
registry = self.lib.registry.collect { hosts = self.nixosConfigurations; };
};
bridgeName = id: "br-vlan${toString id}";

View File

@@ -279,13 +279,8 @@ let
registry.address.${config.networking.hostName}.${machineName} = lib.mkIf (cfg.address != null) cfg.address;
modules.reverse-proxy.routes.${machineName} = lib.mkIf cfg.reverseProxy.enable (
reverseProxyRoutes.routeFields (
cfg.reverseProxy
// lib.optionalAttrs (cfg.reverseProxy.backend == null && cfg.reverseProxy.endpoint == null) {
endpoint = "${config.networking.hostName}-${machineName}";
}
)
registry.route.${config.networking.hostName}.${machineName} = lib.mkIf cfg.reverseProxy.enable (
reverseProxyRoutes.routeFields cfg.reverseProxy
);
assertions = [

View File

@@ -32,6 +32,11 @@ in
{
inherit definitions;
at = registry: hostName: entryName:
filterAttrs (_name: value: value != null) (
mapAttrs (_name: hostEntriesByName: (hostEntriesByName.${hostName} or { }).${entryName} or null) registry
);
module = {
options.registry = mapAttrs (_name: registry: registryOption registry) definitions;
};

View File

@@ -42,23 +42,17 @@ let
description = "Full HTTP backend URL that receives proxied traffic.";
};
endpoint = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Fleet network endpoint key used to derive the backend URL.";
};
port = lib.mkOption {
type = lib.types.nullOr (lib.types.ints.between 1 65535);
default = null;
example = 3923;
description = "Backend TCP port used with the fleet network endpoint.";
description = "Backend TCP port used with the matching address registry entry.";
};
scheme = lib.mkOption {
type = lib.types.str;
default = "http";
description = "Backend URL scheme used with the fleet network endpoint.";
description = "Backend URL scheme used with the matching address registry entry.";
};
tls = lib.mkOption {
@@ -80,45 +74,15 @@ let
aliases
path
backend
endpoint
port
scheme
tls
websockets
;
};
endpointBackend = endpoints: route:
let
endpoint = endpoints.${route.endpoint} or (
throw "Unknown reverse-proxy endpoint `${route.endpoint}`. Known endpoints: ${lib.concatStringsSep ", " (lib.attrNames endpoints)}"
);
in
"${route.scheme}://${endpoint.address}:${toString route.port}";
resolvedRouteFields = endpoints: route:
let
routeWithBackend =
if route.backend != null then
route
else if route.endpoint != null && route.port != null then
route // { backend = endpointBackend endpoints route; }
else
throw "Reverse-proxy route `${route.host}${route.path}` needs either backend or endpoint plus port.";
in
routeFields routeWithBackend;
collectFleetRoutes =
hosts: endpoints:
lib.concatMapAttrs (
hostName: host:
lib.mapAttrs' (
routeName: route: lib.nameValuePair "${hostName}-${routeName}" (resolvedRouteFields endpoints route)
) host.config.modules.reverse-proxy.routes
) hosts;
in
{
inherit collectFleetRoutes mkRouteOptions resolvedRouteFields routeFields;
inherit mkRouteOptions routeFields;
routeType = lib.types.submodule (
{ name, ... }: