From 4f6ade4ed51efb3eb4d07a57ef65d1fbd7722a6c Mon Sep 17 00:00:00 2001 From: alexion Date: Tue, 15 Sep 2026 19:34:35 -0400 Subject: [PATCH] feat(nixos): add local homepage route --- configs/nixos/hosts/pikachu/default.nix | 5 +++ configs/nixos/modules/services/homepage.nix | 2 +- .../nixos/modules/services/reverse-proxy.nix | 35 ++++++++++++++++--- configs/nixos/registry/local-route.nix | 25 +++++++++++++ 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 configs/nixos/registry/local-route.nix diff --git a/configs/nixos/hosts/pikachu/default.nix b/configs/nixos/hosts/pikachu/default.nix index 51008af..5101337 100644 --- a/configs/nixos/hosts/pikachu/default.nix +++ b/configs/nixos/hosts/pikachu/default.nix @@ -150,6 +150,11 @@ address = "10.23.20.110"; }; + registry.local-route.${config.networking.hostName}.homepage = { + domain = "home"; + port = 8082; + }; + guests.copyparty = { enable = true; vlan = 20; diff --git a/configs/nixos/modules/services/homepage.nix b/configs/nixos/modules/services/homepage.nix index 49e3414..d205f1c 100644 --- a/configs/nixos/modules/services/homepage.nix +++ b/configs/nixos/modules/services/homepage.nix @@ -72,7 +72,7 @@ in listenPort = lib.mkOption { type = lib.types.port; - default = 80; + default = 8082; description = "Port for Homepage to bind to."; }; diff --git a/configs/nixos/modules/services/reverse-proxy.nix b/configs/nixos/modules/services/reverse-proxy.nix index 916ca29..75140b8 100644 --- a/configs/nixos/modules/services/reverse-proxy.nix +++ b/configs/nixos/modules/services/reverse-proxy.nix @@ -11,7 +11,7 @@ let mkBackend = address: route: "http://${address}:${toString route.port}"; - mkVirtualHost = host: entry: + mkPublicVirtualHost = host: entry: let compositeEntry = registry.join host entry; route = compositeEntry.route; @@ -19,6 +19,7 @@ let in { inherit host entry; + registryName = "route"; name = route.domain; value = { forceSSL = true; @@ -27,15 +28,41 @@ let }; }; - generatedVirtualHostList = lib.concatLists ( + mkLocalVirtualHost = host: entry: + let + compositeEntry = registry.join host entry; + route = compositeEntry.local-route; + address = compositeEntry.address or (throw "no matching entry in registry.address for ${mkRegistryPath "local-route" host entry}"); + in + { + inherit host entry; + registryName = "local-route"; + name = route.domain; + value = { + forceSSL = false; + enableACME = false; + locations."/".proxyPass = mkBackend address route; + }; + }; + + generatedPublicVirtualHostList = lib.concatLists ( lib.mapAttrsToList ( host: routes: - lib.mapAttrsToList (entry: _route: mkVirtualHost host entry) ( + lib.mapAttrsToList (entry: _route: mkPublicVirtualHost host entry) ( lib.filterAttrs (_entry: route: route.port != null) routes ) ) registry.route ); + generatedLocalVirtualHostList = lib.concatLists ( + lib.mapAttrsToList ( + host: routes: + lib.mapAttrsToList (entry: _route: mkLocalVirtualHost host entry) routes + ) registry.local-route + ); + + generatedVirtualHostList = generatedPublicVirtualHostList ++ generatedLocalVirtualHostList; + virtualHostsByDomain = lib.foldl' ( acc: virtualHost: acc @@ -49,7 +76,7 @@ let domain: virtualHosts: if lib.length virtualHosts > 1 then [ - "duplicate route domain ${domain} in ${lib.concatMapStringsSep " and " (virtualHost: mkRegistryPath "route" virtualHost.host virtualHost.entry) virtualHosts}" + "duplicate reverse-proxy domain ${domain} in ${lib.concatMapStringsSep " and " (virtualHost: mkRegistryPath virtualHost.registryName virtualHost.host virtualHost.entry) virtualHosts}" ] else [ ] diff --git a/configs/nixos/registry/local-route.nix b/configs/nixos/registry/local-route.nix new file mode 100644 index 0000000..5235c5e --- /dev/null +++ b/configs/nixos/registry/local-route.nix @@ -0,0 +1,25 @@ +{ lib, ... }: +{ + checks = [ + { + assertion = lib.registry.predicates.uniqueValue (entry: entry.value.domain); + message = "Local route domains must be unique."; + } + ]; + + type = lib.types.submodule { + options = { + domain = lib.mkOption { + type = lib.types.str; + example = "home"; + description = "Local HTTP domain served by this route entry."; + }; + + port = lib.mkOption { + type = lib.types.port; + example = 8082; + description = "Backend TCP port served by the matching address registry entry."; + }; + }; + }; +}