Skip to content

Comments

nixos/caddy: validate at build-time#377075

Merged
Stunkymonkey merged 2 commits intoNixOS:masterfrom
Stunkymonkey:caddy-validate
Mar 25, 2025
Merged

nixos/caddy: validate at build-time#377075
Stunkymonkey merged 2 commits intoNixOS:masterfrom
Stunkymonkey:caddy-validate

Conversation

@Stunkymonkey
Copy link
Contributor

@Stunkymonkey Stunkymonkey commented Jan 26, 2025

its nice to validate the config before reloading/restarting. same as nginx

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.

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 Jan 26, 2025
@Stunkymonkey Stunkymonkey requested a review from sephii January 26, 2025 19:22
@Stunkymonkey Stunkymonkey marked this pull request as ready for review January 26, 2025 19:22
@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 This PR causes between 1 and 10 packages to rebuild on Linux. labels Jan 26, 2025
@sephii
Copy link
Contributor

sephii commented Jan 26, 2025

We used to have this done in the build phase (which, in my opinion, is a better place to have it) before it was removed in 27b7132 (because the way it was done caused an import from derivation, but I think it can be easily adapted). How about doing the check in the build phase?

@Stunkymonkey
Copy link
Contributor Author

@sephii doing this while building is an ever better idea. To catch the errors as early as possible. I am marking my MR as draft and ping you again if I have something. Thanks for the fast response.

@Stunkymonkey Stunkymonkey marked this pull request as draft January 26, 2025 21:35
@Stunkymonkey Stunkymonkey force-pushed the caddy-validate branch 3 times, most recently from aa16781 to 59bfa80 Compare January 27, 2025 22:45
@Stunkymonkey
Copy link
Contributor Author

Stunkymonkey commented Jan 27, 2025

@sephii validating is a bit weird. caddy validate results into:

       > 2025/01/27 21:46:30.999 INFO    using config from file  {"file": "/nix/store/kz8lm6k81lsmziphpq0mfhdrv4nx049v-Caddyfile-formatted/Caddyfile"}
       > 2025/01/27 21:46:31.009 INFO    adapted config to JSON  {"adapter": "caddyfile"}
       > Error: setting up custom log 'log1': opening log writer using &logging.FileWriter{Filename:"/var/log/caddy/access-http:__localhost:8081.log", Mode:0x0, Roll:(*bool)(nil), RollSizeMB:0, RollCompress:(*bool)(nil), RollLocalTime:false, RollKeep:0, RollKeepDays:0}: mkdir /var: permission denied

it always wants to start and log the events, which can not work... so then i discovered caddy adapt, which validates the config itself. But somehow the json-test is not working any more, because of:

       last 1 log lines:
       > Error: Unexpected '{' on a new line; did you mean to place the '{' on the previous line?, at /nix/store/f2927acpq53srgy3sl722ssqj4yrn60k-caddy.json:10
       For full logs, run 'nix log /nix/store/b6ccpz1rfqvnj4ysdm4qmf5m4wb7r4vd-validate-caddy-config.drv'.

but the config is a valid json... not sure why caddy does this: https://github.com/caddyserver/caddy/blob/9996d6a70ba76a94dfc90548f25fbac0ce9da497/caddyconfig/caddyfile/parse.go#L656

@sephii
Copy link
Contributor

sephii commented Feb 5, 2025

Sorry I’m very busy these days. I’ll give it a look soon, maybe next week!

@Stunkymonkey
Copy link
Contributor Author

@sephii no problem. thanks for reaching out.

@Stunkymonkey
Copy link
Contributor Author

I am currently stuck here caddyserver/caddy#6788 (comment) .

@mohammed90
Copy link

caddy adapt, which validates the config itself

That's not what adapt does. It doesn't validate config. It only converts it from whatever format the specified adapter accepts to JSON. The validation is something separate.

@Stunkymonkey Stunkymonkey force-pushed the caddy-validate branch 5 times, most recently from da256cd to 2de2277 Compare February 13, 2025 21:01
@Stunkymonkey Stunkymonkey changed the title nixos/caddy: validate before start nixos/caddy: validate at build-time Feb 13, 2025
@Stunkymonkey Stunkymonkey marked this pull request as ready for review February 13, 2025 21:02
@Stunkymonkey
Copy link
Contributor Author

@sephii i finally managed to put something together. please take your time.

@sephii
Copy link
Contributor

sephii commented Feb 26, 2025

Ok I finally got some time for this, thanks for your patience. Based on your code I worked out a different implementation that does not rely on an assertion but on running the adapt command in the Caddyfile generation (inspired by the way it’s done in the nginx module).

I’ll let you check it out and make changes to your PR if you think it’s good, otherwise please let me know.

@Stunkymonkey
Copy link
Contributor Author

@sephii thanks for this. much more cleaner and simpler. lets not over-complicate it.

@sephii
Copy link
Contributor

sephii commented Mar 2, 2025

Could you please also revert the change to the adapterParam variable which is not necessary anymore?

@Stunkymonkey
Copy link
Contributor Author

@sephii should be correct now. thanks for your help and your suggestions.

Copy link
Contributor

@sephii sephii left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks!

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/5269

@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one person. label Mar 11, 2025
@Stunkymonkey
Copy link
Contributor Author

@sephii are you fine with merging? I could do it, or do you want additional reviewer?

@sephii
Copy link
Contributor

sephii commented Mar 25, 2025

If you can merge it, please do!

@Stunkymonkey Stunkymonkey merged commit fb0fb09 into NixOS:master Mar 25, 2025
28 checks passed
@Stunkymonkey Stunkymonkey deleted the caddy-validate branch March 25, 2025 21:53

# 'validate' cannot be used for validation, due to log location access
# See https://github.com/caddyserver/caddy/issues/6788
${lib.getExe cfg.package} adapt --config $out/Caddyfile
Copy link
Contributor

Choose a reason for hiding this comment

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

This breaks cross-compiled nixos configs, since you can't run a non native binary on a different kind of build machine.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be enough to pass cfg.package as a native build input on line 53 and then just call caddy adapt?

@cything
Copy link
Contributor

cything commented Mar 27, 2025

Have you tested this with environment variables? I use an environment variable for API token and the build fails with Error: parsing caddyfile tokens for 'tls': missing API token. This is the part it's complaining about:

(common) {
          encode zstd gzip
          header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
          tls {
            dns cloudflare {$CLOUDFLARE_KEY}
            resolvers 1.1.1.1 8.8.8.8
          }
        }

The environment variable is obviously not available at build time.

@sephii
Copy link
Contributor

sephii commented Mar 27, 2025

This seems indeed incompatible with local validation. How about we add a validateConfigFile toggle option (like the nginx module)?

I suggest we also add an entry to the release notes since this change is not as backwards compatible as I thought (or set validateConfigFile to false by default, what’s your opinion on this?).

@cything
Copy link
Contributor

cything commented Mar 27, 2025

This seems indeed incompatible with local validation. How about we add a validateConfigFile toggle option (like the nginx module)?

If this breaks everyone passing API tokens with the environmentFile option, we definitely need this.

I suggest we also add an entry to the release notes since this change is not as backwards compatible as I thought (or set validateConfigFile to false by default, what’s your opinion on this?).

If this is limited to environment variables, we could set validateConfigFile to false if environmentFile is set and default to true otherwise. And add a description under validateConfigFile explaining why.

@Stunkymonkey
Copy link
Contributor Author

maybe we should merge #393806 until we figured out the rest of the problems

sorry for the delayed response, I was ill for a few days

@6543
Copy link
Member

6543 commented Mar 31, 2025

there was #380894 already dealing with this stuff ...

@6543
Copy link
Member

6543 commented Mar 31, 2025

@Stunkymonkey you removed the nativeBuildInputs = [ cfg.package ]; - have you tested that it works in cross-compile environments now!

@6543
Copy link
Member

6543 commented Mar 31, 2025

PS: we should make it a config to disable it! as there are cases where it will fail!

example: have static own ssl certs on the target host but not on the build host ...

@6543
Copy link
Member

6543 commented Mar 31, 2025

ok I have updated #380894 ...

@sephii
Copy link
Contributor

sephii commented Apr 2, 2025

there was #380894 already dealing with this stuff ...

This PR was created on Jan 26, your PR was created on Feb 10.

Your PR seems to suffer from the same problem that was mentioned in this discussion regarding the use of environment variables in the Caddy config. We should either disable the configuration check at build time by default, or find a way to only enable it if we’re sure the config can be checked locally.

@6543
Copy link
Member

6543 commented Apr 3, 2025

the default setup can be enabled only quite complex can not ... and in this case the option can simply be disabled ...

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 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants