You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

211 lines
6.5 KiB

  1. { config, lib, pkgs, ... }:
  2. with lib;
  3. let
  4. cfg = config.services.rabbitmq;
  5. inherit (builtins) concatStringsSep;
  6. config_file_content = lib.generators.toKeyValue {} cfg.configItems;
  7. config_file = pkgs.writeText "rabbitmq.conf" config_file_content;
  8. advanced_config_file = pkgs.writeText "advanced.config" cfg.config;
  9. in {
  10. ###### interface
  11. options = {
  12. services.rabbitmq = {
  13. enable = mkOption {
  14. default = false;
  15. description = ''
  16. Whether to enable the RabbitMQ server, an Advanced Message
  17. Queuing Protocol (AMQP) broker.
  18. '';
  19. };
  20. package = mkOption {
  21. default = pkgs.rabbitmq-server;
  22. type = types.package;
  23. defaultText = "pkgs.rabbitmq-server";
  24. description = ''
  25. Which rabbitmq package to use.
  26. '';
  27. };
  28. listenAddress = mkOption {
  29. default = "127.0.0.1";
  30. example = "";
  31. description = ''
  32. IP address on which RabbitMQ will listen for AMQP
  33. connections. Set to the empty string to listen on all
  34. interfaces. Note that RabbitMQ creates a user named
  35. <literal>guest</literal> with password
  36. <literal>guest</literal> by default, so you should delete
  37. this user if you intend to allow external access.
  38. Together with 'port' setting it's mostly an alias for
  39. configItems."listeners.tcp.1" and it's left for backwards
  40. compatibility with previous version of this module.
  41. '';
  42. type = types.str;
  43. };
  44. port = mkOption {
  45. default = 5672;
  46. description = ''
  47. Port on which RabbitMQ will listen for AMQP connections.
  48. '';
  49. type = types.int;
  50. };
  51. dataDir = mkOption {
  52. type = types.path;
  53. default = "/var/lib/rabbitmq";
  54. description = ''
  55. Data directory for rabbitmq.
  56. '';
  57. };
  58. cookie = mkOption {
  59. default = "";
  60. type = types.str;
  61. description = ''
  62. Erlang cookie is a string of arbitrary length which must
  63. be the same for several nodes to be allowed to communicate.
  64. Leave empty to generate automatically.
  65. '';
  66. };
  67. configItems = mkOption {
  68. default = {};
  69. type = types.attrsOf types.str;
  70. example = literalExample ''
  71. {
  72. "auth_backends.1.authn" = "rabbit_auth_backend_ldap";
  73. "auth_backends.1.authz" = "rabbit_auth_backend_internal";
  74. }
  75. '';
  76. description = ''
  77. Configuration options in RabbitMQ's new config file format,
  78. which is a simple key-value format that can not express nested
  79. data structures. This is known as the <literal>rabbitmq.conf</literal> file,
  80. although outside NixOS that filename may have Erlang syntax, particularly
  81. prior to RabbitMQ 3.7.0.
  82. If you do need to express nested data structures, you can use
  83. <literal>config</literal> option. Configuration from <literal>config</literal>
  84. will be merged into these options by RabbitMQ at runtime to
  85. form the final configuration.
  86. See https://www.rabbitmq.com/configure.html#config-items
  87. For the distinct formats, see https://www.rabbitmq.com/configure.html#config-file-formats
  88. '';
  89. };
  90. config = mkOption {
  91. default = "";
  92. type = types.str;
  93. description = ''
  94. Verbatim advanced configuration file contents using the Erlang syntax.
  95. This is also known as the <literal>advanced.config</literal> file or the old config format.
  96. <literal>configItems</literal> is preferred whenever possible. However, nested
  97. data structures can only be expressed properly using the <literal>config</literal> option.
  98. The contents of this option will be merged into the <literal>configItems</literal>
  99. by RabbitMQ at runtime to form the final configuration.
  100. See the second table on https://www.rabbitmq.com/configure.html#config-items
  101. For the distinct formats, see https://www.rabbitmq.com/configure.html#config-file-formats
  102. '';
  103. };
  104. plugins = mkOption {
  105. default = [];
  106. type = types.listOf types.str;
  107. description = "The names of plugins to enable";
  108. };
  109. pluginDirs = mkOption {
  110. default = [];
  111. type = types.listOf types.path;
  112. description = "The list of directories containing external plugins";
  113. };
  114. };
  115. };
  116. ###### implementation
  117. config = mkIf cfg.enable {
  118. # This is needed so we will have 'rabbitmqctl' in our PATH
  119. environment.systemPackages = [ cfg.package ];
  120. services.epmd.enable = true;
  121. users.users.rabbitmq = {
  122. description = "RabbitMQ server user";
  123. home = "${cfg.dataDir}";
  124. createHome = true;
  125. group = "rabbitmq";
  126. uid = config.ids.uids.rabbitmq;
  127. };
  128. users.groups.rabbitmq.gid = config.ids.gids.rabbitmq;
  129. services.rabbitmq.configItems = {
  130. "listeners.tcp.1" = mkDefault "${cfg.listenAddress}:${toString cfg.port}";
  131. };
  132. systemd.services.rabbitmq = {
  133. description = "RabbitMQ Server";
  134. wantedBy = [ "multi-user.target" ];
  135. after = [ "network.target" "epmd.socket" ];
  136. wants = [ "network.target" "epmd.socket" ];
  137. path = [
  138. cfg.package
  139. pkgs.coreutils # mkdir/chown/chmod for preStart
  140. ];
  141. environment = {
  142. RABBITMQ_MNESIA_BASE = "${cfg.dataDir}/mnesia";
  143. RABBITMQ_LOGS = "-";
  144. SYS_PREFIX = "";
  145. RABBITMQ_CONFIG_FILE = config_file;
  146. RABBITMQ_PLUGINS_DIR = concatStringsSep ":" cfg.pluginDirs;
  147. RABBITMQ_ENABLED_PLUGINS_FILE = pkgs.writeText "enabled_plugins" ''
  148. [ ${concatStringsSep "," cfg.plugins} ].
  149. '';
  150. } // optionalAttrs (cfg.config != "") { RABBITMQ_ADVANCED_CONFIG_FILE = advanced_config_file; };
  151. serviceConfig = {
  152. ExecStart = "${cfg.package}/sbin/rabbitmq-server";
  153. ExecStop = "${cfg.package}/sbin/rabbitmqctl shutdown";
  154. User = "rabbitmq";
  155. Group = "rabbitmq";
  156. LogsDirectory = "rabbitmq";
  157. WorkingDirectory = cfg.dataDir;
  158. Type = "notify";
  159. NotifyAccess = "all";
  160. UMask = "0027";
  161. LimitNOFILE = "100000";
  162. Restart = "on-failure";
  163. RestartSec = "10";
  164. TimeoutStartSec = "3600";
  165. };
  166. preStart = ''
  167. ${optionalString (cfg.cookie != "") ''
  168. echo -n ${cfg.cookie} > ${cfg.dataDir}/.erlang.cookie
  169. chmod 600 ${cfg.dataDir}/.erlang.cookie
  170. ''}
  171. '';
  172. };
  173. };
  174. }