Skip to content

Commit c068347

Browse files
authored
Fixes and housekeeping for the varnish module (NixOS#373747)
2 parents 6730279 + 0640622 commit c068347

File tree

7 files changed

+69
-45
lines changed

7 files changed

+69
-45
lines changed

nixos/modules/services/web-servers/varnish/default.nix

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,80 @@
55
...
66
}:
77

8-
with lib;
9-
108
let
119
cfg = config.services.varnish;
1210

11+
# Varnish has very strong opinions and very complicated code around handling
12+
# the stateDir. After a lot of back and forth, we decided that we a)
13+
# do not want a configurable option here, as most of the handling depends
14+
# on the version and the compile time options. Putting everything into
15+
# /var/run (RAM backed) is absolutely recommended by Varnish anyways.
16+
# We do need to pay attention to the version-dependend variations, though!
17+
stateDir =
18+
if
19+
(lib.versionOlder cfg.package.version "7")
20+
# Remove after Varnish 6.0 is gone. In 6.0 varnishadm always appends the
21+
# hostname (by default) and can't be nudged to not use any name. This has
22+
# long changed by 7.5 and can be used without the host name.
23+
then
24+
"/var/run/varnish/${config.networking.hostName}"
25+
# Newer varnish uses this:
26+
else
27+
"/var/run/varnishd";
28+
1329
commandLine =
1430
"-f ${pkgs.writeText "default.vcl" cfg.config}"
1531
+
16-
optionalString (cfg.extraModules != [ ])
32+
lib.optionalString (cfg.extraModules != [ ])
1733
" -p vmod_path='${
18-
makeSearchPathOutput "lib" "lib/varnish/vmods" ([ cfg.package ] ++ cfg.extraModules)
34+
lib.makeSearchPathOutput "lib" "lib/varnish/vmods" ([ cfg.package ] ++ cfg.extraModules)
1935
}' -r vmod_path";
2036
in
2137
{
38+
imports = [
39+
(lib.mkRemovedOptionModule [
40+
"services"
41+
"varnish"
42+
"stateDir"
43+
] "The `stateDir` option never was functional or useful. varnish uses compile-time settings.")
44+
];
45+
2246
options = {
2347
services.varnish = {
24-
enable = mkEnableOption "Varnish Server";
48+
enable = lib.mkEnableOption "Varnish Server";
2549

26-
enableConfigCheck = mkEnableOption "checking the config during build time" // {
50+
enableConfigCheck = lib.mkEnableOption "checking the config during build time" // {
2751
default = true;
2852
};
2953

30-
package = mkPackageOption pkgs "varnish" { };
54+
package = lib.mkPackageOption pkgs "varnish" { };
3155

32-
http_address = mkOption {
33-
type = types.str;
56+
http_address = lib.mkOption {
57+
type = lib.types.str;
3458
default = "*:6081";
3559
description = ''
3660
HTTP listen address and port.
3761
'';
3862
};
3963

40-
config = mkOption {
41-
type = types.lines;
64+
config = lib.mkOption {
65+
type = lib.types.lines;
4266
description = ''
4367
Verbatim default.vcl configuration.
4468
'';
4569
};
4670

47-
stateDir = mkOption {
48-
type = types.path;
49-
default = "/run/varnish/${config.networking.hostName}";
50-
defaultText = literalExpression ''"/run/varnish/''${config.networking.hostName}"'';
51-
description = ''
52-
Directory holding all state for Varnish to run. Note that this should be a tmpfs in order to avoid performance issues and crashes.
53-
'';
54-
};
55-
56-
extraModules = mkOption {
57-
type = types.listOf types.package;
71+
extraModules = lib.mkOption {
72+
type = lib.types.listOf lib.types.package;
5873
default = [ ];
59-
example = literalExpression "[ pkgs.varnishPackages.geoip ]";
74+
example = lib.literalExpression "[ pkgs.varnishPackages.geoip ]";
6075
description = ''
6176
Varnish modules (except 'std').
6277
'';
6378
};
6479

65-
extraCommandLine = mkOption {
66-
type = types.str;
80+
extraCommandLine = lib.mkOption {
81+
type = lib.types.str;
6782
default = "";
6883
example = "-s malloc,256M";
6984
description = ''
@@ -74,30 +89,20 @@ in
7489

7590
};
7691

77-
config = mkIf cfg.enable {
78-
92+
config = lib.mkIf cfg.enable {
7993
systemd.services.varnish = {
8094
description = "Varnish";
8195
wantedBy = [ "multi-user.target" ];
8296
after = [ "network.target" ];
83-
preStart = mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
84-
mkdir -p ${cfg.stateDir}
85-
chown -R varnish:varnish ${cfg.stateDir}
86-
'';
87-
postStop = mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
88-
rm -rf ${cfg.stateDir}
89-
'';
9097
serviceConfig = {
9198
Type = "simple";
9299
PermissionsStartOnly = true;
93-
ExecStart = "${cfg.package}/sbin/varnishd -a ${cfg.http_address} -n ${cfg.stateDir} -F ${cfg.extraCommandLine} ${commandLine}";
100+
ExecStart = "${cfg.package}/sbin/varnishd -a ${cfg.http_address} -n ${stateDir} -F ${cfg.extraCommandLine} ${commandLine}";
94101
Restart = "always";
95102
RestartSec = "5s";
96103
User = "varnish";
97104
Group = "varnish";
98-
RuntimeDirectory = mkIf (lib.hasPrefix "/run/" cfg.stateDir) (
99-
lib.removePrefix "/run/" cfg.stateDir
100-
);
105+
RuntimeDirectory = lib.removePrefix "/var/run/" stateDir;
101106
AmbientCapabilities = "cap_net_bind_service";
102107
NoNewPrivileges = true;
103108
LimitNOFILE = 131072;
@@ -107,7 +112,7 @@ in
107112
environment.systemPackages = [ cfg.package ];
108113

109114
# check .vcl syntax at compile time (e.g. before nixops deployment)
110-
system.checks = mkIf cfg.enableConfigCheck [
115+
system.checks = lib.mkIf cfg.enableConfigCheck [
111116
(pkgs.runCommand "check-varnish-syntax" { } ''
112117
${cfg.package}/bin/varnishd -C ${commandLine} 2> $out || (cat $out; exit 1)
113118
'')

nixos/tests/all-tests.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ in {
11431143
v2ray = handleTest ./v2ray.nix {};
11441144
varnish60 = handleTest ./varnish.nix { package = pkgs.varnish60; };
11451145
varnish75 = handleTest ./varnish.nix { package = pkgs.varnish75; };
1146+
varnish76 = handleTest ./varnish.nix { package = pkgs.varnish76; };
11461147
vault = handleTest ./vault.nix {};
11471148
vault-agent = handleTest ./vault-agent.nix {};
11481149
vault-dev = handleTest ./vault-dev.nix {};

nixos/tests/varnish.nix

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ import ./make-test-python.nix (
5656
5757
client.wait_until_succeeds("curl -f http://varnish/nix-cache-info");
5858
59-
client.wait_until_succeeds("nix-store -r ${testPath}");
60-
client.succeed("${testPath}/bin/hello");
59+
client.wait_until_succeeds("nix-store -r ${testPath}")
60+
client.succeed("${testPath}/bin/hello")
61+
62+
output = varnish.succeed("varnishadm status")
63+
print(output)
64+
assert "Child in state running" in output, "Unexpected varnishadm response"
6165
'';
6266
}
6367
)

pkgs/servers/varnish/default.nix

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let
5454
++ lib.optional stdenv.hostPlatform.isDarwin libunwind
5555
++ lib.optional stdenv.hostPlatform.isLinux jemalloc;
5656

57-
buildFlags = [ "localstatedir=/var/spool" ];
57+
buildFlags = [ "localstatedir=/var/run" ];
5858

5959
postPatch = ''
6060
substituteInPlace bin/varnishtest/vtc_main.c --replace /bin/rm "${coreutils}/bin/rm"
@@ -83,7 +83,7 @@ let
8383
description = "Web application accelerator also known as a caching HTTP reverse proxy";
8484
homepage = "https://www.varnish-cache.org";
8585
license = licenses.bsd2;
86-
maintainers = [ ];
86+
maintainers = lib.teams.flyingcircus.members;
8787
platforms = platforms.unix;
8888
};
8989
};
@@ -99,4 +99,9 @@ in
9999
version = "7.5.0";
100100
hash = "sha256-/KYbmDE54arGHEVG0SoaOrmAfbsdgxRXHjFIyT/3K10=";
101101
};
102+
# EOL 2025-09-15
103+
varnish76 = common {
104+
version = "7.6.1";
105+
hash = "sha256-Wpu1oUn/J4Z7VKZs4W0qS5Pt/6VHPLh8nHH3aZz4Rbo=";
106+
};
102107
}

pkgs/servers/varnish/modules.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,8 @@ in
5959
version = "0.24.0";
6060
hash = "sha256-2MfcrhhkBz9GyQxEWzjipdn1CBEqnCvC3t1G2YSauak=";
6161
};
62+
modules25 = common {
63+
version = "0.25.0";
64+
hash = "sha256-m/7moizVyvoP8xnpircAFVUqCmCfTGkgVyRc6zkdVsk=";
65+
};
6266
}

pkgs/servers/varnish/packages.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
callPackage,
44
varnish60,
55
varnish75,
6+
varnish76,
67
}:
78
{
89
varnish60Packages = rec {
@@ -23,4 +24,8 @@
2324
varnish = varnish75;
2425
modules = (callPackages ./modules.nix { inherit varnish; }).modules24;
2526
};
27+
varnish76Packages = rec {
28+
varnish = varnish76;
29+
modules = (callPackages ./modules.nix { inherit varnish; }).modules25;
30+
};
2631
}

pkgs/top-level/all-packages.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5476,9 +5476,9 @@ with pkgs;
54765476
unzipNLS = lowPrio (unzip.override { enableNLS = true; });
54775477

54785478
inherit (callPackages ../servers/varnish { })
5479-
varnish60 varnish75;
5479+
varnish60 varnish75 varnish76;
54805480
inherit (callPackages ../servers/varnish/packages.nix { })
5481-
varnish60Packages varnish75Packages;
5481+
varnish60Packages varnish75Packages varnish76Packages;
54825482

54835483
varnishPackages = varnish75Packages;
54845484
varnish = varnishPackages.varnish;

0 commit comments

Comments
 (0)