mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-07-13 13:40:28 +03:00
Fixes and housekeeping for the varnish module (#373747)
This commit is contained in:
commit
c068347f63
7 changed files with 69 additions and 45 deletions
|
@ -5,65 +5,80 @@
|
|||
...
|
||||
}:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
cfg = config.services.varnish;
|
||||
|
||||
# Varnish has very strong opinions and very complicated code around handling
|
||||
# the stateDir. After a lot of back and forth, we decided that we a)
|
||||
# do not want a configurable option here, as most of the handling depends
|
||||
# on the version and the compile time options. Putting everything into
|
||||
# /var/run (RAM backed) is absolutely recommended by Varnish anyways.
|
||||
# We do need to pay attention to the version-dependend variations, though!
|
||||
stateDir =
|
||||
if
|
||||
(lib.versionOlder cfg.package.version "7")
|
||||
# Remove after Varnish 6.0 is gone. In 6.0 varnishadm always appends the
|
||||
# hostname (by default) and can't be nudged to not use any name. This has
|
||||
# long changed by 7.5 and can be used without the host name.
|
||||
then
|
||||
"/var/run/varnish/${config.networking.hostName}"
|
||||
# Newer varnish uses this:
|
||||
else
|
||||
"/var/run/varnishd";
|
||||
|
||||
commandLine =
|
||||
"-f ${pkgs.writeText "default.vcl" cfg.config}"
|
||||
+
|
||||
optionalString (cfg.extraModules != [ ])
|
||||
lib.optionalString (cfg.extraModules != [ ])
|
||||
" -p vmod_path='${
|
||||
makeSearchPathOutput "lib" "lib/varnish/vmods" ([ cfg.package ] ++ cfg.extraModules)
|
||||
lib.makeSearchPathOutput "lib" "lib/varnish/vmods" ([ cfg.package ] ++ cfg.extraModules)
|
||||
}' -r vmod_path";
|
||||
in
|
||||
{
|
||||
imports = [
|
||||
(lib.mkRemovedOptionModule [
|
||||
"services"
|
||||
"varnish"
|
||||
"stateDir"
|
||||
] "The `stateDir` option never was functional or useful. varnish uses compile-time settings.")
|
||||
];
|
||||
|
||||
options = {
|
||||
services.varnish = {
|
||||
enable = mkEnableOption "Varnish Server";
|
||||
enable = lib.mkEnableOption "Varnish Server";
|
||||
|
||||
enableConfigCheck = mkEnableOption "checking the config during build time" // {
|
||||
enableConfigCheck = lib.mkEnableOption "checking the config during build time" // {
|
||||
default = true;
|
||||
};
|
||||
|
||||
package = mkPackageOption pkgs "varnish" { };
|
||||
package = lib.mkPackageOption pkgs "varnish" { };
|
||||
|
||||
http_address = mkOption {
|
||||
type = types.str;
|
||||
http_address = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "*:6081";
|
||||
description = ''
|
||||
HTTP listen address and port.
|
||||
'';
|
||||
};
|
||||
|
||||
config = mkOption {
|
||||
type = types.lines;
|
||||
config = lib.mkOption {
|
||||
type = lib.types.lines;
|
||||
description = ''
|
||||
Verbatim default.vcl configuration.
|
||||
'';
|
||||
};
|
||||
|
||||
stateDir = mkOption {
|
||||
type = types.path;
|
||||
default = "/run/varnish/${config.networking.hostName}";
|
||||
defaultText = literalExpression ''"/run/varnish/''${config.networking.hostName}"'';
|
||||
description = ''
|
||||
Directory holding all state for Varnish to run. Note that this should be a tmpfs in order to avoid performance issues and crashes.
|
||||
'';
|
||||
};
|
||||
|
||||
extraModules = mkOption {
|
||||
type = types.listOf types.package;
|
||||
extraModules = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.package;
|
||||
default = [ ];
|
||||
example = literalExpression "[ pkgs.varnishPackages.geoip ]";
|
||||
example = lib.literalExpression "[ pkgs.varnishPackages.geoip ]";
|
||||
description = ''
|
||||
Varnish modules (except 'std').
|
||||
'';
|
||||
};
|
||||
|
||||
extraCommandLine = mkOption {
|
||||
type = types.str;
|
||||
extraCommandLine = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "";
|
||||
example = "-s malloc,256M";
|
||||
description = ''
|
||||
|
@ -74,30 +89,20 @@ in
|
|||
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
systemd.services.varnish = {
|
||||
description = "Varnish";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network.target" ];
|
||||
preStart = mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
|
||||
mkdir -p ${cfg.stateDir}
|
||||
chown -R varnish:varnish ${cfg.stateDir}
|
||||
'';
|
||||
postStop = mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
|
||||
rm -rf ${cfg.stateDir}
|
||||
'';
|
||||
serviceConfig = {
|
||||
Type = "simple";
|
||||
PermissionsStartOnly = true;
|
||||
ExecStart = "${cfg.package}/sbin/varnishd -a ${cfg.http_address} -n ${cfg.stateDir} -F ${cfg.extraCommandLine} ${commandLine}";
|
||||
ExecStart = "${cfg.package}/sbin/varnishd -a ${cfg.http_address} -n ${stateDir} -F ${cfg.extraCommandLine} ${commandLine}";
|
||||
Restart = "always";
|
||||
RestartSec = "5s";
|
||||
User = "varnish";
|
||||
Group = "varnish";
|
||||
RuntimeDirectory = mkIf (lib.hasPrefix "/run/" cfg.stateDir) (
|
||||
lib.removePrefix "/run/" cfg.stateDir
|
||||
);
|
||||
RuntimeDirectory = lib.removePrefix "/var/run/" stateDir;
|
||||
AmbientCapabilities = "cap_net_bind_service";
|
||||
NoNewPrivileges = true;
|
||||
LimitNOFILE = 131072;
|
||||
|
@ -107,7 +112,7 @@ in
|
|||
environment.systemPackages = [ cfg.package ];
|
||||
|
||||
# check .vcl syntax at compile time (e.g. before nixops deployment)
|
||||
system.checks = mkIf cfg.enableConfigCheck [
|
||||
system.checks = lib.mkIf cfg.enableConfigCheck [
|
||||
(pkgs.runCommand "check-varnish-syntax" { } ''
|
||||
${cfg.package}/bin/varnishd -C ${commandLine} 2> $out || (cat $out; exit 1)
|
||||
'')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue