@@ -4,14 +4,18 @@ with lib;
44
55let
66 cfg = config.services.rabbitmq;
7− config_file = pkgs.writeText "rabbitmq.config" cfg.config;
8− config_file_wo_suffix = builtins.substring 0 ((builtins.stringLength config_file) - 7) config_file;
7+
8+ inherit (builtins) concatStringsSep;
9+
10+ config_file_content = lib.generators.toKeyValue {} cfg.configItems;
11+ config_file = pkgs.writeText "rabbitmq.conf" config_file_content;
12+
13+ advanced_config_file = pkgs.writeText "advanced.config" cfg.config;
914
1015in {
1116 ###### interface
1217 options = {
1318 services.rabbitmq = {
14−
1519 enable = mkOption {
1620 default = false;
1721 description = ''
@@ -20,6 +24,15 @@ in {
2024 '';
2125 };
2226
27+ package = mkOption {
28+ default = pkgs.rabbitmq-server;
29+ type = types.package;
30+ defaultText = "pkgs.rabbitmq-server";
31+ description = ''
32+ Which rabbitmq package to use.
33+ '';
34+ };
35+
2336 listenAddress = mkOption {
2437 default = "127.0.0.1";
2538 example = "";
@@ -30,6 +43,10 @@ in {
3043 <literal>guest</literal> with password
3144 <literal>guest</literal> by default, so you should delete
3245 this user if you intend to allow external access.
46+
47+ Together with 'port' setting it's mostly an alias for
48+ configItems."listeners.tcp.1" and it's left for backwards
49+ compatibility with previous version of this module.
3350 '';
3451 type = types.str;
3552 };
@@ -60,11 +77,29 @@ in {
6077 '';
6178 };
6279
80+ configItems = mkOption {
81+ default = {};
82+ type = types.attrsOf types.str;
83+ example = ''
84+ {
85+ "auth_backends.1.authn" = "rabbit_auth_backend_ldap";
86+ "auth_backends.1.authz" = "rabbit_auth_backend_internal";
87+ }
88+ '';
89+ description = ''
90+ New style config options.
91+
92+ See http://www.rabbitmq.com/configure.html
93+ '';
94+ };
95+
6396 config = mkOption {
6497 default = "";
6598 type = types.str;
6699 description = ''
67− Verbatim configuration file contents.
100+ Verbatim advanced configuration file contents.
101+ Prefered way is to use configItems.
102+
68103 See http://www.rabbitmq.com/configure.html
69104 '';
70105 };
@@ -74,6 +109,12 @@ in {
74109 type = types.listOf types.str;
75110 description = "The names of plugins to enable";
76111 };
112+
113+ pluginDirs = mkOption {
114+ default = [];
115+ type = types.listOf types.path;
116+ description = "The list of directories containing external plugins";
117+ };
77118 };
78119 };
79120
@@ -81,7 +122,10 @@ in {
81122 ###### implementation
82123 config = mkIf cfg.enable {
83124
84− environment.systemPackages = [ pkgs.rabbitmq_server ];
125+ # This is needed so we will have 'rabbitmqctl' in our PATH
126+ environment.systemPackages = [ cfg.package ];
127+
128+ services.epmd.enable = true;
85129
86130 users.users.rabbitmq = {
87131 description = "RabbitMQ server user";
@@ -93,44 +137,54 @@ in {
93137
94138 users.groups.rabbitmq.gid = config.ids.gids.rabbitmq;
95139
140+ services.rabbitmq.configItems = {
141+ "listeners.tcp.1" = mkDefault "${cfg.listenAddress}:${toString cfg.port}";
142+ };
143+
96144 systemd.services.rabbitmq = {
97145 description = "RabbitMQ Server";
98146
99147 wantedBy = [ "multi-user.target" ];
100− after = [ "network.target" ];
148+ after = [ "network.target" "epmd.socket" ];
149+ wants = [ "network.target" "epmd.socket" ];
101150
102− path = [ pkgs.rabbitmq_server pkgs.procps ];
151+ path = [ cfg.package pkgs.procps ];
103152
104153 environment = {
105154 RABBITMQ_MNESIA_BASE = "${cfg.dataDir}/mnesia";
106− RABBITMQ_NODE_IP_ADDRESS = cfg.listenAddress;
107− RABBITMQ_NODE_PORT = toString cfg.port;
108155 RABBITMQ_LOGS = "-";
109− RABBITMQ_SASL_LOGS = "-";
110− RABBITMQ_PID_FILE = "${cfg.dataDir}/pid";
111156 SYS_PREFIX = "";
157+ RABBITMQ_CONFIG_FILE = config_file;
158+ RABBITMQ_PLUGINS_DIR = concatStringsSep ":" cfg.pluginDirs;
112159 RABBITMQ_ENABLED_PLUGINS_FILE = pkgs.writeText "enabled_plugins" ''
113160 [ ${concatStringsSep "," cfg.plugins} ].
114161 '';
115− } // optionalAttrs (cfg.config != "") { RABBITMQ_CONFIG_FILE = config_file_wo_suffix; };
162+ } // optionalAttrs (cfg.config != "") { RABBITMQ_ADVANCED_CONFIG_FILE = advanced_config_file; };
116163
117164 serviceConfig = {
118− ExecStart = "${pkgs.rabbitmq_server}/sbin/rabbitmq-server";
119− ExecStop = "${pkgs.rabbitmq_server}/sbin/rabbitmqctl stop";
165+ PermissionsStartOnly = true; # preStart must be run as root
166+ ExecStart = "${cfg.package}/sbin/rabbitmq-server";
167+ ExecStop = "${cfg.package}/sbin/rabbitmqctl shutdown";
120168 User = "rabbitmq";
121169 Group = "rabbitmq";
122170 WorkingDirectory = cfg.dataDir;
171+ Type = "notify";
172+ NotifyAccess = "all";
173+ UMask = "0027";
174+ LimitNOFILE = "100000";
175+ Restart = "on-failure";
176+ RestartSec = "10";
177+ TimeoutStartSec = "3600";
123178 };
124179
125− postStart = ''
126− rabbitmqctl wait ${cfg.dataDir}/pid
127− '';
128−
129180 preStart = ''
130181 ${optionalString (cfg.cookie != "") ''
131182 echo -n ${cfg.cookie} > ${cfg.dataDir}/.erlang.cookie
183+ chown rabbitmq:rabbitmq ${cfg.dataDir}/.erlang.cookie
132184 chmod 600 ${cfg.dataDir}/.erlang.cookie
133185 ''}
186+ mkdir -p /var/log/rabbitmq
187+ chown rabbitmq:rabbitmq /var/log/rabbitmq
134188 '';
135189 };
136190