about summary refs log tree commit diff
path: root/nixos/modules/services/web-servers/varnish/default.nix
blob: 364f6c68faca691989d82f90dae1ca83500e0fe7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{ config, lib, pkgs, ...}:
let
  cfg = config.services.varnish;

in
with lib;
{
  options = {
    services.varnish = {
      enable = mkOption {
        default = false;
        description = "
          Enable the Varnish Server.
        ";
      };

      http_address = mkOption {
        default = "*:6081";
        description = "
          HTTP listen address and port.
        ";
      };

      config = mkOption {
        description = "
          Verbatim default.vcl configuration.
        ";
      };

      stateDir = mkOption {
        default = "/var/spool/varnish";
        description = "
          Directory holding all state for Varnish to run.
        ";
      };
    };

  };

  config = mkIf cfg.enable {

    systemd.services.varnish = {
      description = "Varnish";
      wantedBy = [ "multi-user.target" ];
      preStart = ''
        mkdir -p ${cfg.stateDir}
        chown -R varnish:varnish ${cfg.stateDir}
      '';
      path = [ pkgs.gcc ];
      serviceConfig.ExecStart = "${pkgs.varnish}/sbin/varnishd -a ${cfg.http_address} -f ${pkgs.writeText "default.vcl" cfg.config} -n ${cfg.stateDir} -u varnish";
      serviceConfig.Type = "forking";
    };

    environment.systemPackages = [ pkgs.varnish ];

    users.extraUsers.varnish = {
      group = "varnish";
      uid = config.ids.uids.varnish;
    };

    users.extraGroups.varnish.gid = config.ids.uids.varnish;
  };
}