2024-12-10 20:26:33 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2017-11-26 08:13:09 +00:00
|
|
|
|
2013-05-12 20:32:25 +02:00
|
|
|
let
|
|
|
|
cfg = config.services.varnish;
|
2013-10-04 00:24:59 +02:00
|
|
|
|
2025-01-14 16:44:08 +01:00
|
|
|
# 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";
|
|
|
|
|
2024-12-10 20:26:33 +01:00
|
|
|
commandLine =
|
|
|
|
"-f ${pkgs.writeText "default.vcl" cfg.config}"
|
|
|
|
+
|
2025-01-16 18:47:14 +01:00
|
|
|
lib.optionalString (cfg.extraModules != [ ])
|
2024-12-10 20:26:33 +01:00
|
|
|
" -p vmod_path='${
|
2025-01-16 18:47:14 +01:00
|
|
|
lib.makeSearchPathOutput "lib" "lib/varnish/vmods" ([ cfg.package ] ++ cfg.extraModules)
|
2024-12-10 20:26:33 +01:00
|
|
|
}' -r vmod_path";
|
2013-05-12 20:32:25 +02:00
|
|
|
in
|
|
|
|
{
|
2025-01-14 16:44:08 +01:00
|
|
|
imports = [
|
|
|
|
(lib.mkRemovedOptionModule [
|
|
|
|
"services"
|
|
|
|
"varnish"
|
|
|
|
"stateDir"
|
|
|
|
] "The `stateDir` option never was functional or useful. varnish uses compile-time settings.")
|
|
|
|
];
|
|
|
|
|
2013-05-12 20:32:25 +02:00
|
|
|
options = {
|
|
|
|
services.varnish = {
|
2025-01-16 18:47:14 +01:00
|
|
|
enable = lib.mkEnableOption "Varnish Server";
|
2013-05-12 20:32:25 +02:00
|
|
|
|
2025-01-16 18:47:14 +01:00
|
|
|
enableConfigCheck = lib.mkEnableOption "checking the config during build time" // {
|
2024-12-10 20:26:33 +01:00
|
|
|
default = true;
|
|
|
|
};
|
2021-10-06 22:00:36 +02:00
|
|
|
|
2025-01-16 18:47:14 +01:00
|
|
|
package = lib.mkPackageOption pkgs "varnish" { };
|
2018-03-20 06:19:32 +00:00
|
|
|
|
2025-01-16 18:47:14 +01:00
|
|
|
http_address = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2013-10-04 00:24:59 +02:00
|
|
|
default = "*:6081";
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2013-10-04 00:24:59 +02:00
|
|
|
HTTP listen address and port.
|
2022-08-14 05:16:55 +02:00
|
|
|
'';
|
2013-10-04 00:24:59 +02:00
|
|
|
};
|
|
|
|
|
2025-01-16 18:47:14 +01:00
|
|
|
config = lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2013-05-12 20:32:25 +02:00
|
|
|
Verbatim default.vcl configuration.
|
2022-08-14 05:16:55 +02:00
|
|
|
'';
|
2013-05-12 20:32:25 +02:00
|
|
|
};
|
|
|
|
|
2025-01-16 18:47:14 +01:00
|
|
|
extraModules = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.package;
|
2024-12-10 20:26:33 +01:00
|
|
|
default = [ ];
|
2025-01-16 18:47:14 +01:00
|
|
|
example = lib.literalExpression "[ pkgs.varnishPackages.geoip ]";
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2017-07-15 22:17:53 +00:00
|
|
|
Varnish modules (except 'std').
|
2022-08-14 05:16:55 +02:00
|
|
|
'';
|
2017-07-15 22:17:53 +00:00
|
|
|
};
|
|
|
|
|
2025-01-16 18:47:14 +01:00
|
|
|
extraCommandLine = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2017-07-15 22:17:53 +00:00
|
|
|
default = "";
|
|
|
|
example = "-s malloc,256M";
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2017-07-15 22:17:53 +00:00
|
|
|
Command line switches for varnishd (run 'varnishd -?' to get list of options)
|
2022-08-14 05:16:55 +02:00
|
|
|
'';
|
2017-07-15 22:17:53 +00:00
|
|
|
};
|
2013-05-12 20:32:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2025-01-16 18:47:14 +01:00
|
|
|
config = lib.mkIf cfg.enable {
|
2013-05-12 20:32:25 +02:00
|
|
|
systemd.services.varnish = {
|
|
|
|
description = "Varnish";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2017-07-15 22:17:53 +00:00
|
|
|
after = [ "network.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
PermissionsStartOnly = true;
|
2025-01-14 16:44:08 +01:00
|
|
|
ExecStart = "${cfg.package}/sbin/varnishd -a ${cfg.http_address} -n ${stateDir} -F ${cfg.extraCommandLine} ${commandLine}";
|
2017-07-15 22:17:53 +00:00
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "5s";
|
|
|
|
User = "varnish";
|
|
|
|
Group = "varnish";
|
2025-01-14 16:44:08 +01:00
|
|
|
RuntimeDirectory = lib.removePrefix "/var/run/" stateDir;
|
2017-07-15 22:17:53 +00:00
|
|
|
AmbientCapabilities = "cap_net_bind_service";
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
LimitNOFILE = 131072;
|
|
|
|
};
|
2013-05-12 20:32:25 +02:00
|
|
|
};
|
|
|
|
|
2018-03-20 06:19:32 +00:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2013-05-12 20:32:25 +02:00
|
|
|
|
2017-11-26 08:13:09 +00:00
|
|
|
# check .vcl syntax at compile time (e.g. before nixops deployment)
|
2025-01-16 18:47:14 +01:00
|
|
|
system.checks = lib.mkIf cfg.enableConfigCheck [
|
2024-12-10 20:26:33 +01:00
|
|
|
(pkgs.runCommand "check-varnish-syntax" { } ''
|
2021-10-06 22:00:36 +02:00
|
|
|
${cfg.package}/bin/varnishd -C ${commandLine} 2> $out || (cat $out; exit 1)
|
|
|
|
'')
|
2017-11-26 08:13:09 +00:00
|
|
|
];
|
|
|
|
|
2018-06-30 01:58:35 +02:00
|
|
|
users.users.varnish = {
|
2013-05-12 20:32:25 +02:00
|
|
|
group = "varnish";
|
2013-08-23 11:33:24 +02:00
|
|
|
uid = config.ids.uids.varnish;
|
2013-05-12 20:32:25 +02:00
|
|
|
};
|
|
|
|
|
2018-06-30 01:58:35 +02:00
|
|
|
users.groups.varnish.gid = config.ids.uids.varnish;
|
2013-05-12 20:32:25 +02:00
|
|
|
};
|
|
|
|
}
|