1{ pkgs, package, ... }:
2let
3 testPath = pkgs.hello;
4
5 # Same stateDir logic as in nixos/modules/services/web-servers/varnish/default.nix
6 stateDir =
7 hostName:
8 if (pkgs.lib.versionOlder package.version "7") then
9 "/var/run/varnish/${hostName}"
10 else
11 "/var/run/varnishd";
12in
13{
14 name = "varnish";
15 meta = {
16 maintainers = [ ];
17 };
18
19 nodes = {
20 varnish =
21 {
22 config,
23 pkgs,
24 lib,
25 ...
26 }:
27 {
28 services.nix-serve = {
29 enable = true;
30 };
31
32 services.varnish = {
33 inherit package;
34 enable = true;
35 http_address = "0.0.0.0:81";
36 listen = [
37 {
38 address = "0.0.0.0";
39 port = 80;
40 proto = "HTTP";
41 }
42 {
43 name = "proxyport";
44 address = "0.0.0.0";
45 port = 8080;
46 proto = "PROXY";
47 }
48 {
49 address = "${stateDir config.networking.hostName}/client.http.sock";
50 user = "varnish";
51 group = "varnish";
52 mode = "660";
53 }
54 ]
55 ++ lib.optionals (lib.versionAtLeast package.version "7.3") [
56 # Support added in 7.3.0
57 { address = "@asdf"; }
58 ];
59 config = ''
60 vcl 4.1;
61
62 backend nix-serve {
63 .host = "127.0.0.1";
64 .port = "${toString config.services.nix-serve.port}";
65 }
66 '';
67 };
68
69 networking.firewall.allowedTCPPorts = [ 80 ];
70 system.extraDependencies = [ testPath ];
71
72 assertions =
73 let
74 cmdline = config.systemd.services.varnish.serviceConfig.ExecStart;
75 in
76 map
77 (pattern: {
78 assertion = lib.hasInfix pattern cmdline;
79 message = "Address argument `${pattern}` missing in commandline `${cmdline}`.";
80 })
81 (
82 [
83 " -a 0.0.0.0:80,HTTP "
84 " -a proxyport=0.0.0.0:8080,PROXY "
85 " -a ${stateDir config.networking.hostName}/client.http.sock,HTTP,user=varnish,group=varnish,mode=660 "
86 " -a 0.0.0.0:81 "
87 ]
88 ++ lib.optionals (lib.versionAtLeast package.version "7.3") [
89 " -a @asdf,HTTP "
90 ]
91 );
92 };
93
94 client =
95 { lib, ... }:
96 {
97 nix.settings = {
98 require-sigs = false;
99 substituters = lib.mkForce [ "http://varnish" ];
100 };
101 };
102 };
103
104 testScript = ''
105 start_all()
106 varnish.wait_for_open_port(80)
107
108
109 client.wait_until_succeeds("curl -f http://varnish/nix-cache-info");
110
111 client.wait_until_succeeds("nix-store -r ${testPath}")
112 client.succeed("${testPath}/bin/hello")
113
114 output = varnish.succeed("varnishadm status")
115 print(output)
116 assert "Child in state running" in output, "Unexpected varnishadm response"
117 '';
118}