1 { config, lib, pkgs, ... }:
6 cfg = config.services.trafficserver;
7 user = config.users.users.trafficserver.name;
8 group = config.users.groups.trafficserver.name;
10 getManualUrl = name: "https://docs.trafficserver.apache.org/en/latest/admin-guide/files/${name}.en.html";
12 yaml = pkgs.formats.yaml { };
14 mkYamlConf = name: cfg:
16 "trafficserver/${name}.yaml".source = yaml.generate "${name}.yaml" cfg;
18 "trafficserver/${name}.yaml".text = "";
21 mkRecordLines = path: value:
23 lib.mapAttrsToList (n: v: mkRecordLines (path ++ [ n ]) v) value
24 else if isInt value then
25 "CONFIG ${concatStringsSep "." path} INT ${toString value}"
26 else if isFloat value then
27 "CONFIG ${concatStringsSep "." path} FLOAT ${toString value}"
29 "CONFIG ${concatStringsSep "." path} STRING ${toString value}";
31 mkRecordsConfig = cfg: concatStringsSep "\n" (flatten (mkRecordLines [ ] cfg));
32 mkPluginConfig = cfg: concatStringsSep "\n" (map (p: "${p.path} ${p.arg}") cfg);
35 options.services.trafficserver = {
36 enable = mkEnableOption "Apache Traffic Server";
41 example = "dest_domain=example.com suffix=js action=never-cache";
43 Caching rules that overrule the origin's caching policy.
46 documentation](${getManualUrl "cache.config"}) for more details.
53 example = "domain=example.com volume=1";
55 Partition the cache according to origin server or domain
58 upstream documentation](${getManualUrl "hosting.config"}) for more details.
63 type = types.nullOr yaml.type;
64 default = lib.importJSON ./ip_allow.json;
65 defaultText = literalMD "upstream defaults";
66 example = literalExpression ''
70 ip_addrs = "127.0.0.1";
77 Control client access to Traffic Server and Traffic Server connections
81 documentation](${getManualUrl "ip_allow.yaml"}) for more details.
86 type = types.nullOr yaml.type;
87 default = lib.importJSON ./logging.json;
88 defaultText = literalMD "upstream defaults";
94 documentation](${getManualUrl "logging.yaml"}) for more details.
102 dest_domain=. method=get parent="p1.example:8080; p2.example:8080" round_robin=true
105 Identify the parent proxies used in an cache hierarchy.
107 Consult the [upstream
108 documentation](${getManualUrl "parent.config"}) for more details.
116 Controls run-time loadable plugins available to Traffic Server, as
117 well as their configuration.
119 Consult the [upstream
120 documentation](${getManualUrl "plugin.config"}) for more details.
125 options.path = mkOption {
127 example = "xdebug.so";
129 Path to plugin. The path can either be absolute, or relative to
130 the plugin directory.
133 options.arg = mkOption {
136 example = "--header=ATS-My-Debug";
137 description = "arguments to pass to the plugin";
144 let valueType = (attrsOf (oneOf [ int float str valueType ])) // {
145 description = "Traffic Server records value";
150 example = { proxy.config.proxy_name = "my_server"; };
152 List of configurable variables used by Traffic Server.
155 upstream documentation](${getManualUrl "records.config"}) for more details.
162 example = "map http://from.example http://origin.example";
164 URL remapping rules used by Traffic Server.
167 upstream documentation](${getManualUrl "remap.config"}) for more details.
171 splitDns = mkOption {
175 dest_domain=internal.corp.example named="255.255.255.255:212 255.255.255.254" def_domain=corp.example search_list="corp.example corp1.example"
176 dest_domain=!internal.corp.example named=255.255.255.253
179 Specify the DNS server that Traffic Server should use under specific
183 upstream documentation](${getManualUrl "splitdns.config"}) for more details.
187 sslMulticert = mkOption {
190 example = "dest_ip=* ssl_cert_name=default.pem";
192 Configure SSL server certificates to terminate the SSL sessions.
195 upstream documentation](${getManualUrl "ssl_multicert.config"}) for more details.
200 type = types.nullOr yaml.type;
202 example = literalExpression ''
205 fqdn = "no-http2.example.com";
211 Configure aspects of TLS connection handling for both inbound and
212 outbound connections.
214 Consult the [upstream
215 documentation](${getManualUrl "sni.yaml"}) for more details.
221 default = "/var/cache/trafficserver 256M";
222 example = "/dev/disk/by-id/XXXXX volume=1";
224 List all the storage that make up the Traffic Server cache.
227 upstream documentation](${getManualUrl "storage.config"}) for more details.
231 strategies = mkOption {
232 type = types.nullOr yaml.type;
235 Specify the next hop proxies used in an cache hierarchy and the
236 algorithms used to select the next proxy.
239 upstream documentation](${getManualUrl "strategies.yaml"}) for more details.
244 type = types.nullOr yaml.type;
246 example = "volume=1 scheme=http size=20%";
248 Manage cache space more efficiently and restrict disk usage by
249 creating cache volumes of different sizes.
252 upstream documentation](${getManualUrl "volume.config"}) for more details.
257 config = mkIf cfg.enable {
259 "trafficserver/cache.config".text = cfg.cache;
260 "trafficserver/hosting.config".text = cfg.hosting;
261 "trafficserver/parent.config".text = cfg.parent;
262 "trafficserver/plugin.config".text = mkPluginConfig cfg.plugins;
263 "trafficserver/records.config".text = mkRecordsConfig cfg.records;
264 "trafficserver/remap.config".text = cfg.remap;
265 "trafficserver/splitdns.config".text = cfg.splitDns;
266 "trafficserver/ssl_multicert.config".text = cfg.sslMulticert;
267 "trafficserver/storage.config".text = cfg.storage;
268 "trafficserver/volume.config".text = cfg.volume;
269 } // (mkYamlConf "ip_allow" cfg.ipAllow)
270 // (mkYamlConf "logging" cfg.logging)
271 // (mkYamlConf "sni" cfg.sni)
272 // (mkYamlConf "strategies" cfg.strategies);
274 environment.systemPackages = [ pkgs.trafficserver ];
275 systemd.packages = [ pkgs.trafficserver ];
277 # Traffic Server does privilege handling independently of systemd, and
278 # therefore should be started as root
279 systemd.services.trafficserver = {
281 wantedBy = [ "multi-user.target" ];
284 # These directories can't be created by systemd because:
286 # 1. Traffic Servers starts as root and switches to an unprivileged user
287 # afterwards. The runtime directories defined below are assumed to be
288 # owned by that user.
289 # 2. The bin/trafficserver script assumes these directories exist.
290 systemd.tmpfiles.rules = [
291 "d '/run/trafficserver' - ${user} ${group} - -"
292 "d '/var/cache/trafficserver' - ${user} ${group} - -"
293 "d '/var/lib/trafficserver' - ${user} ${group} - -"
294 "d '/var/log/trafficserver' - ${user} ${group} - -"
297 services.trafficserver = {
298 records.proxy.config.admin.user_id = user;
299 records.proxy.config.body_factory.template_sets_dir =
300 "${pkgs.trafficserver}/etc/trafficserver/body_factory";
303 users.users.trafficserver = {
304 description = "Apache Traffic Server";
308 users.groups.trafficserver = { };