tangled
alpha
login
or
join now
pyrox.dev
/
nixpkgs
0
fork
atom
lol
0
fork
atom
overview
issues
pulls
pipelines
nixos/caddy: Add rfc42 settings option
sohalt
2 years ago
c0187461
4b7ad2c7
+61
-13
2 changed files
expand all
collapse all
unified
split
nixos
modules
services
web-servers
caddy
default.nix
tests
caddy.nix
+39
-13
nixos/modules/services/web-servers/caddy/default.nix
···
24
24
}
25
25
'';
26
26
27
27
+
settingsFormat = pkgs.formats.json { };
28
28
+
27
29
configFile =
28
28
-
let
29
29
-
Caddyfile = pkgs.writeTextDir "Caddyfile" ''
30
30
-
{
31
31
-
${cfg.globalConfig}
32
32
-
}
33
33
-
${cfg.extraConfig}
34
34
-
'';
30
30
+
if cfg.settings != { } then
31
31
+
settingsFormat.generate "caddy.json" cfg.settings
32
32
+
else
33
33
+
let
34
34
+
Caddyfile = pkgs.writeTextDir "Caddyfile" ''
35
35
+
{
36
36
+
${cfg.globalConfig}
37
37
+
}
38
38
+
${cfg.extraConfig}
39
39
+
'';
35
40
36
36
-
Caddyfile-formatted = pkgs.runCommand "Caddyfile-formatted" { nativeBuildInputs = [ cfg.package ]; } ''
37
37
-
mkdir -p $out
38
38
-
cp --no-preserve=mode ${Caddyfile}/Caddyfile $out/Caddyfile
39
39
-
caddy fmt --overwrite $out/Caddyfile
40
40
-
'';
41
41
-
in
41
41
+
Caddyfile-formatted = pkgs.runCommand "Caddyfile-formatted" { nativeBuildInputs = [ cfg.package ]; } ''
42
42
+
mkdir -p $out
43
43
+
cp --no-preserve=mode ${Caddyfile}/Caddyfile $out/Caddyfile
44
44
+
caddy fmt --overwrite $out/Caddyfile
45
45
+
'';
46
46
+
in
42
47
"${if pkgs.stdenv.buildPlatform == pkgs.stdenv.hostPlatform then Caddyfile-formatted else Caddyfile}/Caddyfile";
43
48
44
49
etcConfigFile = "caddy/caddy_config";
···
297
302
to a non-infinite value in {option}`services.caddy.globalConfig`
298
303
to prevent Caddy waiting for active connections to finish,
299
304
which could delay the reload essentially indefinitely.
305
305
+
'';
306
306
+
};
307
307
+
308
308
+
settings = mkOption {
309
309
+
type = settingsFormat.type;
310
310
+
default = {};
311
311
+
description = lib.mdDoc ''
312
312
+
Structured configuration for Caddy to generate a Caddy JSON configuration file.
313
313
+
See <https://caddyserver.com/docs/json/> for available options.
314
314
+
315
315
+
::: {.warning}
316
316
+
Using a [Caddyfile](https://caddyserver.com/docs/caddyfile) instead of a JSON config is highly recommended by upstream.
317
317
+
There are only very few exception to this.
318
318
+
319
319
+
Please use a Caddyfile via {option}`services.caddy.configFile`, {option}`services.caddy.virtualHosts` or
320
320
+
{option}`services.caddy.extraConfig` with {option}`services.caddy.globalConfig` instead.
321
321
+
:::
322
322
+
323
323
+
::: {.note}
324
324
+
Takes presence over most `services.caddy.*` options, such as {option}`services.caddy.configFile` and {option}`services.caddy.virtualHosts`, if specified.
325
325
+
:::
300
326
'';
301
327
};
302
328
};
+22
nixos/tests/caddy.nix
···
50
50
"http://localhost:8081" = { };
51
51
};
52
52
};
53
53
+
specialisation.rfc42.configuration = {
54
54
+
services.caddy.settings = {
55
55
+
apps.http.servers.default = {
56
56
+
listen = [ ":80" ];
57
57
+
routes = [{
58
58
+
handle = [{
59
59
+
body = "hello world";
60
60
+
handler = "static_response";
61
61
+
status_code = 200;
62
62
+
}];
63
63
+
}];
64
64
+
};
65
65
+
};
66
66
+
};
53
67
};
54
68
};
55
69
···
58
72
etagSystem = "${nodes.webserver.system.build.toplevel}/specialisation/etag";
59
73
justReloadSystem = "${nodes.webserver.system.build.toplevel}/specialisation/config-reload";
60
74
multipleConfigs = "${nodes.webserver.system.build.toplevel}/specialisation/multiple-configs";
75
75
+
rfc42Config = "${nodes.webserver.system.build.toplevel}/specialisation/rfc42";
61
76
in
62
77
''
63
78
url = "http://localhost/example.html"
···
106
121
)
107
122
webserver.wait_for_open_port(8080)
108
123
webserver.wait_for_open_port(8081)
124
124
+
125
125
+
with subtest("rfc42 settings config"):
126
126
+
webserver.succeed(
127
127
+
"${rfc42Config}/bin/switch-to-configuration test >&2"
128
128
+
)
129
129
+
webserver.wait_for_open_port(80)
130
130
+
webserver.succeed("curl http://localhost | grep hello")
109
131
'';
110
132
})