nixos/caddy: Add rfc42 settings option

This commit is contained in:
sohalt 2023-08-22 20:14:35 +02:00
parent 4b7ad2c7ea
commit c01874615b
2 changed files with 62 additions and 14 deletions

View File

@ -24,21 +24,26 @@ let
}
'';
configFile =
let
Caddyfile = pkgs.writeTextDir "Caddyfile" ''
{
${cfg.globalConfig}
}
${cfg.extraConfig}
'';
settingsFormat = pkgs.formats.json { };
Caddyfile-formatted = pkgs.runCommand "Caddyfile-formatted" { nativeBuildInputs = [ cfg.package ]; } ''
mkdir -p $out
cp --no-preserve=mode ${Caddyfile}/Caddyfile $out/Caddyfile
caddy fmt --overwrite $out/Caddyfile
'';
in
configFile =
if cfg.settings != { } then
settingsFormat.generate "caddy.json" cfg.settings
else
let
Caddyfile = pkgs.writeTextDir "Caddyfile" ''
{
${cfg.globalConfig}
}
${cfg.extraConfig}
'';
Caddyfile-formatted = pkgs.runCommand "Caddyfile-formatted" { nativeBuildInputs = [ cfg.package ]; } ''
mkdir -p $out
cp --no-preserve=mode ${Caddyfile}/Caddyfile $out/Caddyfile
caddy fmt --overwrite $out/Caddyfile
'';
in
"${if pkgs.stdenv.buildPlatform == pkgs.stdenv.hostPlatform then Caddyfile-formatted else Caddyfile}/Caddyfile";
etcConfigFile = "caddy/caddy_config";
@ -299,6 +304,27 @@ in
which could delay the reload essentially indefinitely.
'';
};
settings = mkOption {
type = settingsFormat.type;
default = {};
description = lib.mdDoc ''
Structured configuration for Caddy to generate a Caddy JSON configuration file.
See <https://caddyserver.com/docs/json/> for available options.
::: {.warning}
Using a [Caddyfile](https://caddyserver.com/docs/caddyfile) instead of a JSON config is highly recommended by upstream.
There are only very few exception to this.
Please use a Caddyfile via {option}`services.caddy.configFile`, {option}`services.caddy.virtualHosts` or
{option}`services.caddy.extraConfig` with {option}`services.caddy.globalConfig` instead.
:::
::: {.note}
Takes presence over most `services.caddy.*` options, such as {option}`services.caddy.configFile` and {option}`services.caddy.virtualHosts`, if specified.
:::
'';
};
};
# implementation

View File

@ -50,6 +50,20 @@ import ./make-test-python.nix ({ pkgs, ... }: {
"http://localhost:8081" = { };
};
};
specialisation.rfc42.configuration = {
services.caddy.settings = {
apps.http.servers.default = {
listen = [ ":80" ];
routes = [{
handle = [{
body = "hello world";
handler = "static_response";
status_code = 200;
}];
}];
};
};
};
};
};
@ -58,6 +72,7 @@ import ./make-test-python.nix ({ pkgs, ... }: {
etagSystem = "${nodes.webserver.system.build.toplevel}/specialisation/etag";
justReloadSystem = "${nodes.webserver.system.build.toplevel}/specialisation/config-reload";
multipleConfigs = "${nodes.webserver.system.build.toplevel}/specialisation/multiple-configs";
rfc42Config = "${nodes.webserver.system.build.toplevel}/specialisation/rfc42";
in
''
url = "http://localhost/example.html"
@ -106,5 +121,12 @@ import ./make-test-python.nix ({ pkgs, ... }: {
)
webserver.wait_for_open_port(8080)
webserver.wait_for_open_port(8081)
with subtest("rfc42 settings config"):
webserver.succeed(
"${rfc42Config}/bin/switch-to-configuration test >&2"
)
webserver.wait_for_open_port(80)
webserver.succeed("curl http://localhost | grep hello")
'';
})