about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/web-servers/traefik.nix
blob: 8de7df0d446c9d1c0b14ef052c427178a38360d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.traefik;
  configFile =
    if cfg.configFile == null then
      pkgs.runCommand "config.toml" {
        buildInputs = [ pkgs.remarshal ];
        preferLocalBuild = true;
      } ''
        remarshal -if json -of toml \
          < ${pkgs.writeText "config.json" (builtins.toJSON cfg.configOptions)} \
          > $out
      ''
    else cfg.configFile;

in {
  options.services.traefik = {
    enable = mkEnableOption "Traefik web server";

    configFile = mkOption {
      default = null;
      example = literalExample "/path/to/config.toml";
      type = types.nullOr types.path;
      description = ''
        Path to verbatim traefik.toml to use.
        (Using that option has precedence over <literal>configOptions</literal>)
      '';
    };

    configOptions = mkOption {
      description = ''
        Config for Traefik.
      '';
      type = types.attrs;
      default = {
        defaultEntryPoints = ["http"];
        entryPoints.http.address = ":80";
      };
      example = {
        defaultEntrypoints = [ "http" ];
        web.address = ":8080";
        entryPoints.http.address = ":80";

        file = {};
        frontends = {
          frontend1 = {
            backend = "backend1";
            routes.test_1.rule = "Host:localhost";
          };
        };
        backends.backend1 = {
          servers.server1.url = "http://localhost:8000";
        };
      };
    };

    dataDir = mkOption {
      default = "/var/lib/traefik";
      type = types.path;
      description = ''
      Location for any persistent data traefik creates, ie. acme
      '';
    };

    group = mkOption {
      default = "traefik";
      type = types.str;
      example = "docker";
      description = ''
        Set the group that traefik runs under.
        For the docker backend this needs to be set to <literal>docker</literal> instead.
      '';
    };

    package = mkOption {
      default = pkgs.traefik;
      defaultText = "pkgs.traefik";
      type = types.package;
      description = "Traefik package to use.";
    };
  };

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

    systemd.services.traefik = {
      description = "Traefik web server";
      after = [ "network-online.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        ExecStart = ''${cfg.package.bin}/bin/traefik --configfile=${configFile}'';
        Type = "simple";
        User = "traefik";
        Group = cfg.group;
        Restart = "on-failure";
        StartLimitInterval = 86400;
        StartLimitBurst = 5;
        AmbientCapabilities = "cap_net_bind_service";
        CapabilityBoundingSet = "cap_net_bind_service";
        NoNewPrivileges = true;
        LimitNPROC = 64;
        LimitNOFILE = 1048576;
        PrivateTmp = true;
        PrivateDevices = true;
        ProtectHome = true;
        ProtectSystem = "full";
        ReadWriteDirectories = cfg.dataDir;
      };
    };

    users.users.traefik = {
      group = "traefik";
      home = cfg.dataDir;
      createHome = true;
    };

    users.groups.traefik = {};
  };
}