@@ -8,18 +8,21 @@ let
88 cassandraConfig = flip recursiveUpdate cfg.extraConfig
99 ({ commitlog_sync = "batch";
1010 commitlog_sync_batch_window_in_ms = 2;
11+ start_native_transport = cfg.allowClients;
12+ cluster_name = cfg.clusterName;
1113 partitioner = "org.apache.cassandra.dht.Murmur3Partitioner";
1214 endpoint_snitch = "SimpleSnitch";
13− seed_provider =
14− [{ class_name = "org.apache.cassandra.locator.SimpleSeedProvider";
15− parameters = [ { seeds = "127.0.0.1"; } ];
16− }];
1715 data_file_directories = [ "${cfg.homeDir}/data" ];
1816 commitlog_directory = "${cfg.homeDir}/commitlog";
1917 saved_caches_directory = "${cfg.homeDir}/saved_caches";
20− } // (if builtins.compareVersions cfg.package.version "3" >= 0
21− then { hints_directory = "${cfg.homeDir}/hints"; }
22− else {})
18+ } // (lib.optionalAttrs (cfg.seedAddresses != []) {
19+ seed_provider = [{
20+ class_name = "org.apache.cassandra.locator.SimpleSeedProvider";
21+ parameters = [ { seeds = concatStringsSep "," cfg.seedAddresses; } ];
22+ }];
23+ }) // (lib.optionalAttrs (lib.versionAtLeast cfg.package.version "3") {
24+ hints_directory = "${cfg.homeDir}/hints";
25+ })
2326 );
2427 cassandraConfigWithAddresses = cassandraConfig //
2528 ( if cfg.listenAddress == null
@@ -39,15 +42,42 @@ let
3942 mkdir -p "$out"
4043
4144 echo "$cassandraYaml" > "$out/cassandra.yaml"
42− ln -s "$cassandraEnvPkg" "$out/cassandra-env.sh"
4345 ln -s "$cassandraLogbackConfig" "$out/logback.xml"
46+
47+ cp "$cassandraEnvPkg" "$out/cassandra-env.sh"
48+
49+ # Delete default JMX Port, otherwise we can't set it using env variable
50+ sed -i '/JMX_PORT="7199"/d' "$out/cassandra-env.sh"
51+
52+ # Delete default password file
53+ sed -i '/-Dcom.sun.management.jmxremote.password.file=\/etc\/cassandra\/jmxremote.password/d' "$out/cassandra-env.sh"
4454 '';
4555 };
56+ defaultJmxRolesFile = builtins.foldl'
57+ (left: right: left + right) ""
58+ (map (role: "${role.username} ${role.password}") cfg.jmxRoles);
59+ fullJvmOptions = cfg.jvmOpts
60+ ++ lib.optionals (cfg.jmxRoles != []) [
61+ "-Dcom.sun.management.jmxremote.authenticate=true"
62+ "-Dcom.sun.management.jmxremote.password.file=${cfg.jmxRolesFile}"
63+ ]
64+ ++ lib.optionals cfg.remoteJmx [
65+ "-Djava.rmi.server.hostname=${cfg.rpcAddress}"
66+ ];
4667in {
4768 options.services.cassandra = {
4869 enable = mkEnableOption ''
4970 Apache Cassandra – Scalable and highly available database.
5071 '';
72+ clusterName = mkOption {
73+ type = types.str;
74+ default = "NixOS Test Cluster";
75+ description = ''
76+ The name of the cluster.
77+ This setting prevents nodes in one logical cluster from joining
78+ another. All nodes in a cluster must have the same value.
79+ '';
80+ };
5181 user = mkOption {
5282 type = types.str;
5383 default = defaultUser;
@@ -162,6 +192,28 @@ in {
162192 XML logback configuration for cassandra
163193 '';
164194 };
195+ seedAddresses = mkOption {
196+ type = types.listOf types.str;
197+ default = [ "127.0.0.1" ];
198+ description = ''
199+ The addresses of hosts designated as contact points in the cluster. A
200+ joining node contacts one of the nodes in the seeds list to learn the
201+ topology of the ring.
202+ Set to 127.0.0.1 for a single node cluster.
203+ '';
204+ };
205+ allowClients = mkOption {
206+ type = types.bool;
207+ default = true;
208+ description = ''
209+ Enables or disables the native transport server (CQL binary protocol).
210+ This server uses the same address as the <literal>rpcAddress</literal>,
211+ but the port it uses is not <literal>rpc_port</literal> but
212+ <literal>native_transport_port</literal>. See the official Cassandra
213+ docs for more information on these variables and set them using
214+ <literal>extraConfig</literal>.
215+ '';
216+ };
165217 extraConfig = mkOption {
166218 type = types.attrs;
167219 default = {};
@@ -178,11 +230,11 @@ in {
178230 example = literalExample "null";
179231 description = ''
180232 Set the interval how often full repairs are run, i.e.
181− `nodetool repair --full` is executed. See
233+ <literal>nodetool repair --full</literal> is executed. See
182234 https://cassandra.apache.org/doc/latest/operating/repair.html
183235 for more information.
184236
185− Set to `null` to disable full repairs.
237+ Set to <literal>null</literal> to disable full repairs.
186238 '';
187239 };
188240 fullRepairOptions = mkOption {
@@ -199,11 +251,11 @@ in {
199251 example = literalExample "null";
200252 description = ''
201253 Set the interval how often incremental repairs are run, i.e.
202− `nodetool repair` is executed. See
254+ <literal>nodetool repair<literal> is executed. See
203255 https://cassandra.apache.org/doc/latest/operating/repair.html
204256 for more information.
205257
206− Set to `null` to disable incremental repairs.
258+ Set to <literal>null</literal> to disable incremental repairs.
207259 '';
208260 };
209261 incrementalRepairOptions = mkOption {
@@ -214,20 +266,135 @@ in {
214266 Options passed through to the incremental repair command.
215267 '';
216268 };
269+ maxHeapSize = mkOption {
270+ type = types.nullOr types.string;
271+ default = null;
272+ example = "4G";
273+ description = ''
274+ Must be left blank or set together with heapNewSize.
275+ If left blank a sensible value for the available amount of RAM and CPU
276+ cores is calculated.
277+
278+ Override to set the amount of memory to allocate to the JVM at
279+ start-up. For production use you may wish to adjust this for your
280+ environment. MAX_HEAP_SIZE is the total amount of memory dedicated
281+ to the Java heap. HEAP_NEWSIZE refers to the size of the young
282+ generation.
283+
284+ The main trade-off for the young generation is that the larger it
285+ is, the longer GC pause times will be. The shorter it is, the more
286+ expensive GC will be (usually).
287+ '';
288+ };
289+ heapNewSize = mkOption {
290+ type = types.nullOr types.string;
291+ default = null;
292+ example = "800M";
293+ description = ''
294+ Must be left blank or set together with heapNewSize.
295+ If left blank a sensible value for the available amount of RAM and CPU
296+ cores is calculated.
297+
298+ Override to set the amount of memory to allocate to the JVM at
299+ start-up. For production use you may wish to adjust this for your
300+ environment. HEAP_NEWSIZE refers to the size of the young
301+ generation.
302+
303+ The main trade-off for the young generation is that the larger it
304+ is, the longer GC pause times will be. The shorter it is, the more
305+ expensive GC will be (usually).
306+
307+ The example HEAP_NEWSIZE assumes a modern 8-core+ machine for decent pause
308+ times. If in doubt, and if you do not particularly want to tweak, go with
309+ 100 MB per physical CPU core.
310+ '';
311+ };
312+ mallocArenaMax = mkOption {
313+ type = types.nullOr types.int;
314+ default = null;
315+ example = 4;
316+ description = ''
317+ Set this to control the amount of arenas per-thread in glibc.
318+ '';
319+ };
320+ remoteJmx = mkOption {
321+ type = types.bool;
322+ default = false;
323+ description = ''
324+ Cassandra ships with JMX accessible *only* from localhost.
325+ To enable remote JMX connections set to true.
326+
327+ Be sure to also enable authentication and/or TLS.
328+ See: https://wiki.apache.org/cassandra/JmxSecurity
329+ '';
330+ };
331+ jmxPort = mkOption {
332+ type = types.int;
333+ default = 7199;
334+ description = ''
335+ Specifies the default port over which Cassandra will be available for
336+ JMX connections.
337+ For security reasons, you should not expose this port to the internet.
338+ Firewall it if needed.
339+ '';
340+ };
341+ jmxRoles = mkOption {
342+ default = [];
343+ description = ''
344+ Roles that are allowed to access the JMX (e.g. nodetool)
345+ BEWARE: The passwords will be stored world readable in the nix-store.
346+ It's recommended to use your own protected file using
347+ <literal>jmxRolesFile</literal>
348+
349+ Doesn't work in versions older than 3.11 because they don't like that
350+ it's world readable.
351+ '';
352+ type = types.listOf (types.submodule {
353+ options = {
354+ username = mkOption {
355+ type = types.string;
356+ description = "Username for JMX";
357+ };
358+ password = mkOption {
359+ type = types.string;
360+ description = "Password for JMX";
361+ };
362+ };
363+ });
364+ };
365+ jmxRolesFile = mkOption {
366+ type = types.nullOr types.path;
367+ default = if (lib.versionAtLeast cfg.package.version "3.11")
368+ then pkgs.writeText "jmx-roles-file" defaultJmxRolesFile
369+ else null;
370+ example = "/var/lib/cassandra/jmx.password";
371+ description = ''
372+ Specify your own jmx roles file.
373+
374+ Make sure the permissions forbid "others" from reading the file if
375+ you're using Cassandra below version 3.11.
376+ '';
377+ };
217378 };
218379
219380 config = mkIf cfg.enable {
220381 assertions =
221− [ { assertion =
222− (cfg.listenAddress == null || cfg.listenInterface == null)
223− && !(cfg.listenAddress == null && cfg.listenInterface == null);
382+ [ { assertion = (cfg.listenAddress == null) != (cfg.listenInterface == null);
224383 message = "You have to set either listenAddress or listenInterface";
225384 }
226− { assertion =
227− (cfg.rpcAddress == null || cfg.rpcInterface == null)
228− && !(cfg.rpcAddress == null && cfg.rpcInterface == null);
385+ { assertion = (cfg.rpcAddress == null) != (cfg.rpcInterface == null);
229386 message = "You have to set either rpcAddress or rpcInterface";
230387 }
388+ { assertion = (cfg.maxHeapSize == null) == (cfg.heapNewSize == null);
389+ message = "If you set either of maxHeapSize or heapNewSize you have to set both";
390+ }
391+ { assertion = cfg.remoteJmx -> cfg.jmxRolesFile != null;
392+ message = ''
393+ If you want JMX available remotely you need to set a password using
394+ <literal>jmxRoles</literal> or <literal>jmxRolesFile</literal> if
395+ using Cassandra older than v3.11.
396+ '';
397+ }
231398 ];
232399 users = mkIf (cfg.user == defaultUser) {
233400 extraUsers."${defaultUser}" =
@@ -245,7 +412,12 @@ in {
245412 after = [ "network.target" ];
246413 environment =
247414 { CASSANDRA_CONF = "${cassandraEtc}";
248− JVM_OPTS = builtins.concatStringsSep " " cfg.jvmOpts;
415+ JVM_OPTS = builtins.concatStringsSep " " fullJvmOptions;
416+ MAX_HEAP_SIZE = toString cfg.maxHeapSize;
417+ HEAP_NEWSIZE = toString cfg.heapNewSize;
418+ MALLOC_ARENA_MAX = toString cfg.mallocArenaMax;
419+ LOCAL_JMX = if cfg.remoteJmx then "no" else "yes";
420+ JMX_PORT = toString cfg.jmxPort;
249421 };
250422 wantedBy = [ "multi-user.target" ];
251423 serviceConfig =