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