Skip to content

Commit

Permalink
nixos/caddy: optional formatting of Caddyfile
Browse files Browse the repository at this point in the history
As explained in
caddyserver/caddy#5930 (comment),
`caddy fmt` doesn’t handle heredocs. The existing module always runs
`caddy fmt` on native builds, and thus for a configuration containing a
heredoc, the service fails at runtime.

This change is simple, and backwards compatible. Alternatives I
considered include only using the output of `caddy fmt` if `caddy adapt`
also succeeds, removing the formatting and adding an option defaulting to false.
Adding an option that defaults to true allows users to disable `caddy fmt`
for other reasons, thoguh.
  • Loading branch information
dbaynard committed Nov 7, 2023
1 parent 59e6ccc commit d3bfa16
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions nixos/doc/manual/release-notes/rl-2311.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ The module update takes care of the new config syntax and the data itself (user

- `services.bitcoind` now properly respects the `enable` option.

- The Caddy module gained a new option named `services.caddy.formatCaddyfile` which is enabled by default, to match existing behaviour. When disabled, `caddy fmt` will no longer run on the generated caddyfile. This is useful where the caddyfile is not supported by `caddy fmt`, for example [for heredocs](https://github.com/caddyserver/caddy/issues/5930).

## Nixpkgs internals {#sec-release-23.11-nixpkgs-internals}

- The use of `sourceRoot = "source";`, `sourceRoot = "source/subdir";`, and similar lines in package derivations using the default `unpackPhase` is deprecated as it requires `unpackPhase` to always produce a directory named "source". Use `sourceRoot = src.name`, `sourceRoot = "${src.name}/subdir";`, or `setSourceRoot = "sourceRoot=$(echo */subdir)";` or similar instead.
Expand Down
17 changes: 16 additions & 1 deletion nixos/modules/services/web-servers/caddy/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ let
caddy fmt --overwrite $out/Caddyfile
'';
in
"${if pkgs.stdenv.buildPlatform == pkgs.stdenv.hostPlatform then Caddyfile-formatted else Caddyfile}/Caddyfile";
"${if cfg.formatCaddyfile && pkgs.stdenv.buildPlatform == pkgs.stdenv.hostPlatform
then Caddyfile-formatted else Caddyfile}/Caddyfile";

etcConfigFile = "caddy/caddy_config";

Expand Down Expand Up @@ -149,6 +150,20 @@ in
'';
};

formatCaddyfile = mkOption {
default = true;
type = types.bool;
description = lib.mdDoc ''
Format the generated caddyfile using `caddy fmt` if `caddy` is available
(i.e. if buildPlatform = hostPlatform) (defaults to true to match behaviour
in earlier versions of this module).
Caddy fmt does not handle all valid Caddyfiles. For example, see
<https://github.com/caddyserver/caddy/issues/5930> for the lack of heredoc
support.
'';
};

configFile = mkOption {
type = types.path;
default = configFile;
Expand Down

0 comments on commit d3bfa16

Please sign in to comment.