2 # 1. nginx generates config file with shared http context definitions above
3 # generated virtual hosts config.
4 # 2. whether the ETag header is properly generated whenever we're serving
5 # files in Nix store paths
6 # 3. nginx doesn't restart on configuration changes (only reloads)
7 import ./make-test-python.nix ({ pkgs, ... }: {
9 meta = with pkgs.lib.maintainers; {
10 maintainers = [ mbbx6spp danbst ];
14 webserver = { pkgs, lib, ... }: {
15 services.nginx.enable = true;
16 services.nginx.commonHttpConfig = ''
17 log_format ceeformat '@cee: {"status":"$status",'
18 '"request_time":$request_time,'
19 '"upstream_response_time":$upstream_response_time,'
20 '"pipe":"$pipe","bytes_sent":$bytes_sent,'
21 '"connection":"$connection",'
22 '"remote_addr":"$remote_addr",'
24 '"timestamp":"$time_iso8601",'
25 '"request":"$request",'
26 '"http_referer":"$http_referer",'
27 '"upstream_addr":"$upstream_addr"}';
29 services.nginx.virtualHosts."0.my.test" = {
31 access_log syslog:server=unix:/dev/log,facility=user,tag=mytag,severity=info ceeformat;
32 location /favicon.ico { allow all; access_log off; log_not_found off; }
36 services.nginx.virtualHosts.localhost = {
37 root = pkgs.runCommand "testdir" {} ''
39 echo hello world > "$out/index.html"
43 services.nginx.enableReload = true;
45 specialisation.etagSystem.configuration = {
46 services.nginx.virtualHosts.localhost = {
47 root = lib.mkForce (pkgs.runCommand "testdir2" {} ''
49 echo content changed > "$out/index.html"
54 specialisation.justReloadSystem.configuration = {
55 services.nginx.virtualHosts."1.my.test".listen = [ { addr = "127.0.0.1"; port = 8080; }];
58 specialisation.reloadRestartSystem.configuration = {
59 services.nginx.package = pkgs.nginxMainline;
62 specialisation.reloadWithErrorsSystem.configuration = {
63 services.nginx.package = pkgs.nginxMainline;
64 services.nginx.virtualHosts."!@$$(#*%".locations."~@#*$*!)".proxyPass = ";;;";
69 testScript = { nodes, ... }: let
70 etagSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/etagSystem";
71 justReloadSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/justReloadSystem";
72 reloadRestartSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/reloadRestartSystem";
73 reloadWithErrorsSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/reloadWithErrorsSystem";
75 url = "http://localhost/index.html"
79 etag = webserver.succeed(
80 f'curl -v {url} 2>&1 | sed -n -e "s/^< etag: *//ip"'
82 http_code = webserver.succeed(
83 f"curl -w '%{{http_code}}' --head --fail -H 'If-None-Match: {etag}' {url}"
85 assert http_code.split("\n")[-1] == "304"
90 webserver.wait_for_unit("nginx")
91 webserver.wait_for_open_port(80)
93 with subtest("check ETag if serving Nix store paths"):
94 old_etag = check_etag()
96 "${etagSystem}/bin/switch-to-configuration test >&2"
99 new_etag = check_etag()
100 assert old_etag != new_etag
102 with subtest("config is reloaded on nixos-rebuild switch"):
104 "${justReloadSystem}/bin/switch-to-configuration test >&2"
106 webserver.wait_for_open_port(8080)
107 webserver.fail("journalctl -u nginx | grep -q -i stopped")
108 webserver.succeed("journalctl -u nginx | grep -q -i reloaded")
110 with subtest("restart when nginx package changes"):
112 "${reloadRestartSystem}/bin/switch-to-configuration test >&2"
114 webserver.wait_for_unit("nginx")
115 webserver.succeed("journalctl -u nginx | grep -q -i stopped")
117 with subtest("nixos-rebuild --switch should fail when there are configuration errors"):
119 "${reloadWithErrorsSystem}/bin/switch-to-configuration test >&2"
121 webserver.succeed("[[ $(systemctl is-failed nginx-config-reload) == failed ]]")
122 webserver.succeed("[[ $(systemctl is-failed nginx) == active ]]")
123 # just to make sure operation is idempotent. During development I had a situation
124 # when first time it shows error, but stops showing it on subsequent rebuilds
126 "${reloadWithErrorsSystem}/bin/switch-to-configuration test >&2"