git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/nginx: don't hide nginx config errors on nixos-rebuild --switch with reload enabled (#76179)

nixos/nginx: don't hide nginx config errors on nixos-rebuild --switch with reload enabled Closes https://github.com/NixOS/nixpkgs/issues/73455

commitcef68c4580f1d5bb648d0c7ce969f696fa1a2459 authorDanylo Hlynskyi <abcz2.uprola@gmail.com> dateSun 5 Jan 2020, 00:39 (7 years ago) parent4c9e280
2 files changed+32−5
nixos/modules/services/web-servers/nginx/default.nix+13−4 nixos/tests/nginx.nix+19−1
nixos/modules/services/web-servers/nginx/default.nix+13−4
@@ -178,6 +178,8 @@ let
178178 then "/etc/nginx/nginx.conf"
179179 else configFile;
180180
181+ execCommand = "${cfg.package}/bin/nginx -c '${configPath}' -p '${cfg.stateDir}'";
182+
181183 vhosts = concatStringsSep "\n" (mapAttrsToList (vhostName: vhost:
182184 let
183185 onlySSL = vhost.onlySSL || vhost.enableSSL;
@@ -682,10 +684,10 @@ in
682684 stopIfChanged = false;
683685 preStart = ''
684686 ${cfg.preStart}
685− ${cfg.package}/bin/nginx -c '${configPath}' -p '${cfg.stateDir}' -t
687+ ${execCommand} -t
686688 '';
687689 serviceConfig = {
688− ExecStart = "${cfg.package}/bin/nginx -c '${configPath}' -p '${cfg.stateDir}'";
690+ ExecStart = execCommand;
689691 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
690692 Restart = "always";
691693 RestartSec = "10s";
@@ -706,11 +708,18 @@ in
706708 };
707709
708710 systemd.services.nginx-config-reload = mkIf cfg.enableReload {
709− wantedBy = [ "nginx.service" ];
711+ wants = [ "nginx.service" ];
712+ wantedBy = [ "multi-user.target" ];
710713 restartTriggers = [ configFile ];
714+ # commented, because can cause extra delays during activate for this config:
715+ # services.nginx.virtualHosts."_".locations."/".proxyPass = "http://blabla:3000";
716+ # stopIfChanged = false;
717+ serviceConfig.Type = "oneshot";
718+ serviceConfig.TimeoutSec = 60;
711719 script = ''
712720 if ${pkgs.systemd}/bin/systemctl -q is-active nginx.service ; then
713− ${pkgs.systemd}/bin/systemctl reload nginx.service
721+ ${execCommand} -t && \
722+ ${pkgs.systemd}/bin/systemctl reload nginx.service
714723 fi
715724 '';
716725 serviceConfig.RemainAfterExit = true;
nixos/tests/nginx.nix+19−1
@@ -7,7 +7,7 @@
77import ./make-test-python.nix ({ pkgs, ... }: {
88 name = "nginx";
99 meta = with pkgs.stdenv.lib.maintainers; {
10− maintainers = [ mbbx6spp ];
10+ maintainers = [ mbbx6spp danbst ];
1111 };
1212
1313 nodes = {
@@ -59,6 +59,11 @@ import ./make-test-python.nix ({ pkgs, ... }: {
5959 {
6060 services.nginx.package = pkgs.nginxUnstable;
6161 }
62+
63+ {
64+ services.nginx.package = pkgs.nginxUnstable;
65+ services.nginx.virtualHosts."!@$$(#*%".locations."~@#*$*!)".proxyPass = ";;;";
66+ }
6267 ];
6368 };
6469
@@ -68,6 +73,7 @@ import ./make-test-python.nix ({ pkgs, ... }: {
6873 etagSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-1";
6974 justReloadSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-2";
7075 reloadRestartSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-3";
76+ reloadWithErrorsSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-4";
7177 in ''
7278 url = "http://localhost/index.html"
7379
@@ -110,5 +116,17 @@ import ./make-test-python.nix ({ pkgs, ... }: {
110116 )
111117 webserver.wait_for_unit("nginx")
112118 webserver.succeed("journalctl -u nginx | grep -q -i stopped")
119+
120+ with subtest("nixos-rebuild --switch should fail when there are configuration errors"):
121+ webserver.fail(
122+ "${reloadWithErrorsSystem}/bin/switch-to-configuration test >&2"
123+ )
124+ webserver.succeed("[[ $(systemctl is-failed nginx-config-reload) == failed ]]")
125+ webserver.succeed("[[ $(systemctl is-failed nginx) == active ]]")
126+ # just to make sure operation is idempotent. During development I had a situation
127+ # when first time it shows error, but stops showing it on subsequent rebuilds
128+ webserver.fail(
129+ "${reloadWithErrorsSystem}/bin/switch-to-configuration test >&2"
130+ )
113131 '';
114132})