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