Merge pull request #244626 from Sohalt/caddy-rfc42

nixos/caddy: Add rfc42 settings option
This commit is contained in:
Emily 2023-08-23 20:36:46 +02:00 committed by GitHub
commit 73a060e7ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -34,6 +34,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;
}];
}];
};
};
};
};
};
@ -41,6 +55,7 @@ import ./make-test-python.nix ({ pkgs, ... }: {
let
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"
@ -62,5 +77,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")
'';
})