Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/traefik: use formats.toml to generate config #291432

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 17 additions & 50 deletions nixos/modules/services/web-servers/traefik.nix
@@ -1,57 +1,15 @@
{ config, lib, pkgs, ... }:

with lib;

let
inherit (lib) mkDefault mkEnableOption mkIf mkOption mkPackageOption literalExpression types;

cfg = config.services.traefik;
jsonValue = with types;
let
valueType = nullOr (oneOf [
bool
int
float
str
(lazyAttrsOf valueType)
(listOf valueType)
]) // {
description = "JSON value";
emptyValue.value = { };
};
in valueType;
dynamicConfigFile = if cfg.dynamicConfigFile == null then
pkgs.runCommand "config.toml" {
buildInputs = [ pkgs.remarshal ];
preferLocalBuild = true;
} ''
remarshal -if json -of toml \
< ${
pkgs.writeText "dynamic_config.json"
(builtins.toJSON cfg.dynamicConfigOptions)
} \
> $out
''
else
cfg.dynamicConfigFile;
staticConfigFile = if cfg.staticConfigFile == null then
pkgs.runCommand "config.toml" {
buildInputs = [ pkgs.yj ];
preferLocalBuild = true;
} ''
yj -jt -i \
< ${
pkgs.writeText "static_config.json" (builtins.toJSON
(recursiveUpdate cfg.staticConfigOptions {
providers.file.filename = "${dynamicConfigFile}";
}))
} \
> $out
''
else
cfg.staticConfigFile;

configFormat = pkgs.formats.toml {};

finalStaticConfigFile =
if cfg.environmentFiles == []
then staticConfigFile
then cfg.staticConfigFile
else "/run/traefik/config.toml";
in {
options.services.traefik = {
Expand All @@ -71,7 +29,7 @@ in {
description = lib.mdDoc ''
Static configuration for Traefik.
'';
type = jsonValue;
type = configFormat.type;
default = { entryPoints.http.address = ":80"; };
example = {
entryPoints.web.address = ":8080";
Expand All @@ -95,7 +53,7 @@ in {
description = lib.mdDoc ''
Dynamic configuration for Traefik.
'';
type = jsonValue;
type = configFormat.type;
default = { };
example = {
http.routers.router1 = {
Expand Down Expand Up @@ -140,6 +98,13 @@ in {
};

config = mkIf cfg.enable {
services.traefik = {
staticConfigOptions.providers.file.filename = cfg.dynamicConfigFile;

staticConfigFile = mkDefault (configFormat.generate "traefik-static.toml" cfg.staticConfigOptions);
dynamicConfigFile = mkDefault (configFormat.generate "traefik-dynamic.toml" cfg.dynamicConfigOptions);
};

systemd.tmpfiles.rules = [ "d '${cfg.dataDir}' 0700 traefik traefik - -" ];

systemd.services.traefik = {
Expand All @@ -154,7 +119,7 @@ in {
ExecStartPre = lib.optional (cfg.environmentFiles != [])
(pkgs.writeShellScript "pre-start" ''
umask 077
${pkgs.envsubst}/bin/envsubst -i "${staticConfigFile}" > "${finalStaticConfigFile}"
${pkgs.envsubst}/bin/envsubst -i "${cfg.staticConfigFile}" > "${finalStaticConfigFile}"
'');
ExecStart = "${cfg.package}/bin/traefik --configfile=${finalStaticConfigFile}";
Type = "simple";
Expand Down Expand Up @@ -184,4 +149,6 @@ in {

users.groups.traefik = { };
};

meta.maintainers = with lib.maintainers; [ Scrumplex ];
}