1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9 inherit (lib) mkIf mkOption optional;
10 inherit (lib.types)
11 path
12 bool
13 listOf
14 str
15 port
16 ;
17 cfg = config.services.darkhttpd;
18
19 args = lib.concatStringsSep " " (
20 [
21 cfg.rootDir
22 "--port ${toString cfg.port}"
23 "--addr ${cfg.address}"
24 ]
25 ++ cfg.extraArgs
26 ++ optional cfg.hideServerId "--no-server-id"
27 ++ optional config.networking.enableIPv6 "--ipv6"
28 );
29
30in
31{
32 options.services.darkhttpd = {
33 enable = lib.mkEnableOption "DarkHTTPd web server";
34
35 port = mkOption {
36 default = 80;
37 type = port;
38 description = ''
39 Port to listen on.
40 Pass 0 to let the system choose any free port for you.
41 '';
42 };
43
44 address = mkOption {
45 default = "127.0.0.1";
46 type = str;
47 description = ''
48 Address to listen on.
49 Pass `all` to listen on all interfaces.
50 '';
51 };
52
53 rootDir = mkOption {
54 type = path;
55 description = ''
56 Path from which to serve files.
57 '';
58 };
59
60 hideServerId = mkOption {
61 type = bool;
62 default = true;
63 description = ''
64 Don't identify the server type in headers or directory listings.
65 '';
66 };
67
68 extraArgs = mkOption {
69 type = listOf str;
70 default = [ ];
71 description = ''
72 Additional configuration passed to the executable.
73 '';
74 };
75 };
76
77 config = mkIf cfg.enable {
78 systemd.services.darkhttpd = {
79 description = "Dark HTTPd";
80 wants = [ "network.target" ];
81 after = [ "network.target" ];
82 wantedBy = [ "multi-user.target" ];
83 serviceConfig = {
84 DynamicUser = true;
85 ExecStart = "${pkgs.darkhttpd}/bin/darkhttpd ${args}";
86 AmbientCapabilities = lib.mkIf (cfg.port < 1024) [ "CAP_NET_BIND_SERVICE" ];
87 Restart = "on-failure";
88 RestartSec = "2s";
89 };
90 };
91 };
92}