Skip to content

Commit 2d0a3ad

Browse files
committed
added static-darkhttpd webservice with darkhttpd backend
1 parent 433ab9c commit 2d0a3ad

File tree

6 files changed

+107
-4
lines changed

6 files changed

+107
-4
lines changed

lib/make-webservice.nix

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ wsName: module:
1111
../modules/web/webserver/apache.nix
1212
../modules/web/webserver/lighttpd.nix
1313
../modules/web/webserver/nginx.nix
14+
../modules/web/webserver/darkhttpd.nix
1415
];
1516
_module.args = { inherit wsName pkgs toplevel; };
1617
});

modules/web/core/base.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ in {
5454
};
5555

5656
webserver.variant = lib.mkOption {
57-
type = lib.types.nullOr (lib.types.enum [ "apache" "lighttpd" "nginx" ]);
57+
type = lib.types.nullOr (lib.types.enum [ "apache" "lighttpd" "nginx" "darkhttpd" ]);
5858
default = null;
5959
description = "The webserver module to use for this webservice.";
6060
};

modules/web/default.nix

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
{
44
imports = lib.mapAttrsToList (import ../../lib/make-webservice.nix) {
5-
filesender = services/filesender;
6-
leaps = services/leaps;
7-
mediawiki = services/mediawiki;
5+
filesender = services/filesender;
6+
leaps = services/leaps;
7+
mediawiki = services/mediawiki;
8+
static-darkhttpd = services/static-darkhttpd;
89
};
910

1011
config = let
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{ config, lib, pkgs, wsName, mkUnique, darkhttpd, ... }:
2+
3+
with lib;
4+
5+
{
6+
options = {};
7+
8+
config = rec {
9+
webserver.variant = "darkhttpd";
10+
tests.wanted = [ ./test.nix ];
11+
};
12+
13+
meta = {
14+
description = "Using darkhttpd for static file serving (no CGI)";
15+
maintainers = with maintainers; [ qknight ];
16+
license = lib.licenses.bsd2;
17+
homepage = https://github.com/nixcloud/nixcloud-webservices;
18+
};
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
name = "static-darkhttpd";
3+
4+
machine.imports = [ ../../../../tests/common/eatmydata.nix ];
5+
machine.nixcloud.reverse-proxy.enable = true;
6+
machine.nixcloud.reverse-proxy.extendEtcHosts = true;
7+
machine.nixcloud.webservices.static-darkhttpd = {
8+
foo.enable = true;
9+
foo.proxyOptions.TLS = "none";
10+
foo.proxyOptions.domain = "example.com";
11+
foo.proxyOptions.http.mode = "on";
12+
foo.proxyOptions.https.mode = "off";
13+
foo.proxyOptions.port = 8080;
14+
15+
bar.enable = true;
16+
bar.proxyOptions.TLS = "none";
17+
bar.proxyOptions.domain = "example.org";
18+
bar.proxyOptions.http.mode = "on";
19+
bar.proxyOptions.https.mode = "off";
20+
bar.proxyOptions.port = 8081;
21+
};
22+
23+
testScript = let
24+
searchFor = "Generated by darkhttpd";
25+
in ''
26+
$machine->waitForUnit('multi-user.target');
27+
$machine->succeed('curl -L http://example.com/ | grep -qF "${searchFor}"');
28+
$machine->succeed('curl -L http://example.org/ | grep -qF "${searchFor}"');
29+
'';
30+
}

modules/web/webserver/darkhttpd.nix

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{ config, pkgs, lib, options, wsName, mkUnique, ... }:
2+
3+
with lib;
4+
5+
{
6+
options.webserver.darkhttpd = {
7+
extraServiceDependencies = mkOption {
8+
type = types.listOf types.str;
9+
default = [ ];
10+
example = [ "postgresql.service" ];
11+
description = "Makes it easy to replace postgresql by mysql and depend on the service before we start the webservice.";
12+
};
13+
};
14+
15+
config = mkIf (config.webserver.variant == "darkhttpd" && config.enable) {
16+
directories.log = {
17+
permissions.defaultDirectoryMode = "0750";
18+
permissions.others.noAccess = true;
19+
owner = mkUnique config.webserver.user;
20+
group = mkUnique config.webserver.group;
21+
instance.before = [ "webserver-init.service" "instance-init.target" ];
22+
};
23+
24+
systemd.services.darkhttpd = {
25+
description = "${config.uniqueName} main service (darkhttpd)";
26+
wantedBy = [ "multi-user.target" ];
27+
wants = [ "keys.target" ];
28+
after = [ "network.target" "fs.target" "keys.target" ];
29+
instance.after = [ "database.target" "webserver-init.service" ];
30+
31+
serviceConfig = {
32+
ExecStart = "${pkgs.darkhttpd}/bin/darkhttpd ${config.stateDir} --port ${toString config.proxyOptions.port} --addr 127.0.0.1";
33+
KillSignal = "SIGTERM";
34+
Restart = "always";
35+
RestartSec = "10s";
36+
StartLimitInterval = "1min";
37+
User = config.webserver.user;
38+
Group = config.webserver.group;
39+
PermissionsStartOnly = true;
40+
PrivateTmp = config.webserver.privateTmp;
41+
WorkingDirectory = config.stateDir;
42+
MemoryDenyWriteExecute = true;
43+
RestrictNameSpaces = true;
44+
NoNewPrivileges = true;
45+
ProtectHome = true;
46+
PrivateUsers = true;
47+
ProtectSystem = true;
48+
ProtectKernelTunables = true;
49+
};
50+
};
51+
};
52+
}

0 commit comments

Comments
 (0)