2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.services.apache-kafka;
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
# The `javaProperties` generator takes care of various escaping rules and
|
|
|
|
# generation of the properties file, but we'll handle stringly conversion
|
|
|
|
# ourselves in mkPropertySettings and stringlySettings, since we know more
|
|
|
|
# about the specifically allowed format eg. for lists of this type, and we
|
|
|
|
# don't want to coerce-downsample values to str too early by having the
|
|
|
|
# coercedTypes from javaProperties directly in our NixOS option types.
|
|
|
|
#
|
|
|
|
# Make sure every `freeformType` and any specific option type in `settings` is
|
|
|
|
# supported here.
|
|
|
|
|
|
|
|
mkPropertyString = let
|
|
|
|
render = {
|
2024-09-19 14:19:46 +00:00
|
|
|
bool = lib.boolToString;
|
2024-01-02 11:29:13 +00:00
|
|
|
int = toString;
|
2024-09-19 14:19:46 +00:00
|
|
|
list = lib.concatMapStringsSep "," mkPropertyString;
|
|
|
|
string = lib.id;
|
2024-01-02 11:29:13 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
v: render.${builtins.typeOf v} v;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
stringlySettings = lib.mapAttrs (_: mkPropertyString)
|
|
|
|
(lib.filterAttrs (_: v: v != null) cfg.settings);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
generator = (pkgs.formats.javaProperties {}).generate;
|
2020-04-24 23:36:52 +00:00
|
|
|
in {
|
|
|
|
|
|
|
|
options.services.apache-kafka = {
|
2024-09-19 14:19:46 +00:00
|
|
|
enable = lib.mkEnableOption "Apache Kafka event streaming broker";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
settings = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-01-02 11:29:13 +00:00
|
|
|
[Kafka broker configuration](https://kafka.apache.org/documentation.html#brokerconfigs)
|
|
|
|
{file}`server.properties`.
|
|
|
|
|
|
|
|
Note that .properties files contain mappings from string to string.
|
|
|
|
Keys with dots are NOT represented by nested attrs in these settings,
|
|
|
|
but instead as quoted strings (ie. `settings."broker.id"`, NOT
|
|
|
|
`settings.broker.id`).
|
|
|
|
'';
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.submodule {
|
|
|
|
freeformType = with lib.types; let
|
2024-01-02 11:29:13 +00:00
|
|
|
primitive = oneOf [bool int str];
|
|
|
|
in lazyAttrsOf (nullOr (either primitive (listOf primitive)));
|
|
|
|
|
|
|
|
options = {
|
2024-09-19 14:19:46 +00:00
|
|
|
"broker.id" = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Broker ID. -1 or null to auto-allocate in zookeeper mode.";
|
2024-01-02 11:29:13 +00:00
|
|
|
default = null;
|
2024-09-19 14:19:46 +00:00
|
|
|
type = with lib.types; nullOr int;
|
2024-01-02 11:29:13 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
"log.dirs" = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Log file directories.";
|
2024-01-02 11:29:13 +00:00
|
|
|
# Deliberaly leave out old default and use the rewrite opportunity
|
|
|
|
# to have users choose a safer value -- /tmp might be volatile and is a
|
|
|
|
# slightly scary default choice.
|
|
|
|
# default = [ "/tmp/apache-kafka" ];
|
2024-09-19 14:19:46 +00:00
|
|
|
type = with lib.types; listOf path;
|
2024-01-02 11:29:13 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
"listeners" = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-01-02 11:29:13 +00:00
|
|
|
Kafka Listener List.
|
|
|
|
See [listeners](https://kafka.apache.org/documentation/#brokerconfigs_listeners).
|
|
|
|
'';
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.listOf lib.types.str;
|
2024-01-02 11:29:13 +00:00
|
|
|
default = [ "PLAINTEXT://localhost:9092" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
clusterId = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-01-02 11:29:13 +00:00
|
|
|
KRaft mode ClusterId used for formatting log directories. Can be generated with `kafka-storage.sh random-uuid`
|
|
|
|
'';
|
2024-09-19 14:19:46 +00:00
|
|
|
type = with lib.types; nullOr str;
|
2024-01-02 11:29:13 +00:00
|
|
|
default = null;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
configFiles.serverProperties = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-01-02 11:29:13 +00:00
|
|
|
Kafka server.properties configuration file path.
|
|
|
|
Defaults to the rendered `settings`.
|
|
|
|
'';
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.path;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
configFiles.log4jProperties = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Kafka log4j property configuration file path";
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.path;
|
2024-01-02 11:29:13 +00:00
|
|
|
default = pkgs.writeText "log4j.properties" cfg.log4jProperties;
|
|
|
|
defaultText = ''pkgs.writeText "log4j.properties" cfg.log4jProperties'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
formatLogDirs = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-01-02 11:29:13 +00:00
|
|
|
Whether to format log dirs in KRaft mode if all log dirs are
|
|
|
|
unformatted, ie. they contain no meta.properties.
|
|
|
|
'';
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.bool;
|
2024-01-02 11:29:13 +00:00
|
|
|
default = false;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
formatLogDirsIgnoreFormatted = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-01-02 11:29:13 +00:00
|
|
|
Whether to ignore already formatted log dirs when formatting log dirs,
|
|
|
|
instead of failing. Useful when replacing or adding disks.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.bool;
|
2024-01-02 11:29:13 +00:00
|
|
|
default = false;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
log4jProperties = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Kafka log4j property configuration.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = ''
|
|
|
|
log4j.rootLogger=INFO, stdout
|
|
|
|
|
|
|
|
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
|
|
|
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
|
|
|
log4j.appender.stdout.layout.ConversionPattern=[%d] %p %m (%c)%n
|
|
|
|
'';
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
jvmOptions = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Extra command line options for the JVM running Kafka.";
|
2021-03-12 07:09:13 +00:00
|
|
|
default = [];
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.listOf lib.types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
example = [
|
|
|
|
"-Djava.net.preferIPv4Stack=true"
|
|
|
|
"-Dcom.sun.management.jmxremote"
|
|
|
|
"-Dcom.sun.management.jmxremote.local.only=true"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
package = lib.mkPackageOption pkgs "apacheKafka" { };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
jre = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The JRE with which to run Kafka";
|
2021-03-12 07:09:13 +00:00
|
|
|
default = cfg.package.passthru.jre;
|
2024-09-19 14:19:46 +00:00
|
|
|
defaultText = lib.literalExpression "pkgs.apacheKafka.passthru.jre";
|
|
|
|
type = lib.types.package;
|
2021-03-12 07:09:13 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
imports = [
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkRenamedOptionModule
|
2024-01-02 11:29:13 +00:00
|
|
|
[ "services" "apache-kafka" "brokerId" ]
|
|
|
|
[ "services" "apache-kafka" "settings" ''broker.id'' ])
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkRenamedOptionModule
|
2024-01-02 11:29:13 +00:00
|
|
|
[ "services" "apache-kafka" "logDirs" ]
|
|
|
|
[ "services" "apache-kafka" "settings" ''log.dirs'' ])
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkRenamedOptionModule
|
2024-01-02 11:29:13 +00:00
|
|
|
[ "services" "apache-kafka" "zookeeper" ]
|
|
|
|
[ "services" "apache-kafka" "settings" ''zookeeper.connect'' ])
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkRemovedOptionModule [ "services" "apache-kafka" "port" ]
|
2024-01-02 11:29:13 +00:00
|
|
|
"Please see services.apache-kafka.settings.listeners and its documentation instead")
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkRemovedOptionModule [ "services" "apache-kafka" "hostname" ]
|
2024-01-02 11:29:13 +00:00
|
|
|
"Please see services.apache-kafka.settings.listeners and its documentation instead")
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkRemovedOptionModule [ "services" "apache-kafka" "extraProperties" ]
|
2024-01-02 11:29:13 +00:00
|
|
|
"Please see services.apache-kafka.settings and its documentation instead")
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkRemovedOptionModule [ "services" "apache-kafka" "serverProperties" ]
|
2024-01-02 11:29:13 +00:00
|
|
|
"Please see services.apache-kafka.settings and its documentation instead")
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2024-01-02 11:29:13 +00:00
|
|
|
services.apache-kafka.configFiles.serverProperties = generator "server.properties" stringlySettings;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
users.users.apache-kafka = {
|
2021-09-18 10:52:07 +00:00
|
|
|
isSystemUser = true;
|
|
|
|
group = "apache-kafka";
|
2020-04-24 23:36:52 +00:00
|
|
|
description = "Apache Kafka daemon user";
|
|
|
|
};
|
2021-09-18 10:52:07 +00:00
|
|
|
users.groups.apache-kafka = {};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
systemd.tmpfiles.rules = map (logDir: "d '${logDir}' 0700 apache-kafka - - -") cfg.settings."log.dirs";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
systemd.services.apache-kafka = {
|
|
|
|
description = "Apache Kafka Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2024-09-19 14:19:46 +00:00
|
|
|
preStart = lib.mkIf cfg.formatLogDirs
|
2024-01-02 11:29:13 +00:00
|
|
|
(if cfg.formatLogDirsIgnoreFormatted then ''
|
|
|
|
${cfg.package}/bin/kafka-storage.sh format -t "${cfg.clusterId}" -c ${cfg.configFiles.serverProperties} --ignore-formatted
|
|
|
|
'' else ''
|
2024-09-19 14:19:46 +00:00
|
|
|
if ${lib.concatMapStringsSep " && " (l: ''[ ! -f "${l}/meta.properties" ]'') cfg.settings."log.dirs"}; then
|
2024-01-02 11:29:13 +00:00
|
|
|
${cfg.package}/bin/kafka-storage.sh format -t "${cfg.clusterId}" -c ${cfg.configFiles.serverProperties}
|
|
|
|
fi
|
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = ''
|
2021-03-12 07:09:13 +00:00
|
|
|
${cfg.jre}/bin/java \
|
2020-04-24 23:36:52 +00:00
|
|
|
-cp "${cfg.package}/libs/*" \
|
2024-01-02 11:29:13 +00:00
|
|
|
-Dlog4j.configuration=file:${cfg.configFiles.log4jProperties} \
|
2020-04-24 23:36:52 +00:00
|
|
|
${toString cfg.jvmOptions} \
|
|
|
|
kafka.Kafka \
|
2024-01-02 11:29:13 +00:00
|
|
|
${cfg.configFiles.serverProperties}
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
User = "apache-kafka";
|
|
|
|
SuccessExitStatus = "0 143";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-01-02 11:29:13 +00:00
|
|
|
|
|
|
|
meta.doc = ./kafka.md;
|
|
|
|
meta.maintainers = with lib.maintainers; [
|
|
|
|
srhb
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|