Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: nixos/caddy: validate config on build by default #380894

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

6543
Copy link
Member

@6543 6543 commented Feb 10, 2025

Adds enforceConfigValidation option (default: true) to validate Caddy
configurations during system build. This catches configuration errors early
in the deployment pipeline, improving reliability and security of Caddy
deployments.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

image
image


Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Feb 10, 2025
@6543 6543 force-pushed the caddy_validate-config-on-build branch from 7e2bddf to e9425cc Compare February 10, 2025 13:19
@6543 6543 force-pushed the caddy_validate-config-on-build branch from e9425cc to 07dfc55 Compare February 10, 2025 13:21
@github-actions github-actions bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Feb 10, 2025
Comment on lines +74 to +94
validateCaddyConfig =
adapter: configFile:
builtins.fromJSON (
builtins.readFile (
pkgs.runCommand "caddy-validate"
{
nativeBuildInputs = [ cfg.package ];
}
''
if caddy validate --adapter ${toString adapter} --config ${configFile}; then
echo "true" > $out
else
echo "false" > $out
fi
''
)
);

configValidationResult = validateCaddyConfig (
if cfg.adapter != null then cfg.adapter else "caddyfile"
) cfg.configFile;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import From Derivation (IFD) is actively discouraged. I'd encourage you to implement this similar to services.nginx (through the use of pkgs.writes.writeNginxConfig) which writes the configuration file as part of the derivation, but fails to build if it is incorrent.

@@ -378,6 +400,16 @@ in
[here](https://caddyserver.com/docs/caddyfile/concepts#environment-variables)
'';
};

enforceConfigValidation = mkOption {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: for consistency with Nginx, I'd name this option validateConfigFile.

@6543 6543 changed the title nixos/caddy: validate config on build by default WIP: nixos/caddy: validate config on build by default Feb 10, 2025
webserver.wait_for_unit("caddy")

with subtest("invalid config fails build when validation is enforced"):
webserver.fail(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: the build is expect to fail ... so i need to check against that differently ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants