traefik
Modern HTTP reverse proxy
Examples
Simple (Cloudflare)
{config, ...}: {
nps.stacks.traefik = {
enable = true;
domain = "example.com";
# Token will be used to fetch Letsencrypt wildcard certificates automatically (DNS challenge)
extraEnv = {
CF_DNS_API_TOKEN.fromFile = config.sops.secrets."traefik/cf_api_token".path;
};
};
}With different DNS provider
{config, ...}: {
nps.stacks.traefik = {
enable = true;
domain = "example.com";
staticConfig.certificatesResolvers.letsencrypt.acme.dnsChallenge.provider = "porkbun";
extraEnv = {
PORKBUN_API_KEY.fromFile = config.sops.secrets."traefik/porkbun_api_key".path;
PORKBUN_SECRET_API_KEY.fromFile = config.sops.secrets."traefik/porkbun_secret_api_key".path;
};
};
}With Geoblock
{config, ...}: {
nps.stacks.traefik = {
enable = true;
domain = "example.com";
extraEnv.CF_DNS_API_TOKEN.fromFile = config.sops.secrets."traefik/cf_api_token".path;
# For exposed services, we can limit access to certain countries using a geoblock middleware
geoblock.allowedCountries = ["DE"];
};
}With file provider
{config, ...}: {
nps.stacks.traefik = {
enable = true;
domain = "example.com";
extraEnv.CF_DNS_API_TOKEN.fromFile = config.sops.secrets."traefik/cf_api_token".path;
provider = "file";
};
}Stack Options
nps.stacks.traefik.crowdsec.enableLogCollection
Whether logs from Traefik should be collected by CrowdSec. Enabling this will configure the acquis settings for CrowdSec.
nps.stacks.traefik.crowdsec.middleware.bouncerKeyFile
Path to the file containing the key for the Traefik bouncer.
If the middleware option is enabled, a Bouncer will be automatically provisioned in CrowdSec. Also a new crowdsec middleware will be registered in Traefik and added to the public chain.
This will block requests to exposed services that are detected as malicious by Crowdsec.
You can generate a bouncer key using openssl: openssl rand -hex 32
nps.stacks.traefik.crowdsec.middleware.enable
Whether to setup a Traefik middleware.
Make sure to also configure the bouncerKeyFile option.
nps.stacks.traefik.domain
Base domain handled by Traefik
nps.stacks.traefik.dynamicConfig
Dynamic configuration for Traefik.
By default, the module will setup two middlewares: private & public.
The private middleware (applied by default to all services) will only allow access from internal networks.
The public middleware will allow access from the internet. It will be configured
with a rate limit, security headers and a geoblock plugin (if enabled). If enabled, Crowdsec will also
be added to the public middleware chain.
nps.stacks.traefik.enable
Wheter to enable Traefik. The Traefik stack ships preconfigured with a dynamic and static configuration.
nps.stacks.traefik.enableGrafanaAccessLogDashboard
Whether to enable Grafana Access Log Dashboard.
nps.stacks.traefik.enableGrafanaMetricsDashboard
Whether to enable Grafana Metrics Dashboard.
nps.stacks.traefik.enablePrometheusExport
Whether to enable Prometheus Export.
nps.stacks.traefik.extraEnv
Extra environment variables to set for the container. Variables can be either set directly or sourced from a file (e.g. for secrets).
attribute set of (null or boolean or signed integer or string or absolute path or (submodule))
{ }
{
CF_DNS_API_TOKEN = {
fromFile = "/run/secrets/secret_name";
};
TRAEFIK_LOG_LEVEL = "ERROR";
}
nps.stacks.traefik.geoblock.allowedCountries
List of allowed country codes (ISO 3166-1 alpha-2 format) See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements
nps.stacks.traefik.geoblock.enable
Enable the geoblock plugin for Traefik.
This will block access to the services based on the country code of the request.
The plugin uses the IP2Location database to determine the country code.
If enabled, the geoblock will be used in the public middleware,
allowing only requests from the allowed countries.
nps.stacks.traefik.network.name
Network name for Podman bridge network. Will be used by the Traefik Docker provider
nps.stacks.traefik.provider
How services are registered in Traefik. "docker" (default): routes are created via container labels (Docker provider). "file": routes and services are generated into the dynamic config file. This is required for on-demand setups (e.g. Sablier), where stopped containers are removed by Quadlet and their labels can therefore not be read anymore.
nps.stacks.traefik.staticConfig
Static configuration for Traefik.
By default, for the configured domain, a wildcard certificate will be requested from Let's Encrypt
and used for all services that are registered with Traefik.
By default Cloudflare with DNS challenge will be used to request the certificate.
This requires the 'CF_DNS_API_TOKEN' environment variable to be present, e.g. by providing it via the extraEnv option.
The DNS provider as well as any other settings can be overwritten. For an example see https://tarow.github.io/nix-podman-stacks/docs/examples.html#change-dns-provider
nps.stacks.traefik.useSocketProxy
Whether to access the Podman socket through the read-only proxy for the traefik stack. Will be enabled by default if the 'docker-socket-proxy' stack is enabled.
boolean
config.nps.stacks.docker-socket-proxy.enable
Container Extension
Traefik adds several container options to the existing services.podman.containers.<name> options:
port: The main port that Traefik will forward traffic to.expose: Whether the service should be publicly reachable. Whenfalse(default), theprivatemiddleware is applied, which only allows requests from private CIDR ranges. Whentrue, thepublicmiddleware is applied, which allows access from the internet (with rate limit, security headers and optional geoblock/Crowdsec).traefik: Controls how the service is registered in Traefik (name,subDomain,middleware). The service is only registered whentraefik.nameis set.
Example:
{config, ...}: {
nps.stacks.streaming.containers.jellyfin = {
expose = true;
traefik = {
subDomain = "movies";
};
};
}services.podman.containers.<name>.expose
Whether the service should be exposed (e.g. reachable from external IP addresses).
When set to false, the private middleware will be applied by Traefik. The private middleware will only allow requests from
private CIDR ranges.
When set to true, the public middleware will be applied. The public middleware will allow access from the internet. It will be configured
with a rate limit, security headers and a geoblock plugin (if enabled). If enabled, Crowdsec will also
be added to the public middleware chain.
services.podman.containers.<name>.port
Main port that Traefik will forward traffic to. If Traefik is disabled, it will instead be added to the "ports" section
null or string or signed integer
null
services.podman.containers.<name>.socketActivation.*.port
Port that the socket should listen on
string or 16 bit unsigned integer; between 0 and 65535 (both inclusive)
services.podman.containers.<name>.traefik.middleware
A mapping of middleware name to a boolean that indicated if the middleware should be applied to the service.
attribute set of (submodule)
{ }
services.podman.containers.<name>.traefik.middleware.<name>.enable
Whether the middleware should be applied to the service
services.podman.containers.<name>.traefik.middleware.<name>.order
Order of the middleware. Middlewares will be called in order by Traefik. Lower number means higher priority.
services.podman.containers.<name>.traefik.name
The name of the service as it will be registered in Traefik. Will be used as a default for the subdomain.
If not set (null), the service will not be registered in Traefik.
services.podman.containers.<name>.traefik.serviceAddressInternal
The internal main address of the service. Can be used for internal communication without going through Traefik, when inside the same Podman network.
If the container shares another container's network namespace (e.g. via network = ["container:gluetun"]),
the address of that container is used instead
string
"${containerName}:${containerCfg.port}"
services.podman.containers.<name>.traefik.serviceHost
The host name of the service as it will be registered in Traefik.
string
"${traefikCfg.subDomain}.${nps.stacks.traefik.domain}"
services.podman.containers.<name>.traefik.serviceUrl
The full URL of the service as it will be registered in Traefik. This will be the serviceHost including the "https://" prefix.
string
"https://${traefikCfg.serviceHost}"
services.podman.containers.<name>.traefik.subDomain
The subdomain of the service as it will be registered in Traefik.
Container Aliases
nps.stacks.traefik.containers.traefik
Alias of {option}services.podman.containers.traefik.
