traefik: routes from a watched file, no restart on a route change
All checks were successful
nixos / nixos (push) Successful in 21s

The NixOS module writes the routes (dynamicConfigOptions) into the
static configuration, so every route change restarted Traefik: about 15
seconds with every site down (08:24 and 09:14 today; the second broke
czsk's image push). The routes now go to /etc/traefik/dynamic.toml, a
copy activation replaces, and Traefik's file provider watches it; the
static configuration stays the same. This deploy restarts Traefik once.

Assisted-by: Claude:opus-5-5
This commit is contained in:
Sergei Poljanski 2026-11-17 13:16:57 +04:00
commit cb9db0ed73
Signed by: asxpi
GPG key ID: 4F8851660FA4121B

View file

@ -54,10 +54,23 @@ in
} }
''; '';
# The routes go to a file Traefik watches, not into its static
# configuration: the NixOS module would otherwise write them into the
# static file, and every route change would restart Traefik (about 15
# seconds with every site down). mode makes /etc hold a copy, which
# activation replaces, so the watch sees the change.
environment.etc."traefik/dynamic.toml" = {
source =
(pkgs.formats.toml { }).generate "dynamic.toml"
config.services.traefik.dynamicConfigOptions;
mode = "0444";
};
services.traefik = { services.traefik = {
enable = true; enable = true;
package = pkgs.traefik; package = pkgs.traefik;
environmentFiles = [ config.sops.templates."traefik.env".path ]; environmentFiles = [ config.sops.templates."traefik.env".path ];
dynamicConfigFile = "/etc/traefik/dynamic.toml";
staticConfigOptions = { staticConfigOptions = {
api = { api = {
@ -81,6 +94,7 @@ in
addRoutersLabels = true; addRoutersLabels = true;
addServicesLabels = true; addServicesLabels = true;
}; };
providers.file.watch = true; # filename: dynamicConfigFile
providers.docker = { providers.docker = {
endpoint = "unix:///run/podman/podman.sock"; endpoint = "unix:///run/podman/podman.sock";
exposedByDefault = false; exposedByDefault = false;