summary refs log tree commit diff
path: root/nixos/modules/services/databases/cockroachdb.nix
blob: eb061af926219fde270f0fdb784bd89f91df0f22 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.cockroachdb;
  crdb = cfg.package;

  escape    = builtins.replaceStrings ["%"] ["%%"];
  ifNotNull = v: s: optionalString (v != null) s;

  startupCommand = lib.concatStringsSep " "
    [ # Basic startup
      "${crdb}/bin/cockroach start"
      "--logtostderr"
      "--store=/var/lib/cockroachdb"
      (ifNotNull cfg.locality "--locality='${cfg.locality}'")

      # WebUI settings
      "--http-addr='${cfg.http.address}:${toString cfg.http.port}'"

      # Cluster listen address
      "--listen-addr='${cfg.listen.address}:${toString cfg.listen.port}'"

      # Cluster configuration
      (ifNotNull cfg.join "--join=${cfg.join}")

      # Cache and memory settings. Must be escaped.
      "--cache='${escape cfg.cache}'"
      "--max-sql-memory='${escape cfg.maxSqlMemory}'"

      # Certificate/security settings.
      (if cfg.insecure then "--insecure" else "--certs-dir=${cfg.certsDir}")
    ];

    addressOption = descr: defaultPort: {
      address = mkOption {
        type = types.str;
        default = "localhost";
        description = "Address to bind to for ${descr}";
      };

      port = mkOption {
        type = types.port;
        default = defaultPort;
        description = "Port to bind to for ${descr}";
      };
    };
in

{
  options = {
    services.cockroachdb = {
      enable = mkEnableOption "CockroachDB Server";

      listen = addressOption "intra-cluster communication" 26257;

      http = addressOption "http-based Admin UI" 8080;

      locality = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = ''
          An ordered, comma-separated list of key-value pairs that describe the
          topography of the machine. Topography might include country,
          datacenter or rack designations. Data is automatically replicated to
          maximize diversities of each tier. The order of tiers is used to
          determine the priority of the diversity, so the more inclusive
          localities like country should come before less inclusive localities
          like datacenter.  The tiers and order must be the same on all nodes.
          Including more tiers is better than including fewer. For example:

          <literal>
              country=us,region=us-west,datacenter=us-west-1b,rack=12
              country=ca,region=ca-east,datacenter=ca-east-2,rack=4

              planet=earth,province=manitoba,colo=secondary,power=3
          </literal>
        '';
      };

      join = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = "The addresses for connecting the node to a cluster.";
      };

      insecure = mkOption {
        type = types.bool;
        default = false;
        description = "Run in insecure mode.";
      };

      certsDir = mkOption {
        type = types.nullOr types.path;
        default = null;
        description = "The path to the certificate directory.";
      };

      user = mkOption {
        type = types.str;
        default = "cockroachdb";
        description = "User account under which CockroachDB runs";
      };

      group = mkOption {
        type = types.str;
        default = "cockroachdb";
        description = "User account under which CockroachDB runs";
      };

      openPorts = mkOption {
        type = types.bool;
        default = false;
        description = "Open firewall ports for cluster communication by default";
      };

      cache = mkOption {
        type = types.str;
        default = "25%";
        description = ''
          The total size for caches.

          This can be a percentage, expressed with a fraction sign or as a
          decimal-point number, or any bytes-based unit. For example,
          <literal>"25%"</literal>, <literal>"0.25"</literal> both represent
          25% of the available system memory. The values
          <literal>"1000000000"</literal> and <literal>"1GB"</literal> both
          represent 1 gigabyte of memory.

        '';
      };

      maxSqlMemory = mkOption {
        type = types.str;
        default = "25%";
        description = ''
          The maximum in-memory storage capacity available to store temporary
          data for SQL queries.

          This can be a percentage, expressed with a fraction sign or as a
          decimal-point number, or any bytes-based unit. For example,
          <literal>"25%"</literal>, <literal>"0.25"</literal> both represent
          25% of the available system memory. The values
          <literal>"1000000000"</literal> and <literal>"1GB"</literal> both
          represent 1 gigabyte of memory.
        '';
      };

      package = mkOption {
        type = types.package;
        default = pkgs.cockroachdb;
        defaultText = literalExpression "pkgs.cockroachdb";
        description = ''
          The CockroachDB derivation to use for running the service.

          This would primarily be useful to enable Enterprise Edition features
          in your own custom CockroachDB build (Nixpkgs CockroachDB binaries
          only contain open source features and open source code).
        '';
      };
    };
  };

  config = mkIf config.services.cockroachdb.enable {
    assertions = [
      { assertion = !cfg.insecure -> cfg.certsDir != null;
        message = "CockroachDB must have a set of SSL certificates (.certsDir), or run in Insecure Mode (.insecure = true)";
      }
    ];

    environment.systemPackages = [ crdb ];

    users.users = optionalAttrs (cfg.user == "cockroachdb") {
      cockroachdb = {
        description = "CockroachDB Server User";
        uid         = config.ids.uids.cockroachdb;
        group       = cfg.group;
      };
    };

    users.groups = optionalAttrs (cfg.group == "cockroachdb") {
      cockroachdb.gid = config.ids.gids.cockroachdb;
    };

    networking.firewall.allowedTCPPorts = lib.optionals cfg.openPorts
      [ cfg.http.port cfg.listen.port ];

    systemd.services.cockroachdb =
      { description   = "CockroachDB Server";
        documentation = [ "man:cockroach(1)" "https://www.cockroachlabs.com" ];

        after    = [ "network.target" "time-sync.target" ];
        requires = [ "time-sync.target" ];
        wantedBy = [ "multi-user.target" ];

        unitConfig.RequiresMountsFor = "/var/lib/cockroachdb";

        serviceConfig =
          { ExecStart = startupCommand;
            Type = "notify";
            User = cfg.user;
            StateDirectory = "cockroachdb";
            StateDirectoryMode = "0700";

            Restart = "always";

            # A conservative-ish timeout is alright here, because for Type=notify
            # cockroach will send systemd pings during startup to keep it alive
            TimeoutStopSec = 60;
            RestartSec = 10;
          };
      };
  };

  meta.maintainers = with lib.maintainers; [ thoughtpolice ];
}