lol

nixos/caddy: Add rfc42 settings option

sohalt c0187461 4b7ad2c7

+61 -13
+39 -13
nixos/modules/services/web-servers/caddy/default.nix
··· 24 24 } 25 25 ''; 26 26 27 + settingsFormat = pkgs.formats.json { }; 28 + 27 29 configFile = 28 - let 29 - Caddyfile = pkgs.writeTextDir "Caddyfile" '' 30 - { 31 - ${cfg.globalConfig} 32 - } 33 - ${cfg.extraConfig} 34 - ''; 30 + if cfg.settings != { } then 31 + settingsFormat.generate "caddy.json" cfg.settings 32 + else 33 + let 34 + Caddyfile = pkgs.writeTextDir "Caddyfile" '' 35 + { 36 + ${cfg.globalConfig} 37 + } 38 + ${cfg.extraConfig} 39 + ''; 35 40 36 - Caddyfile-formatted = pkgs.runCommand "Caddyfile-formatted" { nativeBuildInputs = [ cfg.package ]; } '' 37 - mkdir -p $out 38 - cp --no-preserve=mode ${Caddyfile}/Caddyfile $out/Caddyfile 39 - caddy fmt --overwrite $out/Caddyfile 40 - ''; 41 - in 41 + Caddyfile-formatted = pkgs.runCommand "Caddyfile-formatted" { nativeBuildInputs = [ cfg.package ]; } '' 42 + mkdir -p $out 43 + cp --no-preserve=mode ${Caddyfile}/Caddyfile $out/Caddyfile 44 + caddy fmt --overwrite $out/Caddyfile 45 + ''; 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 + ''; 306 + }; 307 + 308 + settings = mkOption { 309 + type = settingsFormat.type; 310 + default = {}; 311 + description = lib.mdDoc '' 312 + Structured configuration for Caddy to generate a Caddy JSON configuration file. 313 + See <https://caddyserver.com/docs/json/> for available options. 314 + 315 + ::: {.warning} 316 + Using a [Caddyfile](https://caddyserver.com/docs/caddyfile) instead of a JSON config is highly recommended by upstream. 317 + There are only very few exception to this. 318 + 319 + Please use a Caddyfile via {option}`services.caddy.configFile`, {option}`services.caddy.virtualHosts` or 320 + {option}`services.caddy.extraConfig` with {option}`services.caddy.globalConfig` instead. 321 + ::: 322 + 323 + ::: {.note} 324 + Takes presence over most `services.caddy.*` options, such as {option}`services.caddy.configFile` and {option}`services.caddy.virtualHosts`, if specified. 325 + ::: 300 326 ''; 301 327 }; 302 328 };
+22
nixos/tests/caddy.nix
··· 50 50 "http://localhost:8081" = { }; 51 51 }; 52 52 }; 53 + specialisation.rfc42.configuration = { 54 + services.caddy.settings = { 55 + apps.http.servers.default = { 56 + listen = [ ":80" ]; 57 + routes = [{ 58 + handle = [{ 59 + body = "hello world"; 60 + handler = "static_response"; 61 + status_code = 200; 62 + }]; 63 + }]; 64 + }; 65 + }; 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 + 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 + 125 + with subtest("rfc42 settings config"): 126 + webserver.succeed( 127 + "${rfc42Config}/bin/switch-to-configuration test >&2" 128 + ) 129 + webserver.wait_for_open_port(80) 130 + webserver.succeed("curl http://localhost | grep hello") 109 131 ''; 110 132 })