nixos/nginx: validate config at build time#205561
Conversation
Shamelessly stolen from nixcloud-webservices: https://github.com/nixcloud/nixcloud-webservices/blob/master/modules/web/webserver/lib/nginx_check_config.nix The nixos test testing the behavior of nginx in case of faulty config would not build with this change (on purpose), so I modified it so that the failure is not syntactic.
1e26f3c to
26a411b
Compare
|
@ofborg test nginx |
|
Aren't we already sort of doing this here? |
|
This runs gixy, which is a linter for some specific kinds of semantic issues, but not syntax errors. Note that this change caught a (deliberate) syntax error in the nginx nixos test that was not caught by gixy. |
|
Will self-merge in a few days if nobody opposes. |
|
This breaks in presence of backend DNS names in nginx configuration, since the sandbox can't access DNS: |
Do you think it's a dealbreaker even if |
|
Could you share more of the config? I'm trying to reproduce with the nginx nixos test: diff --git a/nixos/tests/nginx.nix b/nixos/tests/nginx.nix
index d9d073822a1..1b06759c162 100644
--- a/nixos/tests/nginx.nix
+++ b/nixos/tests/nginx.nix
@@ -13,6 +13,7 @@ import ./make-test-python.nix ({ pkgs, ... }: {
nodes = {
webserver = { pkgs, lib, ... }: {
services.nginx.enable = true;
+ services.nginx.upstreams.yay.servers."1.2.3.4:3000" = {};
services.nginx.commonHttpConfig = ''
log_format ceeformat '@cee: {"status":"$status",'
'"request_time":$request_time,'
@@ -57,6 +58,7 @@ import ./make-test-python.nix ({ pkgs, ... }: {
specialisation.reloadRestartSystem.configuration = {
services.nginx.package = pkgs.nginxMainline;
+ services.nginx.virtualHosts."yay.com".locations."/".proxyPass = "https://1.2.3.4:443";
};
specialisation.reloadWithErrorsSystem.configuration = {but it still builds. |
You need DNS in your |
diff --git a/nixos/tests/nginx.nix b/nixos/tests/nginx.nix
index d9d073822a1..4172f3ea365 100644
--- a/nixos/tests/nginx.nix
+++ b/nixos/tests/nginx.nix
@@ -13,6 +13,7 @@ import ./make-test-python.nix ({ pkgs, ... }: {
nodes = {
webserver = { pkgs, lib, ... }: {
services.nginx.enable = true;
+ services.nginx.upstreams.yay.servers."example.com:3000" = {};
services.nginx.commonHttpConfig = ''
log_format ceeformat '@cee: {"status":"$status",'
'"request_time":$request_time,'
@@ -57,6 +58,7 @@ import ./make-test-python.nix ({ pkgs, ... }: {
specialisation.reloadRestartSystem.configuration = {
services.nginx.package = pkgs.nginxMainline;
+ services.nginx.virtualHosts."yay.com".locations."/".proxyPass = "https://some.dns.info:443";
};
specialisation.reloadWithErrorsSystem.configuration = {also builds sucessfully. |
I've disabled the config validation for my machine config for now, but I don't have a strong opinion on how important this regression is -- the module maintainers might. |
|
forget what I said, I was not trying on a recent enough revision, it did not contain this PR yet... I can reproduce, thanks |
|
Perhaps we just disable this by default, or document that one should disable it in this case? |
|
I propose a fix in #207413 |
|
Even with #207413 there are still other issues if your nginx has secrets: I would suggest to disable validation by default until we have a baseline. |
There still seem a lot of breakages not addressed yet: NixOS#205561
|
Please merge #207532 for now so that we can review potential fixes to this problem without hurry. |
| }; | ||
|
|
||
| validateConfig = mkOption { | ||
| default = pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform; |
There was a problem hiding this comment.
This should be pkgs.stdenv.buildPlatform.canExecute stdenv.hostPlatform
|
|
||
| mkCertOwnershipAssertion = import ../../../security/acme/mk-cert-ownership-assertion.nix; | ||
|
|
||
| snakeOilCert = pkgs.runCommand "nginx-config-validate-cert" { nativeBuildInputs = [ pkgs.openssl.bin ]; } '' |
There was a problem hiding this comment.
Not sure if we want to use runCommandLocal here. We probably don't want to cache or substitute this, or do we?
There was a problem hiding this comment.
this certificate is only used during build, so reusing it or sharing it via a binary cache is not a problem
| -e "s|ssl_certificate .*;|ssl_certificate ${snakeOilCert}/server.crt;|g" \ | ||
| -e "s|ssl_trusted_certificate .*;|ssl_trusted_certificate ${snakeOilCert}/server.crt;|g" \ | ||
| -e "s|ssl_certificate_key .*;|ssl_certificate_key ${snakeOilCert}/server.key;|g" \ |
There was a problem hiding this comment.
I am not sure how robust the regex is. For using the nix options it is certainly enough.
Description of changes
Shamelessly stolen from nixcloud-webservices:
https://github.com/nixcloud/nixcloud-webservices/blob/master/modules/web/webserver/lib/nginx_check_config.nix
The nixos test testing the behavior of nginx in case of faulty config
would not build with this change (on purpose), so I modified it so that
the failure is not syntactic.
cc @qknight
cc nginx maintainers @thoughtpolice @fpletz @globin @ajs124 @7c6f434c
Things done
sandbox = trueset innix.conf? (See Nix manual)nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/)nixos/doc/manual/md-to-db.shto update generated release notes