summary refs log tree commit diff
path: root/nixos/modules/services/networking/haproxy.nix
blob: c2e2c2d7a4181e1ac3308394912fa54af86bd63a (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{ config, lib, pkgs, ... }:
let
  cfg = config.services.haproxy;
  haproxyCfg = pkgs.writeText "haproxy.conf" cfg.config;
in
with lib;
{
  options = {
    services.haproxy = {

      enable = mkOption {
        default = false;
        description = "
          Enable the HAProxy.
        ";
      };

      config = mkOption {
        default =
          ''
          global
            log 127.0.0.1 local6
            maxconn  24000
            daemon
            nbproc 1

          defaults
            mode http
            option httpclose

            # Remove requests from the queue if people press stop button
            option abortonclose

            # Try to connect this many times on failure
            retries 3

            # If a client is bound to a particular backend but it goes down,
            # send them to a different one
            option redispatch

            monitor-uri /haproxy-ping

            timeout connect 7s
            timeout queue   300s
            timeout client  300s
            timeout server  300s

            # Enable status page at this URL, on the port HAProxy is bound to
            stats enable
            stats uri /haproxy-status
            stats refresh 5s
            stats realm Haproxy statistics
          '';
        description = "
          Default configuration.
        ";
      };

    };

  };

  config = mkIf cfg.enable {

    systemd.services.haproxy = {
      description = "HAProxy";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        Type = "forking";
        PIDFile = "/var/run/haproxy.pid";
        ExecStartPre = "${pkgs.haproxy}/sbin/haproxy -c -q -f ${haproxyCfg}";
        ExecStart = "${pkgs.haproxy}/sbin/haproxy -D -f ${haproxyCfg} -p /var/run/haproxy.pid";
        ExecReload = "-${pkgs.bash}/bin/bash -c \"exec ${pkgs.haproxy}/sbin/haproxy -D -f ${haproxyCfg} -p /var/run/haproxy.pid -sf $MAINPID\"";
      };
    };

    environment.systemPackages = [ pkgs.haproxy ];

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

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