varnish: fix localstatedir for varnish* tools (#17508)

The varnish tools (varnishstat, varnishlog, ...) tried to load the VSM
file from a spurious var directory in the Nix store. Fix the default so
the tools "just work" when also keeping services.varnish.stateDir at the
default.

Notes:
- The tools use $localstatedir/$HOSTNAME so I've adapted the default for
stateDir as well to contain hostName.
- Added postStop action to remove the localstatedir. There is no point
in keeping it around when varnish does not run, as it regenerates it
on startup anyway.

Fixes #7495

authored by Christian Kauhaus and committed by Rok Garbas ea7e705c 8f52a0f9

+6 -1
+4 -1
nixos/modules/services/web-servers/varnish/default.nix
··· 28 }; 29 30 stateDir = mkOption { 31 - default = "/var/spool/varnish"; 32 description = " 33 Directory holding all state for Varnish to run. 34 "; ··· 45 preStart = '' 46 mkdir -p ${cfg.stateDir} 47 chown -R varnish:varnish ${cfg.stateDir} 48 ''; 49 path = [ pkgs.gcc ]; 50 serviceConfig.ExecStart = "${pkgs.varnish}/sbin/varnishd -a ${cfg.http_address} -f ${pkgs.writeText "default.vcl" cfg.config} -n ${cfg.stateDir} -u varnish";
··· 28 }; 29 30 stateDir = mkOption { 31 + default = "/var/spool/varnish/${config.networking.hostName}"; 32 description = " 33 Directory holding all state for Varnish to run. 34 "; ··· 45 preStart = '' 46 mkdir -p ${cfg.stateDir} 47 chown -R varnish:varnish ${cfg.stateDir} 48 + ''; 49 + postStop = '' 50 + rm -rf ${cfg.stateDir} 51 ''; 52 path = [ pkgs.gcc ]; 53 serviceConfig.ExecStart = "${pkgs.varnish}/sbin/varnishd -a ${cfg.http_address} -f ${pkgs.writeText "default.vcl" cfg.config} -n ${cfg.stateDir} -u varnish";
+2
pkgs/servers/varnish/default.nix
··· 13 buildInputs = [ pcre libxslt groff ncurses pkgconfig readline python 14 pythonPackages.docutils]; 15 16 meta = { 17 description = "Web application accelerator also known as a caching HTTP reverse proxy"; 18 homepage = "https://www.varnish-cache.org";
··· 13 buildInputs = [ pcre libxslt groff ncurses pkgconfig readline python 14 pythonPackages.docutils]; 15 16 + buildFlags = "localstatedir=/var/spool"; 17 + 18 meta = { 19 description = "Web application accelerator also known as a caching HTTP reverse proxy"; 20 homepage = "https://www.varnish-cache.org";