2023-03-11 09:07:24 +03:00
|
|
|
{ config, lib, pkgs, ... }:
|
2023-03-04 23:22:03 +03:00
|
|
|
|
|
|
|
let
|
2023-03-11 09:07:24 +03:00
|
|
|
magentaData = import ../data.secret.nix;
|
|
|
|
|
|
|
|
dataDir = "/var/lib/traefik";
|
|
|
|
|
2023-03-04 23:22:03 +03:00
|
|
|
traefikCfg = config.services.traefik;
|
|
|
|
|
2023-03-11 09:07:24 +03:00
|
|
|
user = "traefik";
|
|
|
|
group = "traefik";
|
|
|
|
|
|
|
|
dynamicConfigFile = pkgs.runCommand "config.toml"
|
|
|
|
{
|
|
|
|
buildInputs = [ pkgs.remarshal ];
|
|
|
|
preferLocalBuild = true;
|
|
|
|
}
|
|
|
|
''
|
|
|
|
remarshal -if json -of toml \
|
|
|
|
< ${pkgs.writeText "dynamic_config.json" (builtins.toJSON traefikCfg.dynamicConfigOptions)} \
|
|
|
|
> $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
staticConfigFile = pkgs.runCommand "config.toml"
|
|
|
|
{
|
2023-03-14 20:58:04 +03:00
|
|
|
buildInputs = [ pkgs.remarshal ];
|
2023-03-11 09:07:24 +03:00
|
|
|
preferLocalBuild = true;
|
|
|
|
}
|
|
|
|
''
|
2023-03-14 20:58:04 +03:00
|
|
|
remarshal -if json -of toml \
|
2023-03-11 09:07:24 +03:00
|
|
|
< ${
|
|
|
|
pkgs.writeText "static_config.json" (builtins.toJSON
|
|
|
|
(lib.recursiveUpdate traefikCfg.staticConfigOptions {
|
|
|
|
providers.file.filename = "${dynamicConfigFile}";
|
|
|
|
}))
|
|
|
|
} \
|
|
|
|
> $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
mirrorVolume = path: "${path}:${path}";
|
2023-03-04 23:22:03 +03:00
|
|
|
in
|
|
|
|
{
|
|
|
|
networking.firewall.allowedTCPPorts = [ 80 443 8080 ];
|
|
|
|
|
2023-03-11 09:07:24 +03:00
|
|
|
users.users.${user} = {
|
|
|
|
isSystemUser = true;
|
|
|
|
createHome = true;
|
|
|
|
home = dataDir;
|
|
|
|
inherit group;
|
|
|
|
};
|
|
|
|
users.groups.${group} = { };
|
|
|
|
users.groups.docker.members = [ user ];
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [ "d '${dataDir}' 0700 ${user} ${group} - -" ];
|
|
|
|
|
2023-03-04 23:22:03 +03:00
|
|
|
age.secrets.traefik-dashboard-basicauth-users = {
|
|
|
|
file = ../../../secrets/traefik-dashboard-basicauth-users.age;
|
2023-03-11 09:07:24 +03:00
|
|
|
owner = user;
|
|
|
|
inherit group;
|
2023-03-04 23:22:03 +03:00
|
|
|
};
|
|
|
|
|
2023-03-14 20:58:04 +03:00
|
|
|
virtualisation.docker.stacks.traefik = {
|
|
|
|
networks.traefik_public.external = true;
|
|
|
|
services.traefik = {
|
2023-03-11 09:07:24 +03:00
|
|
|
image = "traefik:v2.9";
|
2023-03-14 20:58:04 +03:00
|
|
|
command = [
|
2023-03-11 09:07:24 +03:00
|
|
|
"--configFile=${staticConfigFile}"
|
|
|
|
];
|
|
|
|
ports = [
|
|
|
|
"80:80"
|
|
|
|
"443:443"
|
|
|
|
"8080:8080"
|
|
|
|
];
|
2023-03-14 20:58:04 +03:00
|
|
|
extra_hosts = [ "host.docker.internal:host-gateway" ];
|
|
|
|
networks = [ "traefik_public" ];
|
2023-03-11 09:07:24 +03:00
|
|
|
volumes = [
|
|
|
|
"${mirrorVolume "/var/run/docker.sock"}:ro"
|
|
|
|
"${mirrorVolume dataDir}"
|
|
|
|
"${mirrorVolume staticConfigFile}:ro"
|
|
|
|
"${mirrorVolume dynamicConfigFile}:ro"
|
|
|
|
"${mirrorVolume config.age.secrets.traefik-dashboard-basicauth-users.path}:ro"
|
|
|
|
];
|
2023-03-14 23:11:28 +03:00
|
|
|
deploy = {
|
|
|
|
placement.constraints = [ "node.role==manager" ];
|
|
|
|
update_config.order = "start-first";
|
|
|
|
};
|
2023-03-11 09:07:24 +03:00
|
|
|
};
|
|
|
|
};
|
2023-03-09 14:15:44 +03:00
|
|
|
|
2023-03-04 23:22:03 +03:00
|
|
|
services.traefik = {
|
|
|
|
staticConfigOptions = {
|
|
|
|
entryPoints = {
|
2023-03-05 16:09:24 +03:00
|
|
|
http = {
|
|
|
|
address = ":80";
|
|
|
|
http.redirections.entryPoint = {
|
|
|
|
to = "https";
|
|
|
|
scheme = "https";
|
|
|
|
};
|
|
|
|
};
|
2023-03-04 23:22:03 +03:00
|
|
|
https.address = ":443";
|
|
|
|
dashboard.address = ":8080";
|
|
|
|
};
|
|
|
|
api = { };
|
|
|
|
log = { };
|
|
|
|
accessLog = { };
|
|
|
|
certificatesResolvers.le.acme = {
|
2023-03-11 09:07:24 +03:00
|
|
|
storage = "${dataDir}/acme.json";
|
2023-03-04 23:22:03 +03:00
|
|
|
email = "dmitriy@pleshevski.ru";
|
|
|
|
tlschallenge = true;
|
|
|
|
};
|
2023-03-09 14:15:44 +03:00
|
|
|
providers.docker = {
|
2023-03-11 09:07:24 +03:00
|
|
|
network = "traefik_public";
|
2023-03-09 14:15:44 +03:00
|
|
|
constraints = "Label(`traefik.constraint-label`, `${config.networking.hostName}_public`)";
|
|
|
|
exposedByDefault = false;
|
|
|
|
swarmMode = true;
|
|
|
|
};
|
2023-03-04 23:22:03 +03:00
|
|
|
};
|
2023-03-11 09:07:24 +03:00
|
|
|
|
2023-03-09 14:15:44 +03:00
|
|
|
dynamicConfigOptions.http = {
|
|
|
|
routers.to_traefik_dashboard = {
|
|
|
|
rule = "Host(`${magentaData.addr}`)";
|
|
|
|
entryPoints = [ "dashboard" ];
|
|
|
|
middlewares = [ "traefik_dashboard_auth" ];
|
|
|
|
service = "api@internal";
|
|
|
|
};
|
|
|
|
middlewares = {
|
|
|
|
traefik_dashboard_auth.basicAuth = {
|
|
|
|
usersFile = config.age.secrets.traefik-dashboard-basicauth-users.path;
|
2023-03-04 23:22:03 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|