Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Server Proxy — Unified Caddy Reverse Proxy

Purpose

The Proxy submodule provides a unified interface for exposing internal services through Caddy. It handles virtual host configuration, automatic SSL via ACME, OAuth2 authentication with Kanidm, static API key authentication, and public exposure through Cloudflared tunnels. It abstracts the complexity of reverse proxying by allowing services to define their proxy requirements within their own module configuration, automatically coordinating between backend hosts and the primary IO host to ensure ports are open and traffic is correctly routed.

Entry Point

  • Main file: modules/nixos/server/proxy/default.nix
  • Supporting file: modules/nixos/server/proxy/options.nix
  • Supporting file: modules/nixos/server/proxy/config.nix
  • Supporting file: modules/nixos/server/proxy/kanidm.nix
  • Supporting file: modules/nixos/server/proxy/extensions.nix
  • Supporting file: modules/nixos/server/proxy/extensions/l4.nix

Options

server.proxy.extensions

Typeattribute set of (submodule)
Default{ }

Registry of proxy extensions. Each extension provides config functions that are injected into vhost Caddy blocks, sorted by priority.


server.proxy.extensions.<name>.config

Typefunction that evaluates to a(n) function that evaluates to a(n) function that evaluates to a(n) string

Function: vhostName -> vhostAttrSet -> hostConfig -> string. Returns Caddy directives to inject, or ‘’ for no-op. The vhostAttrSet includes the resolved extraConfig (already localhost-replaced) as _resolvedExtraConfig.


server.proxy.extensions.<name>.consumesExtraConfig

Typeboolean
Defaultfalse

Whether this extension embeds extraConfig inside its output. When true, config.nix skips the post-extension extraConfig append for this vhost.


server.proxy.extensions.<name>.enable

Typeboolean
Defaultfalse

Whether this extension is globally enabled. Each extension SHOULD auto-detect whether it has work to do and set this to true via mkDefault in its module config. User can explicitly override to force-disable (higher merge priority than mkDefault).


server.proxy.extensions.<name>.globalConfig

Typefunction that evaluates to a(n) string
Default<function>

Function: hostConfig -> string. Returns Caddy directives to inject into the top-level globalConfig block. Only called on the IO primary host. Sorted by priority across extensions.


server.proxy.extensions.<name>.priority

Typesigned integer
Default100

Lower values = earlier in Caddy config. Priority ranges: 0-49 reserved, 50-99 auth, 100-199 general, 200+ post-processing.


server.proxy.extensions.<name>.vhostModule

Typenull or module
Defaultnull

Optional module to inject into each vhost submodule. Use options.<extensionName> (relative to vhost scope) to declare per-vhost options.


server.proxy.kanidmContexts

Typeattribute set of (submodule)
Default{ }

Shared Kanidm OAuth2 context configurations.


server.proxy.kanidmContexts.<name>.allowGroups

Typelist of string
Default[ ]
Example[ "idm_all_persons@auth.racci.dev" "admins@auth.racci.dev" ]

Default list of Kanidm groups allowed to access virtualHosts using this context.


server.proxy.kanidmContexts.<name>.authDomain

Typenull or string
Defaultnull
Example"auth.example.com"

The domain where Kanidm is hosted. Defaults to auth.<data.site.domain> if not specified.


server.proxy.kanidmContexts.<name>.cookieDomain

Typenull or string
Defaultnull
Example"example.com"

Cookie domain for the portal session cookies. When set, the cookie is shared by every host under this domain, which is what makes authentication carry across those hosts. A leading dot is accepted and stripped by the plugin.


server.proxy.kanidmContexts.<name>.scopes

Typelist of string
Default[ "openid" "email" "profile" "groups" ]

OAuth scopes to request from Kanidm.


server.proxy.kanidmContexts.<name>.sharedPortal

Typeboolean
Defaultfalse

Serve one authentication portal for every virtual host using this context. A single portal means a single session, so a login on one host is valid on the others once they share cookieDomain.


server.proxy.kanidmContexts.<name>.tokenLifetime

Typesigned integer
Default3600

Token lifetime in seconds for the authentication portal.


server.proxy.kanidmPortalCss

Typenull or absolute path
Default/nix/store/z8pavkbghx2fcr1ksmy8pzv8d39r1gxm-source/modules/nixos/server/proxy/portal.css

Stylesheet applied to every Kanidm authentication portal. Defaults to the dark theme shipped with this module. Set to null to use the plugin’s own styling.


server.proxy.virtualHosts

Typeattribute set of (submodule)
Default{ }

Virtual hosts to be handled by the IO server and forwarded to the respective backend.


server.proxy.virtualHosts.<name>.aliases

Typelist of string
Default[ ]

A list of virtual host names that should be routed using this configuration. Options added here will inherit the base domain from data.site.domain.


server.proxy.virtualHosts.<name>.baseUrl

Typestring
Default${subdomain}.${self.data.site.domain}

The base url including the configured base domain name.


server.proxy.virtualHosts.<name>.extensions

Typenull or (list of string)
Defaultnull

List of extension names to enable for this virtual host. When null (default), all globally enabled extensions apply. When set to a list, only those named extensions apply. Set to [] to disable all extensions for this vhost.


server.proxy.virtualHosts.<name>.extraConfig

Typestring
Default""

Configuration to be placed in the caddy virtualHost extraConfig.


server.proxy.virtualHosts.<name>.kanidm

Typenull or (submodule)
Defaultnull

Enable Kanidm OAuth2 authentication for this virtual host.


server.proxy.virtualHosts.<name>.kanidm.allowGroups

Typelist of string
Default[ ]
Example[ "idm_all_persons@auth.racci.dev" "admins@auth.racci.dev" ]

Default list of Kanidm groups allowed to access virtualHosts using this context.


server.proxy.virtualHosts.<name>.kanidm.authDomain

Typenull or string
Defaultnull
Example"auth.example.com"

The domain where Kanidm is hosted. Defaults to auth.<data.site.domain> if not specified.


server.proxy.virtualHosts.<name>.kanidm.bypassPaths

Typelist of string
Default[ ]
Example[ "/health" "/api/webhooks/*" ]

List of path patterns that never require authentication.


server.proxy.virtualHosts.<name>.kanidm.context

Typestring
Default"‹name›"

The OAuth context name for this virtual host.


server.proxy.virtualHosts.<name>.kanidm.logoutPath

Typenull or string
Defaultnull
Example"/logout"

Path that signs the user out instead of being proxied. It clears the proxy cookies and ends the Kanidm session, so the next request cannot be silently re-authenticated.


server.proxy.virtualHosts.<name>.kanidm.protectedPaths

Typelist of string
Default[ ]
Example[ "/ui/*" ]

Path patterns that require authentication. When empty, the whole host requires authentication. When set, only matching paths do and every other path is served without authentication, which keeps machine endpoints reachable. bypassPaths still takes precedence for the paths it lists.


server.proxy.virtualHosts.<name>.kanidm.scopes

Typelist of string
Default[ "openid" "email" "profile" "groups" ]

OAuth scopes to request from Kanidm.


server.proxy.virtualHosts.<name>.kanidm.tokenLifetime

Typesigned integer
Default3600

Token lifetime in seconds for the authentication portal.


server.proxy.virtualHosts.<name>.l4

Typenull or (submodule)
Defaultnull

This option has no description.


server.proxy.virtualHosts.<name>.l4.config

Typestring
Default""

Configuration for the L4 plugin.


server.proxy.virtualHosts.<name>.l4.listenPort

Type16 bit unsigned integer; between 0 and 65535 (both inclusive)

Port to listen on for L4 traffic.


server.proxy.virtualHosts.<name>.l4.protocol

Typeone of "tcp", "udp"
Default"tcp"

Protocol for L4 listener.


server.proxy.virtualHosts.<name>.listenPorts

Typenon-empty (list of 16 bit unsigned integer; between 0 and 65535 (both inclusive))
Default[ 443 ]

Port(s) to listen on for incoming traffic for this virtual host. If multiple ports are specified, the virtual host will be accessible on all of them.


server.proxy.virtualHosts.<name>.ports

Typelist of 16 bit unsigned integer; between 0 and 65535 (both inclusive)
Default[ ]

Ports to be opened from the host for IO Hosts to forward traffic to.


server.proxy.virtualHosts.<name>.public

Typeboolean
Defaultfalse

When enabled this service will be accessible to the public via Cloudflared Tunnels.


server.proxy.virtualHosts.<name>.requireApiKey

Typenull or (submodule)
Defaultnull

This option has no description.


server.proxy.virtualHosts.<name>.requireApiKey.bypassPaths

Typelist of string
Default[ ]
Example[ "/health" "/api/webhooks/*" ]

List of path patterns that bypass API key authentication.


server.proxy.virtualHosts.<name>.requireApiKey.enable

Typeboolean
Defaultfalse

Enable API key authentication for this virtual host.


server.proxy.virtualHosts.<name>.useAcmeCerts

Typeboolean
Defaulttrue

Whether to generate and use ACME certificates for this virtual host. If false, you must provide your own TLS configuration in extraConfig via the caddy tls directive.


Architecture / Services / Scope

File Layout

  • default.nix — Logic and helpers: resolving OAuth contexts and mapping local addresses to backend hostnames.
  • options.nix — Option definitions for virtual hosts and shared contexts.
  • config.nix — Caddy integration: generation of services.caddy.virtualHosts and ACME certificate requests. L4 (TCP/UDP) forwarding is handled by the l4 extension, not by config.nix.
  • kanidm.nix — Authentication security: generates the Caddy security block, including identity providers, portals, and authorisation policies.
  • extensions.nix — System integration: connects the proxy to the dashboard, Cloudflared tunnels, and automates Kanidm client provisioning.

Extension System

The proxy module supports a registry-based extension system. Extensions are self-contained modules that inject Caddy directives into virtual host configurations — without modifying proxy internals.

Extension Registry

Extensions register themselves via server.proxy.extensions.<name>, an attribute set of submodules. Each extension has:

FieldTypeDefaultDescription
priorityint100Lower values = earlier Caddy config placement. Ranges: 0-49 reserved, 50-99 auth, 100-199 general, 200+ post-processing
enableboolfalseGlobally enabled. Set via mkDefault based on detected config
consumesExtraConfigboolfalseWhen true, the extension embeds vh._resolvedExtraConfig in its output. config.nix skips appending raw extraConfig
configvhostName -> vhostAttrSet -> hostConfig -> strrequiredPer-vhost Caddy directive generator
globalConfighostConfig -> str_ → ""Top-level Caddy globalConfig directives
vhostModulenullOr deferredModulenullPer-vhost option declarations

Per-Vhost Extension Selection

Each vhost has server.proxy.virtualHosts.<name>.extensions (default null = all enabled extensions). Set to a list of extension names for selective enablement, or [] to disable all extensions.

Config Function Signature

config :: vhostName -> vhostAttrSet -> hostConfig -> string

Arguments:

  • vhostName (str): The vhost’s attribute name (e.g., "grafana").
  • vhostAttrSet: The full vhost attribute set, including _resolvedExtraConfig (user’s extraConfig with replaceLocalHost applied) and _name.
  • hostConfig: Full host-level NixOS config.

GlobalConfig Function Signature

globalConfig :: hostConfig -> string

Called once per enabled extension on the IO primary host. Output concatenated into services.caddy.globalConfig, sorted by extension priority.

Auto-Enable Pattern

Extensions auto-detect whether they have work to do using mkDefault; users can force-disable with explicit enable = false.

Priority Ordering

Extensions sort by priority ascending. Equal priorities break alphabetically by extension name. Extensions with lower priority numbers generate config earlier.

Authoring a New Extension

  1. Create file: modules/nixos/server/proxy/extensions/<name>.nix
  2. Import in proxy/default.nix.
  3. Set server.proxy.extensions.<name> with priority, config function, etc.
  4. Declare per-vhost options via options.server.proxy.virtualHosts with attrsOf (submodule ...).
  5. Use proxyLib for helpers: replaceLocalHost, resolveKanidmContext, hasAnyKanidm, portalName, sanitiseName.

API Key Auth Extension

The api-key-auth extension provides static API key authentication for virtual hosts. When enabled, requests must include a valid Req-API-Key header matching a securely generated secret. Bypass paths are supported per vhost. Mutual exclusivity with Kanidm on the same vhost is enforced by the existing consumesExtraConfig assertion.

Migrated Extensions

ExtensionPriorityPurpose
l410L4 TCP/UDP forwarding (layer4 Caddy block + firewall ports)
kanidm50Kanidm OAuth2 authentication per vhost
api-key-auth50Static API key authentication per vhost (with bypass paths)
dashboard200Auto-generate dashboard items
cloudflared200Cloudflared tunnel ingress

Secrets

Kanidm OAuth2 Context

Authentication requires specific secrets per context, managed via sops-nix:

  1. KANIDM/OAUTH2/<UPPER_CONTEXT>_SECRET: OAuth2 client secret. The Identity Coordinator uses it for Kanidm provisioning; the IO Coordinator uses the same value for Caddy.
  2. PROXY_AUTH/KANIDM/<UPPER_CONTEXT>_SHARED_KEY: Shared key used by Caddy to sign and verify authentication tokens.

On the IO Coordinator, the module renders PROXY_AUTH/KANIDM/<UPPER_CONTEXT>_ENV and loads it as a Caddy environment file with OAUTH_<UPPER_CONTEXT>_CLIENT_SECRET and <UPPER_CONTEXT>_SHARED_KEY. The OAuth2 secret value must match between the Identity Coordinator and IO Coordinator hosts when they are separate.

Kanidm Session Sharing and Path Gating

By default each virtual host gets its own authentication portal and a host-only cookie, so two hosts sharing one OAuth context still prompt twice. Sharing a session is opt-in per context via server.proxy.kanidmContexts.<context>:

OptionDefaultEffect
sharedPortalfalseOne portal named <context>_portal for every vhost using the context, instead of one per vhost
cookieDomainnullEmits cookie domain <value>; required when sharedPortal is set. A leading dot is stripped.

With both set, the session cookie is scoped to the parent domain and validated by one portal instance, so a login on one host is accepted on the others. The cookie prefix is derived from the context name (<UPPER_CONTEXT>_SESSION_ID, <UPPER_CONTEXT>_ACCESS_TOKEN), and the matching policy cookie names are emitted for every vhost in that context. Enabling this renames cookies, so existing sessions are dropped once.

The policy points at the absolute OAuth2 endpoint of its own host, https://<host>/auth/oauth2/<context>, which the plugin uses to build the provider callback and the post-login redirect. Both /auth and /auth/* are matched with a handle so the portal resolves before the authorize handle, and the authenticate handler declares that mount with at /auth, which requires caddy-security v1.6 or newer.

allowGroups feeds two places: the allow groups lines of the Caddy policy, and the scope map of the Kanidm OAuth2 client that the extension provisions for the context. Each group in the resolved set gets a scope map entry carrying the context scopes, with the domain stripped (sysadmin, not sysadmin@auth.racci.dev). Groups may be declared on the context or on the virtual host; the union is used. A context whose hosts resolve no groups fails an assertion, because Kanidm then issues no token at all and every login ends on an Access Denied page.

Each portal also carries a transform user block that adds the authp/user role to the issued token. The portal’s own access list only admits its reserved roles (authp/admin, authp/user, authp/guest), while Kanidm sends group SPNs only. Without the transform the login succeeds, the access token is issued, and the portal then rejects that token on the next request and deletes its cookie, which restarts the login. This role is not an authorization decision: allowGroups on the policy is.

The portal only honours a post-login redirect_url that matches a declared trusted target. Every host and alias of a virtual host using the portal gets one:

trust login redirect uri domain exact <host> path prefix /

Without it the login completes and the browser is sent to the plugin’s own page, /auth/portal, instead of the page the user asked for. The plugin’s /auth/profile/ page is its own account UI and is not backed by an OAuth2 provider; it answers oauth is not supported with profile API and is unused here.

Path gating uses server.proxy.virtualHosts.<name>.kanidm:

OptionDefaultEffect
bypassPaths[ ]Paths that never require authentication
protectedPaths[ ]Paths that require authentication; everything else is served unauthenticated

An empty protectedPaths means the whole host requires authentication. Both lists can be combined, and bypassPaths wins for the paths it lists. Use protectedPaths for hosts that mix browser and machine traffic, such as a storage component whose UI should be gated while its data plane stays reachable.

Signing Out

Applications behind the proxy have no session of their own, so a logout button in their UI does nothing. logoutPath intercepts such a path instead of proxying it:

OptionDefaultEffect
logoutPathnullPath that clears the proxy cookies and ends the Kanidm session

On that path the extension emits Set-Cookie deletions for <PREFIX>_ACCESS_TOKEN, <PREFIX>_SESSION_ID and <PREFIX>_REDIRECT_URL, then redirects to https://<authDomain>/ui/logout. Clearing only the proxy cookies is not enough: Kanidm would authenticate the next request silently. Kanidm ignores post-logout redirect parameters, so the browser ends on the Kanidm login page.

Portal Theme

Every portal emits a ui block that selects the basic theme and a stylesheet from server.proxy.kanidmPortalCss:

OptionDefaultEffect
kanidmPortalCssthe module’s portal.cssStylesheet served with the portal; the default overrides the theme variables with a dark palette

portal.css only overrides CSS variables (--brand-*) and color-scheme, so it keeps working when the plugin changes its markup. Set the option to null to fall back to the plugin’s own colours. The file is read from its store path at request time, so it needs no caddy adapt cycle.

API Key Auth

Secrets are auto-generated via sops at PROXY_AUTH/<VHOST_NAME>_API_KEY, injected via systemd LoadCredential.

Operational Notes / Assumptions

  • Caddy Integration: The module assumes the existence of a default Caddy snippet for common headers and security settings. When public is enabled, it also expects a public snippet.
  • Dashboard Integration: Services defined in server.proxy.virtualHosts are automatically added to the server dashboard with default titles and icons derived from the host name.
  • Layer 4 Forwarding: L4 forwarding uses the caddy.layer4 plugin for non-HTTP traffic like database connections or SSH. Managed by the l4 extension (modules/nixos/server/proxy/extensions/l4.nix), which auto-enables when any vhost has l4 != null.
  • Kanidm Bootstrap Ordering: OAuth identity providers use delayed metadata discovery (delay_start with retries). This avoids startup deadlocks when the metadata endpoint is served through the same Caddy instance.

References