nixos/traefik: Adapt to traefik v2

This commit:

1. Updates the path of the traefik package, so that the out output is
   used.
2. Adapts the configuration settings and options to Traefik v2.
3. Formats the NixOS traefik service using nixfmt.
This commit is contained in:
Ioannis Koutras 2019-12-31 01:41:18 +01:00
parent 07f1844c58
commit bc766b003a

View file

@ -4,56 +4,88 @@ with lib;
let let
cfg = config.services.traefik; cfg = config.services.traefik;
configFile = dynamicConfigFile = if cfg.dynamicConfigFile == null then
if cfg.configFile == null then pkgs.runCommand "config.toml" {
pkgs.runCommand "config.toml" { buildInputs = [ pkgs.remarshal ];
buildInputs = [ pkgs.remarshal ]; preferLocalBuild = true;
preferLocalBuild = true; } ''
} '' remarshal -if json -of toml \
remarshal -if json -of toml \ < ${
< ${pkgs.writeText "config.json" (builtins.toJSON cfg.configOptions)} \ pkgs.writeText "dynamic_config.json"
> $out (builtins.toJSON cfg.dynamicConfigOptions)
'' } \
else cfg.configFile; > $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;
in { in {
options.services.traefik = { options.services.traefik = {
enable = mkEnableOption "Traefik web server"; enable = mkEnableOption "Traefik web server";
configFile = mkOption { staticConfigFile = mkOption {
default = null; default = null;
example = literalExample "/path/to/config.toml"; example = literalExample "/path/to/static_config.toml";
type = types.nullOr types.path; type = types.nullOr types.path;
description = '' description = ''
Path to verbatim traefik.toml to use. Path to traefik's static configuration to use.
(Using that option has precedence over <literal>configOptions</literal>) (Using that option has precedence over <literal>staticConfigOptions</literal> and <literal>dynamicConfigOptions</literal>)
''; '';
}; };
configOptions = mkOption { staticConfigOptions = mkOption {
description = '' description = ''
Config for Traefik. Static configuration for Traefik.
''; '';
type = types.attrs; type = types.attrs;
default = { default = { entryPoints.http.address = ":80"; };
defaultEntryPoints = ["http"];
entryPoints.http.address = ":80";
};
example = { example = {
defaultEntrypoints = [ "http" ]; entryPoints.web.address = ":8080";
web.address = ":8080";
entryPoints.http.address = ":80"; entryPoints.http.address = ":80";
file = {}; api = { };
frontends = { };
frontend1 = { };
backend = "backend1";
routes.test_1.rule = "Host:localhost"; dynamicConfigFile = mkOption {
}; default = null;
}; example = literalExample "/path/to/dynamic_config.toml";
backends.backend1 = { type = types.nullOr types.path;
servers.server1.url = "http://localhost:8000"; description = ''
Path to traefik's dynamic configuration to use.
(Using that option has precedence over <literal>dynamicConfigOptions</literal>)
'';
};
dynamicConfigOptions = mkOption {
description = ''
Dynamic configuration for Traefik.
'';
type = types.attrs;
default = { };
example = {
http.routers.router1 = {
rule = "Host(`localhost`)";
service = "service1";
}; };
http.services.service1.loadBalancer.servers =
[{ url = "http://localhost:8080"; }];
}; };
}; };
@ -61,7 +93,7 @@ in {
default = "/var/lib/traefik"; default = "/var/lib/traefik";
type = types.path; type = types.path;
description = '' description = ''
Location for any persistent data traefik creates, ie. acme Location for any persistent data traefik creates, ie. acme
''; '';
}; };
@ -84,16 +116,15 @@ in {
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
systemd.tmpfiles.rules = [ systemd.tmpfiles.rules = [ "d '${cfg.dataDir}' 0700 traefik traefik - -" ];
"d '${cfg.dataDir}' 0700 traefik traefik - -"
];
systemd.services.traefik = { systemd.services.traefik = {
description = "Traefik web server"; description = "Traefik web server";
after = [ "network-online.target" ]; after = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
serviceConfig = { serviceConfig = {
ExecStart = ''${cfg.package.bin}/bin/traefik --configfile=${configFile}''; ExecStart =
"${cfg.package}/bin/traefik --configfile=${staticConfigFile}";
Type = "simple"; Type = "simple";
User = "traefik"; User = "traefik";
Group = cfg.group; Group = cfg.group;
@ -120,6 +151,6 @@ in {
isSystemUser = true; isSystemUser = true;
}; };
users.groups.traefik = {}; users.groups.traefik = { };
}; };
} }