1{ config, lib, pkgs, ... }:
2
3with pkgs;
4with lib;
5
6let
7
8 cfg = config.services.activemq;
9
10 activemqBroker = stdenv.mkDerivation {
11 name = "activemq-broker";
12 phases = [ "installPhase" ];
13 buildInputs = [ jdk ];
14 installPhase = ''
15 mkdir -p $out/lib
16 source ${activemq}/lib/classpath.env
17 export CLASSPATH
18 ln -s "${./ActiveMQBroker.java}" ActiveMQBroker.java
19 javac -d $out/lib ActiveMQBroker.java
20 '';
21 };
22
23in {
24
25 options = {
26 services.activemq = {
27 enable = mkOption {
28 type = types.bool;
29 default = false;
30 description = ''
31 Enable the Apache ActiveMQ message broker service.
32 '';
33 };
34 configurationDir = mkOption {
35 default = "${activemq}/conf";
36 description = ''
37 The base directory for ActiveMQ's configuration.
38 By default, this directory is searched for a file named activemq.xml,
39 which should contain the configuration for the broker service.
40 '';
41 };
42 configurationURI = mkOption {
43 type = types.string;
44 default = "xbean:activemq.xml";
45 description = ''
46 The URI that is passed along to the BrokerFactory to
47 set up the configuration of the ActiveMQ broker service.
48 You should not need to change this. For custom configuration,
49 set the <literal>configurationDir</literal> instead, and create
50 an activemq.xml configuration file in it.
51 '';
52 };
53 baseDir = mkOption {
54 type = types.string;
55 default = "/var/activemq";
56 description = ''
57 The base directory where ActiveMQ stores its persistent data and logs.
58 This will be overridden if you set "activemq.base" and "activemq.data"
59 in the <literal>javaProperties</literal> option. You can also override
60 this in activemq.xml.
61 '';
62 };
63 javaProperties = mkOption {
64 type = types.attrs;
65 default = { };
66 example = {
67 "java.net.preferIPv4Stack" = "true";
68 };
69 apply = attrs: {
70 "activemq.base" = "${cfg.baseDir}";
71 "activemq.data" = "${cfg.baseDir}/data";
72 "activemq.conf" = "${cfg.configurationDir}";
73 "activemq.home" = "${activemq}";
74 } // attrs;
75 description = ''
76 Specifies Java properties that are sent to the ActiveMQ
77 broker service with the "-D" option. You can set properties
78 here to change the behaviour and configuration of the broker.
79 All essential properties that are not set here are automatically
80 given reasonable defaults.
81 '';
82 };
83 extraJavaOptions = mkOption {
84 type = types.string;
85 default = "";
86 example = "-Xmx2G -Xms2G -XX:MaxPermSize=512M";
87 description = ''
88 Add extra options here that you want to be sent to the
89 Java runtime when the broker service is started.
90 '';
91 };
92 };
93 };
94
95 config = mkIf cfg.enable {
96 users.extraUsers.activemq = {
97 description = "ActiveMQ server user";
98 group = "activemq";
99 uid = config.ids.uids.activemq;
100 };
101
102 users.extraGroups.activemq.gid = config.ids.gids.activemq;
103
104 systemd.services.activemq_init = {
105 wantedBy = [ "activemq.service" ];
106 partOf = [ "activemq.service" ];
107 before = [ "activemq.service" ];
108 serviceConfig.Type = "oneshot";
109 script = ''
110 mkdir -p "${cfg.javaProperties."activemq.data"}"
111 chown -R activemq "${cfg.javaProperties."activemq.data"}"
112 '';
113 };
114
115 systemd.services.activemq = {
116 wantedBy = [ "multi-user.target" ];
117 after = [ "network.target" ];
118 path = [ jre ];
119 serviceConfig.User = "activemq";
120 script = ''
121 source ${activemq}/lib/classpath.env
122 export CLASSPATH=${activemqBroker}/lib:${cfg.configurationDir}:$CLASSPATH
123 exec java \
124 ${concatStringsSep " \\\n" (mapAttrsToList (name: value: "-D${name}=${value}") cfg.javaProperties)} \
125 ${cfg.extraJavaOptions} ActiveMQBroker "${cfg.configurationURI}"
126 '';
127 };
128
129 };
130
131}