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)
|
2023-10-09 19:29:22 +00:00
|
|
|
any
|
|
|
|
attrNames
|
|
|
|
attrValues
|
|
|
|
count
|
2023-08-22 20:05:09 +00:00
|
|
|
escapeShellArg
|
2023-10-09 19:29:22 +00:00
|
|
|
filterAttrs
|
|
|
|
flatten
|
|
|
|
flip
|
|
|
|
getExe
|
2023-08-22 20:05:09 +00:00
|
|
|
hasAttr
|
2023-10-09 19:29:22 +00:00
|
|
|
hasInfix
|
|
|
|
listToAttrs
|
2023-08-22 20:05:09 +00:00
|
|
|
literalExpression
|
2023-10-09 19:29:22 +00:00
|
|
|
mapAttrsToList
|
2023-08-22 20:05:09 +00:00
|
|
|
mkEnableOption
|
2024-01-02 11:29:13 +00:00
|
|
|
mkPackageOption
|
2023-08-22 20:05:09 +00:00
|
|
|
mkIf
|
|
|
|
mkOption
|
2023-10-09 19:29:22 +00:00
|
|
|
nameValuePair
|
|
|
|
optional
|
|
|
|
subtractLists
|
2023-08-22 20:05:09 +00:00
|
|
|
types
|
2023-10-09 19:29:22 +00:00
|
|
|
unique
|
2023-08-22 20:05:09 +00:00
|
|
|
;
|
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
format = pkgs.formats.json { };
|
|
|
|
cfg = config.services.influxdb2;
|
|
|
|
configFile = format.generate "config.json" cfg.settings;
|
2023-10-09 19:29:22 +00:00
|
|
|
|
|
|
|
validPermissions = [
|
|
|
|
"authorizations"
|
|
|
|
"buckets"
|
|
|
|
"dashboards"
|
|
|
|
"orgs"
|
|
|
|
"tasks"
|
|
|
|
"telegrafs"
|
|
|
|
"users"
|
|
|
|
"variables"
|
|
|
|
"secrets"
|
|
|
|
"labels"
|
|
|
|
"views"
|
|
|
|
"documents"
|
|
|
|
"notificationRules"
|
|
|
|
"notificationEndpoints"
|
|
|
|
"checks"
|
|
|
|
"dbrp"
|
|
|
|
"annotations"
|
|
|
|
"sources"
|
|
|
|
"scrapers"
|
|
|
|
"notebooks"
|
|
|
|
"remotes"
|
|
|
|
"replications"
|
|
|
|
];
|
|
|
|
|
|
|
|
# Determines whether at least one active api token is defined
|
|
|
|
anyAuthDefined =
|
|
|
|
flip any (attrValues cfg.provision.organizations)
|
|
|
|
(o: o.present && flip any (attrValues o.auths)
|
|
|
|
(a: a.present && a.tokenFile != null));
|
|
|
|
|
|
|
|
provisionState = pkgs.writeText "provision_state.json" (builtins.toJSON {
|
|
|
|
inherit (cfg.provision) organizations users;
|
|
|
|
});
|
|
|
|
|
2024-09-26 11:04:55 +00:00
|
|
|
influxHost = "http://${escapeShellArg (
|
2023-10-09 19:29:22 +00:00
|
|
|
if ! hasAttr "http-bind-address" cfg.settings
|
|
|
|
|| hasInfix "0.0.0.0" cfg.settings.http-bind-address
|
|
|
|
then "localhost:8086"
|
|
|
|
else cfg.settings.http-bind-address
|
2024-09-26 11:04:55 +00:00
|
|
|
)}";
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2024-09-26 11:04:55 +00:00
|
|
|
waitUntilServiceIsReady = pkgs.writeShellScript "wait-until-service-is-ready" ''
|
|
|
|
set -euo pipefail
|
|
|
|
export INFLUX_HOST=${influxHost}
|
2023-10-09 19:29:22 +00:00
|
|
|
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
|
2024-09-26 11:04:55 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
provisioningScript = pkgs.writeShellScript "post-start-provision" ''
|
|
|
|
set -euo pipefail
|
|
|
|
export INFLUX_HOST=${influxHost}
|
2023-10-09 19:29:22 +00:00
|
|
|
|
|
|
|
# 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 cfg.provision.initialSetup.organization} \
|
|
|
|
--bucket ${escapeShellArg cfg.provision.initialSetup.bucket} \
|
|
|
|
--username ${escapeShellArg cfg.provision.initialSetup.username} \
|
|
|
|
--password "$(< "$CREDENTIALS_DIRECTORY/admin-password")" \
|
|
|
|
--token "$(< "$CREDENTIALS_DIRECTORY/admin-token")" \
|
|
|
|
--retention ${toString cfg.provision.initialSetup.retention}s \
|
|
|
|
--force >/dev/null
|
|
|
|
|
|
|
|
rm -f "$STATE_DIRECTORY/.first_startup"
|
|
|
|
fi
|
|
|
|
|
|
|
|
provision_result=$(${getExe pkgs.influxdb2-provision} ${provisionState} "$INFLUX_HOST" "$(< "$CREDENTIALS_DIRECTORY/admin-token")")
|
|
|
|
if [[ "$(jq '[.auths[] | select(.action == "created")] | length' <<< "$provision_result")" -gt 0 ]]; then
|
|
|
|
echo "Created at least one new token, queueing service restart so we can manipulate secrets"
|
|
|
|
touch "$STATE_DIRECTORY/.needs_restart"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
restarterScript = pkgs.writeShellScript "post-start-restarter" ''
|
|
|
|
set -euo pipefail
|
|
|
|
if test -e "$STATE_DIRECTORY/.needs_restart"; then
|
|
|
|
rm -f "$STATE_DIRECTORY/.needs_restart"
|
|
|
|
/run/current-system/systemd/bin/systemctl restart influxdb2
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
organizationSubmodule = types.submodule (organizationSubmod: let
|
|
|
|
org = organizationSubmod.config._module.args.name;
|
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
present = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to ensure that this organization is present or absent.";
|
2023-10-09 19:29:22 +00:00
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
description = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Optional description for the organization.";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
buckets = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Buckets to provision in this organization.";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = {};
|
|
|
|
type = types.attrsOf (types.submodule (bucketSubmod: let
|
|
|
|
bucket = bucketSubmod.config._module.args.name;
|
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
present = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to ensure that this bucket is present or absent.";
|
2023-10-09 19:29:22 +00:00
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
description = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Optional description for the bucket.";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
retention = mkOption {
|
|
|
|
type = types.ints.unsigned;
|
|
|
|
default = 0;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The duration in seconds for which the bucket will retain data (0 is infinite).";
|
2023-10-09 19:29:22 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
auths = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "API tokens to provision for the user in this organization.";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = {};
|
|
|
|
type = types.attrsOf (types.submodule (authSubmod: let
|
|
|
|
auth = authSubmod.config._module.args.name;
|
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
id = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "A unique identifier for this authentication token. Since influx doesn't store names for tokens, this will be hashed and appended to the description to identify the token.";
|
2023-10-09 19:29:22 +00:00
|
|
|
readOnly = true;
|
|
|
|
default = builtins.substring 0 32 (builtins.hashString "sha256" "${org}:${auth}");
|
|
|
|
defaultText = "<a hash derived from org and name>";
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
present = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to ensure that this user is present or absent.";
|
2023-10-09 19:29:22 +00:00
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
description = mkOption {
|
|
|
|
description = ''
|
|
|
|
Optional description for the API token.
|
|
|
|
Note that the actual token will always be created with a descriptionregardless
|
|
|
|
of whether this is given or not. The name is always added plus a unique suffix
|
|
|
|
to later identify the token to track whether it has already been created.
|
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
tokenFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The token value. If not given, influx will automatically generate one.";
|
2023-10-09 19:29:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
operator = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Grants all permissions in all organizations.";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
allAccess = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Grants all permissions in the associated organization.";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
readPermissions = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-10-09 19:29:22 +00:00
|
|
|
The read permissions to include for this token. Access is usually granted only
|
|
|
|
for resources in the associated organization.
|
|
|
|
|
|
|
|
Available permissions are `authorizations`, `buckets`, `dashboards`,
|
|
|
|
`orgs`, `tasks`, `telegrafs`, `users`, `variables`, `secrets`, `labels`, `views`,
|
|
|
|
`documents`, `notificationRules`, `notificationEndpoints`, `checks`, `dbrp`,
|
|
|
|
`annotations`, `sources`, `scrapers`, `notebooks`, `remotes`, `replications`.
|
|
|
|
|
|
|
|
Refer to `influx auth create --help` for a full list with descriptions.
|
|
|
|
|
|
|
|
`buckets` grants read access to all associated buckets. Use `readBuckets` to define
|
|
|
|
more granular access permissions.
|
|
|
|
'';
|
|
|
|
default = [];
|
|
|
|
type = types.listOf (types.enum validPermissions);
|
|
|
|
};
|
|
|
|
|
|
|
|
writePermissions = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-10-09 19:29:22 +00:00
|
|
|
The read permissions to include for this token. Access is usually granted only
|
|
|
|
for resources in the associated organization.
|
|
|
|
|
|
|
|
Available permissions are `authorizations`, `buckets`, `dashboards`,
|
|
|
|
`orgs`, `tasks`, `telegrafs`, `users`, `variables`, `secrets`, `labels`, `views`,
|
|
|
|
`documents`, `notificationRules`, `notificationEndpoints`, `checks`, `dbrp`,
|
|
|
|
`annotations`, `sources`, `scrapers`, `notebooks`, `remotes`, `replications`.
|
|
|
|
|
|
|
|
Refer to `influx auth create --help` for a full list with descriptions.
|
|
|
|
|
|
|
|
`buckets` grants write access to all associated buckets. Use `writeBuckets` to define
|
|
|
|
more granular access permissions.
|
|
|
|
'';
|
|
|
|
default = [];
|
|
|
|
type = types.listOf (types.enum validPermissions);
|
|
|
|
};
|
|
|
|
|
|
|
|
readBuckets = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The organization's buckets which should be allowed to be read";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = [];
|
|
|
|
type = types.listOf types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
writeBuckets = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The organization's buckets which should be allowed to be written";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = [];
|
|
|
|
type = types.listOf types.str;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2021-08-10 14:31:46 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.influxdb2 = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "the influxdb2 server";
|
2021-12-30 13:39:12 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = mkPackageOption pkgs "influxdb2" { };
|
2021-12-30 13:39:12 +00:00
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
settings = mkOption {
|
|
|
|
default = { };
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''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";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Primary organization name";
|
2023-08-22 20:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bucket = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "example";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Primary bucket name";
|
2023-08-22 20:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
username = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "admin";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Primary username";
|
2023-08-22 20:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
retention = mkOption {
|
2023-10-09 19:29:22 +00:00
|
|
|
type = types.ints.unsigned;
|
|
|
|
default = 0;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "The duration in seconds for which the bucket will retain data (0 is infinite).";
|
2023-08-22 20:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.path;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Password for primary user. Don't use a file from the nix store!";
|
2023-08-22 20:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tokenFile = mkOption {
|
|
|
|
type = types.path;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "API Token to set for the admin user. Don't use a file from the nix store!";
|
2023-08-22 20:05:09 +00:00
|
|
|
};
|
|
|
|
};
|
2023-10-09 19:29:22 +00:00
|
|
|
|
|
|
|
organizations = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Organizations to provision.";
|
2023-10-09 19:29:22 +00:00
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
myorg = {
|
|
|
|
description = "My organization";
|
|
|
|
buckets.mybucket = {
|
|
|
|
description = "My bucket";
|
|
|
|
retention = 31536000; # 1 year
|
|
|
|
};
|
|
|
|
auths.mytoken = {
|
|
|
|
readBuckets = ["mybucket"];
|
|
|
|
tokenFile = "/run/secrets/mytoken";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
default = {};
|
|
|
|
type = types.attrsOf organizationSubmodule;
|
|
|
|
};
|
|
|
|
|
|
|
|
users = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Users to provision.";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = {};
|
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
# admin = {}; /* The initialSetup.username will automatically be added. */
|
|
|
|
myuser.passwordFile = "/run/secrets/myuser_password";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
type = types.attrsOf (types.submodule (userSubmod: let
|
|
|
|
user = userSubmod.config._module.args.name;
|
|
|
|
org = userSubmod.config.org;
|
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
present = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to ensure that this user is present or absent.";
|
2023-10-09 19:29:22 +00:00
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Password for the user. If unset, the user will not be able to log in until a password is set by an operator! Don't use a file from the nix store!";
|
2023-10-09 19:29:22 +00:00
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
};
|
2023-08-22 20:05:09 +00:00
|
|
|
};
|
2021-08-10 14:31:46 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2023-10-09 19:29:22 +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";
|
|
|
|
}
|
|
|
|
]
|
|
|
|
++ flatten (flip mapAttrsToList cfg.provision.organizations (orgName: org:
|
|
|
|
flip mapAttrsToList org.auths (authName: auth:
|
|
|
|
[
|
|
|
|
{
|
|
|
|
assertion = 1 == count (x: x) [
|
|
|
|
auth.operator
|
|
|
|
auth.allAccess
|
|
|
|
(auth.readPermissions != []
|
|
|
|
|| auth.writePermissions != []
|
|
|
|
|| auth.readBuckets != []
|
|
|
|
|| auth.writeBuckets != [])
|
|
|
|
];
|
|
|
|
message = "influxdb2: provision.organizations.${orgName}.auths.${authName}: The `operator` and `allAccess` options are mutually exclusive with each other and the granular permission settings.";
|
|
|
|
}
|
|
|
|
(let unknownBuckets = subtractLists (attrNames org.buckets) auth.readBuckets; in {
|
|
|
|
assertion = unknownBuckets == [];
|
|
|
|
message = "influxdb2: provision.organizations.${orgName}.auths.${authName}: Refers to invalid buckets in readBuckets: ${toString unknownBuckets}";
|
|
|
|
})
|
|
|
|
(let unknownBuckets = subtractLists (attrNames org.buckets) auth.writeBuckets; in {
|
|
|
|
assertion = unknownBuckets == [];
|
|
|
|
message = "influxdb2: provision.organizations.${orgName}.auths.${authName}: Refers to invalid buckets in writeBuckets: ${toString unknownBuckets}";
|
|
|
|
})
|
|
|
|
]
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
services.influxdb2.provision = mkIf cfg.provision.enable {
|
|
|
|
organizations.${cfg.provision.initialSetup.organization} = {
|
|
|
|
buckets.${cfg.provision.initialSetup.bucket} = {
|
|
|
|
inherit (cfg.provision.initialSetup) retention;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
users.${cfg.provision.initialSetup.username} = {
|
|
|
|
inherit (cfg.provision.initialSetup) passwordFile;
|
|
|
|
};
|
|
|
|
};
|
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}"
|
|
|
|
];
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2024-09-26 11:04:55 +00:00
|
|
|
ExecStartPost = [
|
|
|
|
waitUntilServiceIsReady
|
|
|
|
] ++ (lib.optionals cfg.provision.enable (
|
2023-10-09 19:29:22 +00:00
|
|
|
[provisioningScript] ++
|
|
|
|
# Only the restarter runs with elevated privileges
|
|
|
|
optional anyAuthDefined "+${restarterScript}"
|
2024-09-26 11:04:55 +00:00
|
|
|
));
|
2021-08-10 14:31:46 +00:00
|
|
|
};
|
2023-08-22 20:05:09 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
path = [
|
|
|
|
pkgs.influxdb2-cli
|
|
|
|
pkgs.jq
|
|
|
|
];
|
2023-08-22 20:05:09 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
# Mark if this is the first startup so postStart can do the initial setup.
|
|
|
|
# Also extract any token secret mappings and apply them if this isn't the first start.
|
|
|
|
preStart = let
|
|
|
|
tokenPaths = listToAttrs (flatten
|
|
|
|
# For all organizations
|
|
|
|
(flip mapAttrsToList cfg.provision.organizations
|
|
|
|
# For each contained token that has a token file
|
|
|
|
(_: org: flip mapAttrsToList (filterAttrs (_: x: x.tokenFile != null) org.auths)
|
|
|
|
# Collect id -> tokenFile for the mapping
|
|
|
|
(_: auth: nameValuePair auth.id auth.tokenFile))));
|
|
|
|
tokenMappings = pkgs.writeText "token_mappings.json" (builtins.toJSON tokenPaths);
|
|
|
|
in mkIf cfg.provision.enable ''
|
2023-08-22 20:05:09 +00:00
|
|
|
if ! test -e "$STATE_DIRECTORY/influxd.bolt"; then
|
|
|
|
touch "$STATE_DIRECTORY/.first_startup"
|
2023-10-09 19:29:22 +00:00
|
|
|
else
|
|
|
|
# Manipulate provisioned api tokens if necessary
|
|
|
|
${getExe pkgs.influxdb2-token-manipulator} "$STATE_DIRECTORY/influxd.bolt" ${tokenMappings}
|
2023-08-22 20:05:09 +00:00
|
|
|
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
|
|
|
}
|