nixos/services.rabbitmq: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-27 20:42:53 +02:00
parent 3e47355b3f
commit 2d4f871b1a

View file

@ -1,7 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
cfg = config.services.rabbitmq; cfg = config.services.rabbitmq;
@ -16,7 +13,7 @@ in
{ {
imports = [ imports = [
(mkRemovedOptionModule [ "services" "rabbitmq" "cookie" ] '' (lib.mkRemovedOptionModule [ "services" "rabbitmq" "cookie" ] ''
This option wrote the Erlang cookie to the store, while it should be kept secret. This option wrote the Erlang cookie to the store, while it should be kept secret.
Please remove it from your NixOS configuration and deploy a cookie securely instead. Please remove it from your NixOS configuration and deploy a cookie securely instead.
The renamed `unsafeCookie` must ONLY be used in isolated non-production environments such as NixOS VM tests. The renamed `unsafeCookie` must ONLY be used in isolated non-production environments such as NixOS VM tests.
@ -26,8 +23,8 @@ in
###### interface ###### interface
options = { options = {
services.rabbitmq = { services.rabbitmq = {
enable = mkOption { enable = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Whether to enable the RabbitMQ server, an Advanced Message Whether to enable the RabbitMQ server, an Advanced Message
@ -35,9 +32,9 @@ in
''; '';
}; };
package = mkPackageOption pkgs "rabbitmq-server" { }; package = lib.mkPackageOption pkgs "rabbitmq-server" { };
listenAddress = mkOption { listenAddress = lib.mkOption {
default = "127.0.0.1"; default = "127.0.0.1";
example = ""; example = "";
description = '' description = ''
@ -52,28 +49,28 @@ in
configItems."listeners.tcp.1" and it's left for backwards configItems."listeners.tcp.1" and it's left for backwards
compatibility with previous version of this module. compatibility with previous version of this module.
''; '';
type = types.str; type = lib.types.str;
}; };
port = mkOption { port = lib.mkOption {
default = 5672; default = 5672;
description = '' description = ''
Port on which RabbitMQ will listen for AMQP connections. Port on which RabbitMQ will listen for AMQP connections.
''; '';
type = types.port; type = lib.types.port;
}; };
dataDir = mkOption { dataDir = lib.mkOption {
type = types.path; type = lib.types.path;
default = "/var/lib/rabbitmq"; default = "/var/lib/rabbitmq";
description = '' description = ''
Data directory for rabbitmq. Data directory for rabbitmq.
''; '';
}; };
unsafeCookie = mkOption { unsafeCookie = lib.mkOption {
default = ""; default = "";
type = types.str; type = lib.types.str;
description = '' description = ''
Erlang cookie is a string of arbitrary length which must Erlang cookie is a string of arbitrary length which must
be the same for several nodes to be allowed to communicate. be the same for several nodes to be allowed to communicate.
@ -86,10 +83,10 @@ in
''; '';
}; };
configItems = mkOption { configItems = lib.mkOption {
default = { }; default = { };
type = types.attrsOf types.str; type = lib.types.attrsOf lib.types.str;
example = literalExpression '' example = lib.literalExpression ''
{ {
"auth_backends.1.authn" = "rabbit_auth_backend_ldap"; "auth_backends.1.authn" = "rabbit_auth_backend_ldap";
"auth_backends.1.authz" = "rabbit_auth_backend_internal"; "auth_backends.1.authz" = "rabbit_auth_backend_internal";
@ -112,9 +109,9 @@ in
''; '';
}; };
config = mkOption { config = lib.mkOption {
default = ""; default = "";
type = types.str; type = lib.types.str;
description = '' description = ''
Verbatim advanced configuration file contents using the Erlang syntax. Verbatim advanced configuration file contents using the Erlang syntax.
This is also known as the `advanced.config` file or the old config format. This is also known as the `advanced.config` file or the old config format.
@ -130,23 +127,23 @@ in
''; '';
}; };
plugins = mkOption { plugins = lib.mkOption {
default = [ ]; default = [ ];
type = types.listOf types.str; type = lib.types.listOf lib.types.str;
description = "The names of plugins to enable"; description = "The names of plugins to enable";
}; };
pluginDirs = mkOption { pluginDirs = lib.mkOption {
default = [ ]; default = [ ];
type = types.listOf types.path; type = lib.types.listOf lib.types.path;
description = "The list of directories containing external plugins"; description = "The list of directories containing external plugins";
}; };
managementPlugin = { managementPlugin = {
enable = mkEnableOption "the management plugin"; enable = lib.mkEnableOption "the management plugin";
port = mkOption { port = lib.mkOption {
default = 15672; default = 15672;
type = types.port; type = lib.types.port;
description = '' description = ''
On which port to run the management plugin On which port to run the management plugin
''; '';
@ -157,7 +154,7 @@ in
###### implementation ###### implementation
config = mkIf cfg.enable { config = lib.mkIf cfg.enable {
# This is needed so we will have 'rabbitmqctl' in our PATH # This is needed so we will have 'rabbitmqctl' in our PATH
environment.systemPackages = [ cfg.package ]; environment.systemPackages = [ cfg.package ];
@ -175,13 +172,13 @@ in
users.groups.rabbitmq.gid = config.ids.gids.rabbitmq; users.groups.rabbitmq.gid = config.ids.gids.rabbitmq;
services.rabbitmq.configItems = { services.rabbitmq.configItems = {
"listeners.tcp.1" = mkDefault "${cfg.listenAddress}:${toString cfg.port}"; "listeners.tcp.1" = lib.mkDefault "${cfg.listenAddress}:${toString cfg.port}";
} // optionalAttrs cfg.managementPlugin.enable { } // lib.optionalAttrs cfg.managementPlugin.enable {
"management.tcp.port" = toString cfg.managementPlugin.port; "management.tcp.port" = toString cfg.managementPlugin.port;
"management.tcp.ip" = cfg.listenAddress; "management.tcp.ip" = cfg.listenAddress;
}; };
services.rabbitmq.plugins = optional cfg.managementPlugin.enable "rabbitmq_management"; services.rabbitmq.plugins = lib.optional cfg.managementPlugin.enable "rabbitmq_management";
systemd.services.rabbitmq = { systemd.services.rabbitmq = {
description = "RabbitMQ Server"; description = "RabbitMQ Server";
@ -200,11 +197,11 @@ in
RABBITMQ_LOGS = "-"; RABBITMQ_LOGS = "-";
SYS_PREFIX = ""; SYS_PREFIX = "";
RABBITMQ_CONFIG_FILE = config_file; RABBITMQ_CONFIG_FILE = config_file;
RABBITMQ_PLUGINS_DIR = concatStringsSep ":" cfg.pluginDirs; RABBITMQ_PLUGINS_DIR = lib.concatStringsSep ":" cfg.pluginDirs;
RABBITMQ_ENABLED_PLUGINS_FILE = pkgs.writeText "enabled_plugins" '' RABBITMQ_ENABLED_PLUGINS_FILE = pkgs.writeText "enabled_plugins" ''
[ ${concatStringsSep "," cfg.plugins} ]. [ ${lib.concatStringsSep "," cfg.plugins} ].
''; '';
} // optionalAttrs (cfg.config != "") { RABBITMQ_ADVANCED_CONFIG_FILE = advanced_config_file; }; } // lib.optionalAttrs (cfg.config != "") { RABBITMQ_ADVANCED_CONFIG_FILE = advanced_config_file; };
serviceConfig = { serviceConfig = {
ExecStart = "${cfg.package}/sbin/rabbitmq-server"; ExecStart = "${cfg.package}/sbin/rabbitmq-server";
@ -223,7 +220,7 @@ in
}; };
preStart = '' preStart = ''
${optionalString (cfg.unsafeCookie != "") '' ${lib.optionalString (cfg.unsafeCookie != "") ''
install -m 600 <(echo -n ${cfg.unsafeCookie}) ${cfg.dataDir}/.erlang.cookie install -m 600 <(echo -n ${cfg.unsafeCookie}) ${cfg.dataDir}/.erlang.cookie
''} ''}
''; '';