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