summary refs log tree commit diff
path: root/wrappers/haproxy.nix
blob: b03475c88765330c975c35b99c0fc8177a02babe (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{ config, lib, ... }:

let cfg = config.smalltech.haproxy;

    # The important thing here is `configText`; you might find it useful to
    # read this bit bottom-to-top to understand how it's built.
    mkDirectiveText = directive: "  " + directive;
    mkSectionText = header: directives:
        builtins.concatStringsSep "\n"
            ([header] ++ (map mkDirectiveText directives));
    mkFrontendText = name: directives:
        mkSectionText "frontend ${name}" directives;
    mkBackendText = name: directives:
        mkSectionText "backend ${name}" directives;
    globalText = mkSectionText "global" cfg.global;
    defaultsText = mkSectionText "defaults" cfg.defaults;
    allFrontendTexts =
        builtins.attrValues (builtins.mapAttrs mkFrontendText cfg.frontends);
    allBackendTexts =
        builtins.attrValues (builtins.mapAttrs mkBackendText cfg.backends);
    allSectionTexts = [globalText defaultsText]
                      ++ allFrontendTexts
                      ++ allBackendTexts;
    configText = builtins.concatStringsSep "\n\n" allSectionTexts;

in
{
  imports = [ ];

  options = {
    smalltech.haproxy = lib.mkOption {
      description = ''
        Options used to provide a higher-level way to configure HAProxy, as
        opposed to writing out an entire config file as a string literal.
      '';
      type = lib.types.submodule {
        options = {
          global = lib.mkOption {
            type = lib.types.listOf lib.types.str;
            description = ''
              Directives to go in HAProxy's "global" section. Ordering is
              significant.
            '';
            default = [ ];
            example = [ "log /dev/log local0 info" "maxconn 2048" ];
          };

          defaults = lib.mkOption {
            type = lib.types.listOf lib.types.str;
            description = ''
              Directives to go in HAProxy's "defaults" section. Ordering is
              significant.
            '';
            default = [ ];
            example = [ "mode tcp" "log global" ];
          };

          frontends = lib.mkOption {
            type = lib.types.attrsOf (lib.types.listOf lib.types.str);
            description = ''
              An attribute set of HAProxy frontend definitions; each gives a
              name and a list of directives. Within each frontend, the
              ordering of directives is significant.
            '';
            default = { };
            example = {
              "multi_https" = [
                "bind 127.0.0.1:443 ssl"
                "mode http"
                "use_backend local_https"
              ];
              "multi_http" = [
                "bind 127.0.0.1:80"
                "mode http"
                "use_backend redirect_to_https"
              ];
            };
          };

          backends = lib.mkOption {
            type = lib.types.attrsOf (lib.types.listOf lib.types.str);
            description = ''
              An attribute set of HAProxy backend definitions; each gives a
              name and a list of directives. Within each backend, the
              ordering of directives is significant.
            '';
            default = { };
            example = {
              "local_https" = [
                "server local_https 127.0.0.1:3000 maxconn 256"
              ];
              "redirect_to_https" = [
                "http-request redirect scheme https code 301"
              ];
            };
          };
        };
      };
    };
  };

  config = {
    services.haproxy = {
      config = configText;
    };
  };
}