1 { config, lib, pkgs, ...}:
6 cfg = config.services.varnish;
8 commandLine = "-f ${pkgs.writeText "default.vcl" cfg.config}" +
9 optionalString (cfg.extraModules != []) " -p vmod_path='${makeSearchPathOutput "lib" "lib/varnish/vmods" ([cfg.package] ++ cfg.extraModules)}' -r vmod_path";
14 enable = mkEnableOption "Varnish Server";
16 enableConfigCheck = mkEnableOption "checking the config during build time" // { default = true; };
18 package = mkPackageOption pkgs "varnish" { };
20 http_address = mkOption {
24 HTTP listen address and port.
31 Verbatim default.vcl configuration.
37 default = "/run/varnish/${config.networking.hostName}";
38 defaultText = literalExpression ''"/run/varnish/''${config.networking.hostName}"'';
40 Directory holding all state for Varnish to run. Note that this should be a tmpfs in order to avoid performance issues and crashes.
44 extraModules = mkOption {
45 type = types.listOf types.package;
47 example = literalExpression "[ pkgs.varnishPackages.geoip ]";
49 Varnish modules (except 'std').
53 extraCommandLine = mkOption {
56 example = "-s malloc,256M";
58 Command line switches for varnishd (run 'varnishd -?' to get list of options)
65 config = mkIf cfg.enable {
67 systemd.services.varnish = {
68 description = "Varnish";
69 wantedBy = [ "multi-user.target" ];
70 after = [ "network.target" ];
71 preStart = mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
72 mkdir -p ${cfg.stateDir}
73 chown -R varnish:varnish ${cfg.stateDir}
75 postStop = mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
76 rm -rf ${cfg.stateDir}
80 PermissionsStartOnly = true;
81 ExecStart = "${cfg.package}/sbin/varnishd -a ${cfg.http_address} -n ${cfg.stateDir} -F ${cfg.extraCommandLine} ${commandLine}";
86 RuntimeDirectory = mkIf (lib.hasPrefix "/run/" cfg.stateDir) (lib.removePrefix "/run/" cfg.stateDir);
87 AmbientCapabilities = "cap_net_bind_service";
88 NoNewPrivileges = true;
93 environment.systemPackages = [ cfg.package ];
95 # check .vcl syntax at compile time (e.g. before nixops deployment)
96 system.checks = mkIf cfg.enableConfigCheck [
97 (pkgs.runCommand "check-varnish-syntax" {} ''
98 ${cfg.package}/bin/varnishd -C ${commandLine} 2> $out || (cat $out; exit 1)
102 users.users.varnish = {
104 uid = config.ids.uids.varnish;
107 users.groups.varnish.gid = config.ids.uids.varnish;