1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 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;
9
10in {
11 ###### interface
12 options = {
13 services.rabbitmq = {
14
15 enable = mkOption {
16 default = false;
17 description = ''
18 Whether to enable the RabbitMQ server, an Advanced Message
19 Queuing Protocol (AMQP) broker.
20 '';
21 };
22
23 listenAddress = mkOption {
24 default = "127.0.0.1";
25 example = "";
26 description = ''
27 IP address on which RabbitMQ will listen for AMQP
28 connections. Set to the empty string to listen on all
29 interfaces. Note that RabbitMQ creates a user named
30 <literal>guest</literal> with password
31 <literal>guest</literal> by default, so you should delete
32 this user if you intend to allow external access.
33 '';
34 type = types.str;
35 };
36
37 port = mkOption {
38 default = 5672;
39 description = ''
40 Port on which RabbitMQ will listen for AMQP connections.
41 '';
42 type = types.int;
43 };
44
45 dataDir = mkOption {
46 type = types.path;
47 default = "/var/lib/rabbitmq";
48 description = ''
49 Data directory for rabbitmq.
50 '';
51 };
52
53 cookie = mkOption {
54 default = "";
55 type = types.str;
56 description = ''
57 Erlang cookie is a string of arbitrary length which must
58 be the same for several nodes to be allowed to communicate.
59 Leave empty to generate automatically.
60 '';
61 };
62
63 config = mkOption {
64 default = "";
65 type = types.str;
66 description = ''
67 Verbatim configuration file contents.
68 See http://www.rabbitmq.com/configure.html
69 '';
70 };
71
72 plugins = mkOption {
73 default = [];
74 type = types.listOf types.str;
75 description = "The names of plugins to enable";
76 };
77 };
78 };
79
80
81 ###### implementation
82 config = mkIf cfg.enable {
83
84 environment.systemPackages = [ pkgs.rabbitmq_server ];
85
86 users.extraUsers.rabbitmq = {
87 description = "RabbitMQ server user";
88 home = "${cfg.dataDir}";
89 createHome = true;
90 group = "rabbitmq";
91 uid = config.ids.uids.rabbitmq;
92 };
93
94 users.extraGroups.rabbitmq.gid = config.ids.gids.rabbitmq;
95
96 systemd.services.rabbitmq = {
97 description = "RabbitMQ Server";
98
99 wantedBy = [ "multi-user.target" ];
100 after = [ "network.target" ];
101
102 path = [ pkgs.rabbitmq_server pkgs.procps ];
103
104 environment = {
105 RABBITMQ_MNESIA_BASE = "${cfg.dataDir}/mnesia";
106 RABBITMQ_NODE_IP_ADDRESS = cfg.listenAddress;
107 RABBITMQ_NODE_PORT = toString cfg.port;
108 RABBITMQ_SERVER_START_ARGS = "-rabbit error_logger tty -rabbit sasl_error_logger false";
109 RABBITMQ_PID_FILE = "${cfg.dataDir}/pid";
110 SYS_PREFIX = "";
111 RABBITMQ_ENABLED_PLUGINS_FILE = pkgs.writeText "enabled_plugins" ''
112 [ ${concatStringsSep "," cfg.plugins} ].
113 '';
114 } // optionalAttrs (cfg.config != "") { RABBITMQ_CONFIG_FILE = config_file_wo_suffix; };
115
116 serviceConfig = {
117 ExecStart = "${pkgs.rabbitmq_server}/sbin/rabbitmq-server";
118 ExecStop = "${pkgs.rabbitmq_server}/sbin/rabbitmqctl stop";
119 User = "rabbitmq";
120 Group = "rabbitmq";
121 WorkingDirectory = cfg.dataDir;
122 };
123
124 postStart = ''
125 rabbitmqctl wait ${cfg.dataDir}/pid
126 '';
127
128 preStart = ''
129 ${optionalString (cfg.cookie != "") ''
130 echo -n ${cfg.cookie} > ${cfg.dataDir}/.erlang.cookie
131 chmod 400 ${cfg.dataDir}/.erlang.cookie
132 ''}
133 '';
134 };
135
136 };
137
138}