I tried to make a NixOS module for influxdb2 since it is missing in nixpkgs
. Here is my attempt based on the current module for influxdb:
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.influxdb2;
configOptions = recursiveUpdate {
bolt-path = "${cfg.dataDir}/bolt";
data = {
engine-path = "${cfg.dataDir}/data";
max-wal-size = 104857600;
wal-enable-logging = true;
wal-flush-interval = "10m";
wal-partition-flush-delay = "2s";
};
cluster = {
shard-writer-timeout = "5s";
write-timeout = "5s";
};
retention = {
enabled = true;
storage-retention-check-interval = "30m";
};
http = {
enabled = true;
http-bind-address = ":8086";
pprof-disabled = false;
};
admin = {
enabled = true;
bind-address = ":8083";
https-enabled = false;
};
hinted-handoff = {
enabled = true;
dir = "${cfg.dataDir}/hh";
max-size = 1073741824;
max-age = "168h";
retry-rate-limit = 0;
retry-interval = "1s";
};
} cfg.extraConfig;
configFile = pkgs.runCommand "config.toml" {
buildInputs = [ pkgs.remarshal ];
preferLocalBuild = true;
} ''
remarshal -if json -of toml \
< ${pkgs.writeText "config.json" (builtins.toJSON configOptions)} \
> $out
'';
in
{
###### interface
options = {
services.influxdb2 = {
enable = mkOption {
default = false;
description = "Whether to enable the influxdb2 server";
type = types.bool;
};
package = mkOption {
default = pkgs.influxdb2;
defaultText = "pkgs.influxdb2";
description = "Which influxdb2 derivation to use";
type = types.package;
};
user = mkOption {
default = "influxdb";
description = "User account under which influxdb2 runs";
type = types.str;
};
group = mkOption {
default = "influxdb";
description = "Group under which influxdb2 runs";
type = types.str;
};
dataDir = mkOption {
default = "/var/db/influxdb2";
description = "Data directory for influxd data files.";
type = types.path;
};
extraConfig = mkOption {
default = {};
description = "Extra configuration options for influxdb2";
type = types.attrs;
};
};
};
###### implementation
config = mkIf config.services.influxdb2.enable {
# systemd.tmpfiles.rules = [
# "d '${cfg.dataDir}' 0770 ${cfg.user} ${cfg.group} - -"
# ];
environment.systemPackages = [
cfg.package
];
environment.etc."influxdb2/influxdb2.conf".source = configFile;
environment.variables = mkForce {
INFLUXD_CONFIG_PATH = "/etc/influxdb2/influxdb2.conf";
};
systemd.services.influxdb2 = {
description = "InfluxDB V2 Server";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
environment = {
INFLUXD_CONFIG_PATH = configFile;
};
serviceConfig = {
# DynamicUser = true;
# StateDirectory = cfg.dataDir;
ExecStartPre =
"+" + pkgs.writeScript "influxdb2-prep"
''
#!${pkgs.bash}/bin/bash
mkdir -p ${cfg.dataDir}
chown -R influxdb:influxdb ${cfg.dataDir}
'';
ExecStart = ''
${cfg.package}/bin/influxd
'';
User = cfg.user;
Group = cfg.group;
};
postStart =
let
scheme = if configOptions.http.enabled then "-k https" else "http";
bindAddr = (ba: if hasPrefix ":" ba then "127.0.0.1${ba}" else "${ba}")(toString configOptions.http.http-bind-address);
in
mkBefore ''
until ${pkgs.curl.bin}/bin/curl -s -o /dev/null ${scheme}://${bindAddr}/ping; do
sleep 1;
done
'';
};
users.users = optionalAttrs (cfg.user == "influxdb") {
influxdb = {
uid = config.ids.uids.influxdb;
description = "Influxdb V2 daemon user";
};
};
users.groups = optionalAttrs (cfg.group == "influxdb") {
influxdb.gid = config.ids.gids.influxdb;
};
};
}
somehow, I couldn’t get rid of the error
myhost influxd[5007]: Error: unable to create directory /data/influxdb2/bolt: mkdir /data/influxdb2: permission denied
where /data/influxdb2
is my dataDir
value.
If I don’t set dataDir
, the error is
Error: mkdir /var/empty/.influxdbv2: operation not permitted
which is very confusing to me.
It would be great if someone can make it work and add it to nixpkgs.