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 (lib.mdDoc "Varnish Server");
16 enableConfigCheck = mkEnableOption (lib.mdDoc "checking the config during build time") // { default = true; };
20 default = pkgs.varnish;
21 defaultText = literalExpression "pkgs.varnish";
22 description = lib.mdDoc ''
27 http_address = mkOption {
30 description = lib.mdDoc ''
31 HTTP listen address and port.
37 description = lib.mdDoc ''
38 Verbatim default.vcl configuration.
44 default = "/var/spool/varnish/${config.networking.hostName}";
45 defaultText = literalExpression ''"/var/spool/varnish/''${config.networking.hostName}"'';
46 description = lib.mdDoc ''
47 Directory holding all state for Varnish to run.
51 extraModules = mkOption {
52 type = types.listOf types.package;
54 example = literalExpression "[ pkgs.varnishPackages.geoip ]";
55 description = lib.mdDoc ''
56 Varnish modules (except 'std').
60 extraCommandLine = mkOption {
63 example = "-s malloc,256M";
64 description = lib.mdDoc ''
65 Command line switches for varnishd (run 'varnishd -?' to get list of options)
72 config = mkIf cfg.enable {
74 systemd.services.varnish = {
75 description = "Varnish";
76 wantedBy = [ "multi-user.target" ];
77 after = [ "network.target" ];
79 mkdir -p ${cfg.stateDir}
80 chown -R varnish:varnish ${cfg.stateDir}
83 rm -rf ${cfg.stateDir}
87 PermissionsStartOnly = true;
88 ExecStart = "${cfg.package}/sbin/varnishd -a ${cfg.http_address} -n ${cfg.stateDir} -F ${cfg.extraCommandLine} ${commandLine}";
93 AmbientCapabilities = "cap_net_bind_service";
94 NoNewPrivileges = true;
99 environment.systemPackages = [ cfg.package ];
101 # check .vcl syntax at compile time (e.g. before nixops deployment)
102 system.checks = mkIf cfg.enableConfigCheck [
103 (pkgs.runCommand "check-varnish-syntax" {} ''
104 ${cfg.package}/bin/varnishd -C ${commandLine} 2> $out || (cat $out; exit 1)
108 users.users.varnish = {
110 uid = config.ids.uids.varnish;
113 users.groups.varnish.gid = config.ids.uids.varnish;