1
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-06-20 08:29:20 +03:00
nixpkgs/nixos/modules/services/web-servers/varnish/default.nix

124 lines
3.3 KiB
Nix
Raw Normal View History

{
config,
lib,
pkgs,
...
}:
with lib;
2013-05-12 20:32:25 +02:00
let
cfg = config.services.varnish;
commandLine =
"-f ${pkgs.writeText "default.vcl" cfg.config}"
+
optionalString (cfg.extraModules != [ ])
" -p vmod_path='${
makeSearchPathOutput "lib" "lib/varnish/vmods" ([ cfg.package ] ++ cfg.extraModules)
}' -r vmod_path";
2013-05-12 20:32:25 +02:00
in
{
options = {
services.varnish = {
enable = mkEnableOption "Varnish Server";
2013-05-12 20:32:25 +02:00
enableConfigCheck = mkEnableOption "checking the config during build time" // {
default = true;
};
2021-10-06 22:00:36 +02:00
package = mkPackageOption pkgs "varnish" { };
http_address = mkOption {
type = types.str;
default = "*:6081";
description = ''
HTTP listen address and port.
'';
};
2013-05-12 20:32:25 +02:00
config = mkOption {
type = types.lines;
description = ''
2013-05-12 20:32:25 +02:00
Verbatim default.vcl configuration.
'';
2013-05-12 20:32:25 +02:00
};
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.
'';
2013-05-12 20:32:25 +02:00
};
extraModules = mkOption {
type = types.listOf types.package;
default = [ ];
example = literalExpression "[ pkgs.varnishPackages.geoip ]";
description = ''
Varnish modules (except 'std').
'';
};
extraCommandLine = mkOption {
type = types.str;
default = "";
example = "-s malloc,256M";
description = ''
Command line switches for varnishd (run 'varnishd -?' to get list of options)
'';
};
2013-05-12 20:32:25 +02:00
};
};
config = mkIf cfg.enable {
systemd.services.varnish = {
description = "Varnish";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
preStart = mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
2013-05-12 20:32:25 +02:00
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}";
Restart = "always";
RestartSec = "5s";
User = "varnish";
Group = "varnish";
RuntimeDirectory = mkIf (lib.hasPrefix "/run/" cfg.stateDir) (
lib.removePrefix "/run/" cfg.stateDir
);
AmbientCapabilities = "cap_net_bind_service";
NoNewPrivileges = true;
LimitNOFILE = 131072;
};
2013-05-12 20:32:25 +02:00
};
environment.systemPackages = [ cfg.package ];
2013-05-12 20:32:25 +02:00
# check .vcl syntax at compile time (e.g. before nixops deployment)
system.checks = mkIf cfg.enableConfigCheck [
(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)
'')
];
users.users.varnish = {
2013-05-12 20:32:25 +02:00
group = "varnish";
uid = config.ids.uids.varnish;
2013-05-12 20:32:25 +02:00
};
users.groups.varnish.gid = config.ids.uids.varnish;
2013-05-12 20:32:25 +02:00
};
}