2021-08-10 14:31:46 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2021-12-30 13:39:12 +00:00
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
let
|
2023-08-22 20:05:09 +00:00
|
|
|
inherit
|
|
|
|
(lib)
|
|
|
|
escapeShellArg
|
|
|
|
hasAttr
|
|
|
|
literalExpression
|
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
|
|
|
mkOption
|
|
|
|
types
|
|
|
|
;
|
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
format = pkgs.formats.json { };
|
|
|
|
cfg = config.services.influxdb2;
|
|
|
|
configFile = format.generate "config.json" cfg.settings;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.influxdb2 = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "the influxdb2 server");
|
2021-12-30 13:39:12 +00:00
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
package = mkOption {
|
2021-12-19 01:06:50 +00:00
|
|
|
default = pkgs.influxdb2-server;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "pkgs.influxdb2";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "influxdb2 derivation to use.";
|
2021-08-10 14:31:46 +00:00
|
|
|
type = types.package;
|
|
|
|
};
|
2021-12-30 13:39:12 +00:00
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
settings = mkOption {
|
|
|
|
default = { };
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''configuration options for influxdb2, see <https://docs.influxdata.com/influxdb/v2.0/reference/config-options> for details.'';
|
2021-08-10 14:31:46 +00:00
|
|
|
type = format.type;
|
|
|
|
};
|
2023-08-22 20:05:09 +00:00
|
|
|
|
|
|
|
provision = {
|
|
|
|
enable = mkEnableOption "initial database setup and provisioning";
|
|
|
|
|
|
|
|
initialSetup = {
|
|
|
|
organization = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "main";
|
|
|
|
description = "Primary organization name";
|
|
|
|
};
|
|
|
|
|
|
|
|
bucket = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "example";
|
|
|
|
description = "Primary bucket name";
|
|
|
|
};
|
|
|
|
|
|
|
|
username = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "admin";
|
|
|
|
description = "Primary username";
|
|
|
|
};
|
|
|
|
|
|
|
|
retention = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0";
|
|
|
|
description = ''
|
|
|
|
The duration for which the bucket will retain data (0 is infinite).
|
|
|
|
Accepted units are `ns` (nanoseconds), `us` or `µs` (microseconds), `ms` (milliseconds),
|
|
|
|
`s` (seconds), `m` (minutes), `h` (hours), `d` (days) and `w` (weeks).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = "Password for primary user. Don't use a file from the nix store!";
|
|
|
|
};
|
|
|
|
|
|
|
|
tokenFile = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = "API Token to set for the admin user. Don't use a file from the nix store!";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2021-08-10 14:31:46 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2023-08-22 20:05:09 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !(hasAttr "bolt-path" cfg.settings) && !(hasAttr "engine-path" cfg.settings);
|
|
|
|
message = "services.influxdb2.config: bolt-path and engine-path should not be set as they are managed by systemd";
|
|
|
|
}
|
|
|
|
];
|
2021-12-30 13:39:12 +00:00
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
systemd.services.influxdb2 = {
|
|
|
|
description = "InfluxDB is an open-source, distributed, time series database";
|
|
|
|
documentation = [ "https://docs.influxdata.com/influxdb/" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
environment = {
|
2021-12-30 13:39:12 +00:00
|
|
|
INFLUXD_CONFIG_PATH = configFile;
|
2022-11-04 12:27:35 +00:00
|
|
|
ZONEINFO = "${pkgs.tzdata}/share/zoneinfo";
|
2021-08-10 14:31:46 +00:00
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${cfg.package}/bin/influxd --bolt-path \${STATE_DIRECTORY}/influxd.bolt --engine-path \${STATE_DIRECTORY}/engine";
|
|
|
|
StateDirectory = "influxdb2";
|
2021-12-30 13:39:12 +00:00
|
|
|
User = "influxdb2";
|
|
|
|
Group = "influxdb2";
|
2021-08-10 14:31:46 +00:00
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
SystemCallFilter = "@system-service";
|
|
|
|
LimitNOFILE = 65536;
|
|
|
|
KillMode = "control-group";
|
|
|
|
Restart = "on-failure";
|
2023-08-22 20:05:09 +00:00
|
|
|
LoadCredential = mkIf cfg.provision.enable [
|
|
|
|
"admin-password:${cfg.provision.initialSetup.passwordFile}"
|
|
|
|
"admin-token:${cfg.provision.initialSetup.tokenFile}"
|
|
|
|
];
|
2021-08-10 14:31:46 +00:00
|
|
|
};
|
2023-08-22 20:05:09 +00:00
|
|
|
|
|
|
|
path = [pkgs.influxdb2-cli];
|
|
|
|
|
|
|
|
# Mark if this is the first startup so postStart can do the initial setup
|
|
|
|
preStart = mkIf cfg.provision.enable ''
|
|
|
|
if ! test -e "$STATE_DIRECTORY/influxd.bolt"; then
|
|
|
|
touch "$STATE_DIRECTORY/.first_startup"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
postStart = let
|
|
|
|
initCfg = cfg.provision.initialSetup;
|
|
|
|
in mkIf cfg.provision.enable (
|
|
|
|
''
|
|
|
|
set -euo pipefail
|
|
|
|
export INFLUX_HOST="http://"${escapeShellArg (cfg.settings.http-bind-address or "localhost:8086")}
|
|
|
|
|
|
|
|
# Wait for the influxdb server to come online
|
|
|
|
count=0
|
|
|
|
while ! influx ping &>/dev/null; do
|
|
|
|
if [ "$count" -eq 300 ]; then
|
|
|
|
echo "Tried for 30 seconds, giving up..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! kill -0 "$MAINPID"; then
|
|
|
|
echo "Main server died, giving up..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
sleep 0.1
|
|
|
|
count=$((count++))
|
|
|
|
done
|
|
|
|
|
|
|
|
# Do the initial database setup. Pass /dev/null as configs-path to
|
|
|
|
# avoid saving the token as the active config.
|
|
|
|
if test -e "$STATE_DIRECTORY/.first_startup"; then
|
|
|
|
influx setup \
|
|
|
|
--configs-path /dev/null \
|
|
|
|
--org ${escapeShellArg initCfg.organization} \
|
|
|
|
--bucket ${escapeShellArg initCfg.bucket} \
|
|
|
|
--username ${escapeShellArg initCfg.username} \
|
|
|
|
--password "$(< "$CREDENTIALS_DIRECTORY/admin-password")" \
|
|
|
|
--token "$(< "$CREDENTIALS_DIRECTORY/admin-token")" \
|
|
|
|
--retention ${escapeShellArg initCfg.retention} \
|
|
|
|
--force >/dev/null
|
|
|
|
|
|
|
|
rm -f "$STATE_DIRECTORY/.first_startup"
|
|
|
|
fi
|
|
|
|
''
|
|
|
|
);
|
2021-08-10 14:31:46 +00:00
|
|
|
};
|
2021-12-30 13:39:12 +00:00
|
|
|
|
|
|
|
users.extraUsers.influxdb2 = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "influxdb2";
|
|
|
|
};
|
|
|
|
|
|
|
|
users.extraGroups.influxdb2 = {};
|
2021-08-10 14:31:46 +00:00
|
|
|
};
|
|
|
|
|
2023-08-22 20:05:09 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ nickcao oddlama ];
|
2021-08-10 14:31:46 +00:00
|
|
|
}
|