2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-08-27 18:42:59 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2013-08-27 18:42:59 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.elasticsearch;
|
|
|
|
|
2021-09-16 16:18:09 +00:00
|
|
|
es7 = builtins.compareVersions cfg.package.version "7" >= 0;
|
|
|
|
|
2014-03-10 10:04:29 +00:00
|
|
|
esConfig = ''
|
2015-12-23 23:28:09 +00:00
|
|
|
network.host: ${cfg.listenAddress}
|
2013-08-27 18:42:59 +00:00
|
|
|
cluster.name: ${cfg.cluster_name}
|
2021-09-16 16:18:09 +00:00
|
|
|
${lib.optionalString cfg.single_node "discovery.type: single-node"}
|
|
|
|
${lib.optionalString (cfg.single_node && es7) "gateway.auto_import_dangling_indices: true"}
|
2017-05-17 12:46:16 +00:00
|
|
|
|
2018-08-03 10:09:50 +00:00
|
|
|
http.port: ${toString cfg.port}
|
2021-09-06 13:28:10 +00:00
|
|
|
transport.port: ${toString cfg.tcp_port}
|
2017-05-17 12:46:16 +00:00
|
|
|
|
2013-08-27 18:42:59 +00:00
|
|
|
${cfg.extraConf}
|
|
|
|
'';
|
|
|
|
|
2018-06-24 11:22:12 +00:00
|
|
|
configDir = cfg.dataDir + "/config";
|
|
|
|
|
|
|
|
elasticsearchYml = pkgs.writeTextFile {
|
|
|
|
name = "elasticsearch.yml";
|
|
|
|
text = esConfig;
|
|
|
|
};
|
|
|
|
|
2018-08-03 10:09:50 +00:00
|
|
|
loggingConfigFilename = "log4j2.properties";
|
2018-06-24 11:22:12 +00:00
|
|
|
loggingConfigFile = pkgs.writeTextFile {
|
|
|
|
name = loggingConfigFilename;
|
|
|
|
text = cfg.logging;
|
2014-03-10 10:04:29 +00:00
|
|
|
};
|
|
|
|
|
2014-07-05 10:09:53 +00:00
|
|
|
esPlugins = pkgs.buildEnv {
|
|
|
|
name = "elasticsearch-plugins";
|
|
|
|
paths = cfg.plugins;
|
2018-08-03 10:09:50 +00:00
|
|
|
postBuild = "${pkgs.coreutils}/bin/mkdir -p $out/plugins";
|
2014-07-05 10:09:53 +00:00
|
|
|
};
|
|
|
|
|
2021-09-01 06:51:33 +00:00
|
|
|
in
|
|
|
|
{
|
2013-08-27 18:42:59 +00:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options.services.elasticsearch = {
|
|
|
|
enable = mkOption {
|
2014-06-24 19:04:56 +00:00
|
|
|
description = "Whether to enable elasticsearch.";
|
2013-08-27 18:42:59 +00:00
|
|
|
default = false;
|
2015-06-15 16:10:26 +00:00
|
|
|
type = types.bool;
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
|
|
|
|
2023-11-27 00:19:27 +00:00
|
|
|
package = mkPackageOption pkgs "elasticsearch" { };
|
2015-09-15 20:49:04 +00:00
|
|
|
|
2015-12-23 23:28:09 +00:00
|
|
|
listenAddress = mkOption {
|
2014-06-24 19:04:56 +00:00
|
|
|
description = "Elasticsearch listen address.";
|
2013-08-27 18:42:59 +00:00
|
|
|
default = "127.0.0.1";
|
2013-10-30 10:02:04 +00:00
|
|
|
type = types.str;
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2014-06-24 19:04:56 +00:00
|
|
|
description = "Elasticsearch port to listen for HTTP traffic.";
|
2014-03-10 10:04:29 +00:00
|
|
|
default = 9200;
|
2022-11-30 16:15:00 +00:00
|
|
|
type = types.port;
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tcp_port = mkOption {
|
2014-06-24 19:04:56 +00:00
|
|
|
description = "Elasticsearch port for the node to node communication.";
|
2014-03-10 10:04:29 +00:00
|
|
|
default = 9300;
|
|
|
|
type = types.int;
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cluster_name = mkOption {
|
2014-06-24 19:04:56 +00:00
|
|
|
description = "Elasticsearch name that identifies your cluster for auto-discovery.";
|
2013-08-27 18:42:59 +00:00
|
|
|
default = "elasticsearch";
|
2013-10-30 10:02:04 +00:00
|
|
|
type = types.str;
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
|
|
|
|
2021-09-06 13:28:10 +00:00
|
|
|
single_node = mkOption {
|
|
|
|
description = "Start a single-node cluster";
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
|
2013-08-27 18:42:59 +00:00
|
|
|
extraConf = mkOption {
|
2014-06-24 19:04:56 +00:00
|
|
|
description = "Extra configuration for elasticsearch.";
|
2013-08-27 18:42:59 +00:00
|
|
|
default = "";
|
2013-10-30 10:02:04 +00:00
|
|
|
type = types.str;
|
2013-08-27 18:42:59 +00:00
|
|
|
example = ''
|
|
|
|
node.name: "elasticsearch"
|
|
|
|
node.master: true
|
|
|
|
node.data: false
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
logging = mkOption {
|
2014-06-24 19:04:56 +00:00
|
|
|
description = "Elasticsearch logging configuration.";
|
2018-08-03 10:09:50 +00:00
|
|
|
default = ''
|
|
|
|
logger.action.name = org.elasticsearch.action
|
|
|
|
logger.action.level = info
|
|
|
|
|
|
|
|
appender.console.type = Console
|
|
|
|
appender.console.name = console
|
|
|
|
appender.console.layout.type = PatternLayout
|
|
|
|
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%m%n
|
|
|
|
|
|
|
|
rootLogger.level = info
|
|
|
|
rootLogger.appenderRef.console.ref = console
|
|
|
|
'';
|
2013-10-30 10:02:04 +00:00
|
|
|
type = types.str;
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
2014-03-10 10:04:29 +00:00
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/elasticsearch";
|
|
|
|
description = ''
|
|
|
|
Data directory for elasticsearch.
|
|
|
|
'';
|
|
|
|
};
|
2014-06-24 09:01:28 +00:00
|
|
|
|
|
|
|
extraCmdLineOptions = mkOption {
|
2014-06-24 19:04:56 +00:00
|
|
|
description = "Extra command line options for the elasticsearch launcher.";
|
2021-09-01 06:51:33 +00:00
|
|
|
default = [ ];
|
2015-06-15 16:18:46 +00:00
|
|
|
type = types.listOf types.str;
|
2017-05-17 12:46:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extraJavaOptions = mkOption {
|
|
|
|
description = "Extra command line options for Java.";
|
2021-09-01 06:51:33 +00:00
|
|
|
default = [ ];
|
2017-05-17 12:46:16 +00:00
|
|
|
type = types.listOf types.str;
|
2014-06-24 09:01:28 +00:00
|
|
|
example = [ "-Djava.net.preferIPv4Stack=true" ];
|
|
|
|
};
|
|
|
|
|
2014-07-05 10:09:53 +00:00
|
|
|
plugins = mkOption {
|
|
|
|
description = "Extra elasticsearch plugins";
|
2021-09-01 06:51:33 +00:00
|
|
|
default = [ ];
|
2014-07-05 10:09:53 +00:00
|
|
|
type = types.listOf types.package;
|
2021-10-03 16:06:03 +00:00
|
|
|
example = lib.literalExpression "[ pkgs.elasticsearchPlugins.discovery-ec2 ]";
|
2014-07-05 10:09:53 +00:00
|
|
|
};
|
|
|
|
|
2022-01-05 13:10:39 +00:00
|
|
|
restartIfChanged = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Automatically restart the service on config change.
|
|
|
|
This can be set to false to defer restarts on a server or cluster.
|
|
|
|
Please consider the security implications of inadvertently running an older version,
|
|
|
|
and the possibility of unexpected behavior caused by inconsistent versions across a cluster when disabling this option.
|
|
|
|
'';
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2013-11-05 07:52:23 +00:00
|
|
|
systemd.services.elasticsearch = {
|
2014-06-24 19:10:22 +00:00
|
|
|
description = "Elasticsearch Daemon";
|
2013-08-27 18:42:59 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2016-09-10 18:20:12 +00:00
|
|
|
after = [ "network.target" ];
|
2016-01-18 14:57:52 +00:00
|
|
|
path = [ pkgs.inetutils ];
|
2022-01-05 13:10:39 +00:00
|
|
|
inherit (cfg) restartIfChanged;
|
2015-12-04 00:12:25 +00:00
|
|
|
environment = {
|
|
|
|
ES_HOME = cfg.dataDir;
|
2021-08-31 03:43:28 +00:00
|
|
|
ES_JAVA_OPTS = toString cfg.extraJavaOptions;
|
2017-12-18 19:53:54 +00:00
|
|
|
ES_PATH_CONF = configDir;
|
2015-12-04 00:12:25 +00:00
|
|
|
};
|
2013-08-27 18:42:59 +00:00
|
|
|
serviceConfig = {
|
2017-05-17 12:46:16 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/elasticsearch ${toString cfg.extraCmdLineOptions}";
|
2013-08-27 18:42:59 +00:00
|
|
|
User = "elasticsearch";
|
2014-06-13 11:42:52 +00:00
|
|
|
PermissionsStartOnly = true;
|
2017-05-17 12:46:16 +00:00
|
|
|
LimitNOFILE = "1024000";
|
2022-01-05 13:10:39 +00:00
|
|
|
Restart = "always";
|
|
|
|
TimeoutStartSec = "infinity";
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
2014-03-10 10:04:29 +00:00
|
|
|
preStart = ''
|
2017-10-24 20:31:06 +00:00
|
|
|
${optionalString (!config.boot.isContainer) ''
|
|
|
|
# Only set vm.max_map_count if lower than ES required minimum
|
|
|
|
# This avoids conflict if configured via boot.kernel.sysctl
|
|
|
|
if [ `${pkgs.procps}/bin/sysctl -n vm.max_map_count` -lt 262144 ]; then
|
|
|
|
${pkgs.procps}/bin/sysctl -w vm.max_map_count=262144
|
|
|
|
fi
|
|
|
|
''}
|
2017-05-17 12:46:16 +00:00
|
|
|
|
2014-03-10 10:04:29 +00:00
|
|
|
mkdir -m 0700 -p ${cfg.dataDir}
|
2014-07-05 10:09:53 +00:00
|
|
|
|
|
|
|
# Install plugins
|
2016-01-18 14:57:52 +00:00
|
|
|
ln -sfT ${esPlugins}/plugins ${cfg.dataDir}/plugins
|
2015-12-04 00:12:25 +00:00
|
|
|
ln -sfT ${cfg.package}/lib ${cfg.dataDir}/lib
|
2016-04-29 10:01:13 +00:00
|
|
|
ln -sfT ${cfg.package}/modules ${cfg.dataDir}/modules
|
2018-06-24 11:22:12 +00:00
|
|
|
|
|
|
|
# elasticsearch needs to create the elasticsearch.keystore in the config directory
|
|
|
|
# so this directory needs to be writable.
|
|
|
|
mkdir -m 0700 -p ${configDir}
|
|
|
|
|
|
|
|
# Note that we copy config files from the nix store instead of symbolically linking them
|
|
|
|
# because otherwise X-Pack Security will raise the following exception:
|
|
|
|
# java.security.AccessControlException:
|
|
|
|
# access denied ("java.io.FilePermission" "/var/lib/elasticsearch/config/elasticsearch.yml" "read")
|
|
|
|
|
|
|
|
cp ${elasticsearchYml} ${configDir}/elasticsearch.yml
|
|
|
|
# Make sure the logging configuration for old elasticsearch versions is removed:
|
2018-08-03 10:09:50 +00:00
|
|
|
rm -f "${configDir}/logging.yml"
|
2018-06-24 11:22:12 +00:00
|
|
|
cp ${loggingConfigFile} ${configDir}/${loggingConfigFilename}
|
2018-08-03 10:09:50 +00:00
|
|
|
mkdir -p ${configDir}/scripts
|
2021-08-31 03:43:28 +00:00
|
|
|
cp ${cfg.package}/config/jvm.options ${configDir}/jvm.options
|
2021-09-01 06:52:31 +00:00
|
|
|
# redirect jvm logs to the data directory
|
|
|
|
mkdir -m 0700 -p ${cfg.dataDir}/logs
|
|
|
|
${pkgs.sd}/bin/sd 'logs/gc.log' '${cfg.dataDir}/logs/gc.log' ${configDir}/jvm.options \
|
2018-06-24 11:22:12 +00:00
|
|
|
|
|
|
|
if [ "$(id -u)" = 0 ]; then chown -R elasticsearch:elasticsearch ${cfg.dataDir}; fi
|
2014-03-10 10:04:29 +00:00
|
|
|
'';
|
2021-06-02 12:33:13 +00:00
|
|
|
postStart = ''
|
|
|
|
# Make sure elasticsearch is up and running before dependents
|
|
|
|
# are started
|
2021-12-30 18:03:35 +00:00
|
|
|
while ! ${pkgs.curl}/bin/curl -sS -f http://${cfg.listenAddress}:${toString cfg.port} 2>/dev/null; do
|
2021-06-02 12:33:13 +00:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
'';
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
|
|
|
|
2015-09-15 20:49:04 +00:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2013-08-27 18:42:59 +00:00
|
|
|
|
2016-07-01 16:01:52 +00:00
|
|
|
users = {
|
|
|
|
groups.elasticsearch.gid = config.ids.gids.elasticsearch;
|
|
|
|
users.elasticsearch = {
|
|
|
|
uid = config.ids.uids.elasticsearch;
|
|
|
|
description = "Elasticsearch daemon user";
|
|
|
|
home = cfg.dataDir;
|
|
|
|
group = "elasticsearch";
|
|
|
|
};
|
2013-08-27 18:42:59 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|