Skip to content

Commit 0640622

Browse files
committed
nixos/varnish: fix stateDir to allow direct use of varnishadm
1 parent ba79b6d commit 0640622

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@
88
let
99
cfg = config.services.varnish;
1010

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+
1129
commandLine =
1230
"-f ${pkgs.writeText "default.vcl" cfg.config}"
1331
+
@@ -17,6 +35,14 @@ let
1735
}' -r vmod_path";
1836
in
1937
{
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+
2046
options = {
2147
services.varnish = {
2248
enable = lib.mkEnableOption "Varnish Server";
@@ -42,14 +68,6 @@ in
4268
'';
4369
};
4470

45-
stateDir = lib.mkOption {
46-
type = lib.types.path;
47-
default = "/run/varnish/${config.networking.hostName}";
48-
defaultText = lib.literalExpression ''"/run/varnish/''${config.networking.hostName}"'';
49-
description = ''
50-
Directory holding all state for Varnish to run. Note that this should be a tmpfs in order to avoid performance issues and crashes.
51-
'';
52-
};
5371
extraModules = lib.mkOption {
5472
type = lib.types.listOf lib.types.package;
5573
default = [ ];
@@ -76,24 +94,15 @@ in
7694
description = "Varnish";
7795
wantedBy = [ "multi-user.target" ];
7896
after = [ "network.target" ];
79-
preStart = lib.mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
80-
mkdir -p ${cfg.stateDir}
81-
chown -R varnish:varnish ${cfg.stateDir}
82-
'';
83-
postStop = lib.mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
84-
rm -rf ${cfg.stateDir}
85-
'';
8697
serviceConfig = {
8798
Type = "simple";
8899
PermissionsStartOnly = true;
89-
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}";
90101
Restart = "always";
91102
RestartSec = "5s";
92103
User = "varnish";
93104
Group = "varnish";
94-
RuntimeDirectory = lib.mkIf (lib.hasPrefix "/run/" cfg.stateDir) (
95-
lib.removePrefix "/run/" cfg.stateDir
96-
);
105+
RuntimeDirectory = lib.removePrefix "/var/run/" stateDir;
97106
AmbientCapabilities = "cap_net_bind_service";
98107
NoNewPrivileges = true;
99108
LimitNOFILE = 131072;

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: 1 addition & 1 deletion
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"

0 commit comments

Comments
 (0)