nixos/services.netdata: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-30 00:47:00 +02:00
parent c93d8f88a7
commit f88528a137

View File

@ -1,7 +1,4 @@
{ config, pkgs, lib, ... }: { config, pkgs, lib, ... }:
with lib;
let let
cfg = config.services.netdata; cfg = config.services.netdata;
@ -25,7 +22,7 @@ let
configDirectory = pkgs.runCommand "netdata-config-d" { } '' configDirectory = pkgs.runCommand "netdata-config-d" { } ''
mkdir $out mkdir $out
${concatStringsSep "\n" (mapAttrsToList (path: file: '' ${lib.concatStringsSep "\n" (lib.mapAttrsToList (path: file: ''
mkdir -p "$out/$(dirname ${path})" mkdir -p "$out/$(dirname ${path})"
ln -s "${file}" "$out/${path}" ln -s "${file}" "$out/${path}"
'') cfg.configDir)} '') cfg.configDir)}
@ -34,7 +31,7 @@ let
localConfig = { localConfig = {
global = { global = {
"config directory" = "/etc/netdata/conf.d"; "config directory" = "/etc/netdata/conf.d";
"plugins directory" = concatStringsSep " " plugins; "plugins directory" = lib.concatStringsSep " " plugins;
}; };
web = { web = {
"web files owner" = "root"; "web files owner" = "root";
@ -45,7 +42,7 @@ let
"use unified cgroups" = "yes"; "use unified cgroups" = "yes";
}; };
}; };
mkConfig = generators.toINI {} (recursiveUpdate localConfig cfg.config); mkConfig = lib.generators.toINI {} (lib.recursiveUpdate localConfig cfg.config);
configFile = pkgs.writeText "netdata.conf" (if cfg.configText != null then cfg.configText else mkConfig); configFile = pkgs.writeText "netdata.conf" (if cfg.configText != null then cfg.configText else mkConfig);
defaultUser = "netdata"; defaultUser = "netdata";
@ -54,24 +51,24 @@ let
in { in {
options = { options = {
services.netdata = { services.netdata = {
enable = mkEnableOption "netdata"; enable = lib.mkEnableOption "netdata";
package = mkPackageOption pkgs "netdata" { }; package = lib.mkPackageOption pkgs "netdata" { };
user = mkOption { user = lib.mkOption {
type = types.str; type = lib.types.str;
default = "netdata"; default = "netdata";
description = "User account under which netdata runs."; description = "User account under which netdata runs.";
}; };
group = mkOption { group = lib.mkOption {
type = types.str; type = lib.types.str;
default = "netdata"; default = "netdata";
description = "Group under which netdata runs."; description = "Group under which netdata runs.";
}; };
configText = mkOption { configText = lib.mkOption {
type = types.nullOr types.lines; type = lib.types.nullOr lib.types.lines;
description = "Verbatim netdata.conf, cannot be combined with config."; description = "Verbatim netdata.conf, cannot be combined with config.";
default = null; default = null;
example = '' example = ''
@ -83,26 +80,26 @@ in {
}; };
python = { python = {
enable = mkOption { enable = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = true; default = true;
description = '' description = ''
Whether to enable python-based plugins Whether to enable python-based plugins
''; '';
}; };
recommendedPythonPackages = mkOption { recommendedPythonPackages = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Whether to enable a set of recommended Python plugins Whether to enable a set of recommended Python plugins
by installing extra Python packages. by installing extra Python packages.
''; '';
}; };
extraPackages = mkOption { extraPackages = lib.mkOption {
type = types.functionTo (types.listOf types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = ps: []; default = ps: [];
defaultText = literalExpression "ps: []"; defaultText = lib.literalExpression "ps: []";
example = literalExpression '' example = lib.literalExpression ''
ps: [ ps: [
ps.psycopg2 ps.psycopg2
ps.docker ps.docker
@ -116,10 +113,10 @@ in {
}; };
}; };
extraPluginPaths = mkOption { extraPluginPaths = lib.mkOption {
type = types.listOf types.path; type = lib.types.listOf lib.types.path;
default = [ ]; default = [ ];
example = literalExpression '' example = lib.literalExpression ''
[ "/path/to/plugins.d" ] [ "/path/to/plugins.d" ]
''; '';
description = '' description = ''
@ -134,11 +131,11 @@ in {
''; '';
}; };
config = mkOption { config = lib.mkOption {
type = types.attrsOf types.attrs; type = lib.types.attrsOf lib.types.attrs;
default = {}; default = {};
description = "netdata.conf configuration as nix attributes. cannot be combined with configText."; description = "netdata.conf configuration as nix attributes. cannot be combined with configText.";
example = literalExpression '' example = lib.literalExpression ''
global = { global = {
"debug log" = "syslog"; "debug log" = "syslog";
"access log" = "syslog"; "access log" = "syslog";
@ -147,8 +144,8 @@ in {
''; '';
}; };
configDir = mkOption { configDir = lib.mkOption {
type = types.attrsOf types.path; type = lib.types.attrsOf lib.types.path;
default = {}; default = {};
description = '' description = ''
Complete netdata config directory except netdata.conf. Complete netdata config directory except netdata.conf.
@ -159,7 +156,7 @@ in {
Its value is the absolute path and must be readable by netdata. Its value is the absolute path and must be readable by netdata.
Cannot be combined with configText. Cannot be combined with configText.
''; '';
example = literalExpression '' example = lib.literalExpression ''
"health_alarm_notify.conf" = pkgs.writeText "health_alarm_notify.conf" ''' "health_alarm_notify.conf" = pkgs.writeText "health_alarm_notify.conf" '''
sendmail="/path/to/sendmail" sendmail="/path/to/sendmail"
'''; ''';
@ -167,8 +164,8 @@ in {
''; '';
}; };
claimTokenFile = mkOption { claimTokenFile = lib.mkOption {
type = types.nullOr types.path; type = lib.types.nullOr lib.types.path;
default = null; default = null;
description = '' description = ''
If set, automatically registers the agent using the given claim token If set, automatically registers the agent using the given claim token
@ -176,8 +173,8 @@ in {
''; '';
}; };
enableAnalyticsReporting = mkOption { enableAnalyticsReporting = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Enable reporting of anonymous usage statistics to Netdata Inc. via either Enable reporting of anonymous usage statistics to Netdata Inc. via either
@ -187,8 +184,8 @@ in {
''; '';
}; };
deadlineBeforeStopSec = mkOption { deadlineBeforeStopSec = lib.mkOption {
type = types.int; type = lib.types.int;
default = 120; default = 120;
description = '' description = ''
In order to detect when netdata is misbehaving, we run a concurrent task pinging netdata (wait-for-netdata-up) In order to detect when netdata is misbehaving, we run a concurrent task pinging netdata (wait-for-netdata-up)
@ -203,7 +200,7 @@ in {
}; };
}; };
config = mkIf cfg.enable { config = lib.mkIf cfg.enable {
assertions = assertions =
[ { assertion = cfg.config != {} -> cfg.configText == null ; [ { assertion = cfg.config != {} -> cfg.configText == null ;
message = "Cannot specify both config and configText"; message = "Cannot specify both config and configText";
@ -220,7 +217,7 @@ in {
ps.netdata-pandas ps.netdata-pandas
]); ]);
services.netdata.configDir.".opt-out-from-anonymous-statistics" = mkIf (!cfg.enableAnalyticsReporting) (pkgs.writeText ".opt-out-from-anonymous-statistics" ""); services.netdata.configDir.".opt-out-from-anonymous-statistics" = lib.mkIf (!cfg.enableAnalyticsReporting) (pkgs.writeText ".opt-out-from-anonymous-statistics" "");
environment.etc."netdata/netdata.conf".source = configFile; environment.etc."netdata/netdata.conf".source = configFile;
environment.etc."netdata/conf.d".source = configDirectory; environment.etc."netdata/conf.d".source = configDirectory;
@ -381,7 +378,7 @@ in {
permissions = "u+rx,g+x,o-rwx"; permissions = "u+rx,g+x,o-rwx";
}; };
} // optionalAttrs (cfg.package.withIpmi) { } // lib.optionalAttrs (cfg.package.withIpmi) {
"freeipmi.plugin" = { "freeipmi.plugin" = {
source = "${cfg.package}/libexec/netdata/plugins.d/freeipmi.plugin.org"; source = "${cfg.package}/libexec/netdata/plugins.d/freeipmi.plugin.org";
capabilities = "cap_dac_override,cap_fowner+ep"; capabilities = "cap_dac_override,cap_fowner+ep";
@ -389,7 +386,7 @@ in {
group = cfg.group; group = cfg.group;
permissions = "u+rx,g+x,o-rwx"; permissions = "u+rx,g+x,o-rwx";
}; };
} // optionalAttrs (cfg.package.withNetworkViewer) { } // lib.optionalAttrs (cfg.package.withNetworkViewer) {
"network-viewer.plugin" = { "network-viewer.plugin" = {
source = "${cfg.package}/libexec/netdata/plugins.d/network-viewer.plugin.org"; source = "${cfg.package}/libexec/netdata/plugins.d/network-viewer.plugin.org";
capabilities = "cap_sys_admin,cap_dac_read_search,cap_sys_ptrace+ep"; capabilities = "cap_sys_admin,cap_dac_read_search,cap_sys_ptrace+ep";
@ -404,7 +401,7 @@ in {
{ domain = "netdata"; type = "hard"; item = "nofile"; value = "30000"; } { domain = "netdata"; type = "hard"; item = "nofile"; value = "30000"; }
]; ];
users.users = optionalAttrs (cfg.user == defaultUser) { users.users = lib.optionalAttrs (cfg.user == defaultUser) {
${defaultUser} = { ${defaultUser} = {
group = defaultUser; group = defaultUser;
isSystemUser = true; isSystemUser = true;
@ -413,7 +410,7 @@ in {
}; };
}; };
users.groups = optionalAttrs (cfg.group == defaultUser) { users.groups = lib.optionalAttrs (cfg.group == defaultUser) {
${defaultUser} = { }; ${defaultUser} = { };
}; };