1 {config, pkgs, lib, ...}:
3 cfg = config.services.nghttpx;
5 # renderHost :: Either ServerOptions Path -> String
7 if builtins.isString server
8 then "unix://${server}"
9 else "${server.host},${builtins.toString server.port}";
11 # Filter out submodule parameters whose value is null or false or is
14 # filterParams :: ParamsSubmodule -> ParamsSubmodule
17 (n: v: ("_module" != n) && (null != v) && (false != v))
18 (lib.optionalAttrs (null != p) p);
20 # renderBackend :: BackendSubmodule -> String
21 renderBackend = backend:
23 host = renderHost backend.server;
24 patterns = lib.concatStringsSep ":" backend.patterns;
26 # Render a set of backend parameters, this is somewhat
27 # complicated because nghttpx backend patterns can be entirely
28 # omitted and the params may be given as a mixed collection of
29 # 'key=val' pairs or atoms (e.g: 'proto=h2;tls')
35 else if builtins.isString v
37 else "${n}=${builtins.toString v}")
38 (filterParams backend.params);
40 # NB: params are delimited by a ";" which is the same delimiter
41 # to separate the host;[pattern];[params] sections of a backend
43 builtins.filter (e: "" != e) ([
47 formattedSections = lib.concatStringsSep ";" sections;
49 "backend=${formattedSections}";
51 # renderFrontend :: FrontendSubmodule -> String
52 renderFrontend = frontend:
54 host = renderHost frontend.server;
57 (n: v: if builtins.isBool v then n else v)
58 (filterParams frontend.params);
60 # NB: nghttpx doesn't accept "tls", you must omit "no-tls" for
61 # the default behavior of turning on TLS.
62 params1 = lib.remove "tls" params0;
64 sections = [ host] ++ params1;
65 formattedSections = lib.concatStringsSep ";" sections;
67 "frontend=${formattedSections}";
69 configurationFile = pkgs.writeText "nghttpx.conf" ''
70 ${lib.optionalString (null != cfg.tls) ("private-key-file="+cfg.tls.key)}
71 ${lib.optionalString (null != cfg.tls) ("certificate-file="+cfg.tls.crt)}
75 ${lib.concatMapStringsSep "\n" renderFrontend cfg.frontends}
76 ${lib.concatMapStringsSep "\n" renderBackend cfg.backends}
78 backlog=${builtins.toString cfg.backlog}
79 backend-address-family=${cfg.backend-address-family}
81 workers=${builtins.toString cfg.workers}
82 rlimit-nofile=${builtins.toString cfg.rlimit-nofile}
84 ${lib.optionalString cfg.single-thread "single-thread=yes"}
85 ${lib.optionalString cfg.single-process "single-process=yes"}
94 config = lib.mkIf cfg.enable {
96 users.groups.nghttpx = { };
97 users.users.nghttpx = {
98 group = config.users.groups.nghttpx.name;
105 wantedBy = [ "multi-user.target" ];
106 after = [ "network.target" ];
108 ${pkgs.nghttp2}/bin/nghttpx --conf=${configurationFile}
112 Restart = "on-failure";