nixpkgs/nixos/modules/services/monitoring/collectd.nix
pennae 2e751c0772 treewide: automatically md-convert option descriptions
the conversion procedure is simple:

 - find all things that look like options, ie calls to either `mkOption`
   or `lib.mkOption` that take an attrset. remember the attrset as the
   option
 - for all options, find a `description` attribute who's value is not a
   call to `mdDoc` or `lib.mdDoc`
 - textually convert the entire value of the attribute to MD with a few
   simple regexes (the set from mdize-module.sh)
 - if the change produced a change in the manual output, discard
 - if the change kept the manual unchanged, add some text to the
   description to make sure we've actually found an option. if the
   manual changes this time, keep the converted description

this procedure converts 80% of nixos options to markdown. around 2000
options remain to be inspected, but most of those fail the "does not
change the manual output check": currently the MD conversion process
does not faithfully convert docbook tags like <code> and <package>, so
any option using such tags will not be converted at all.
2022-07-30 15:16:34 +02:00

167 lines
4.2 KiB
Nix

{ config, pkgs, lib, ... }:
with lib;
let
cfg = config.services.collectd;
baseDirLine = ''BaseDir "${cfg.dataDir}"'';
unvalidated_conf = pkgs.writeText "collectd-unvalidated.conf" cfg.extraConfig;
conf = if cfg.validateConfig then
pkgs.runCommand "collectd.conf" {} ''
echo testing ${unvalidated_conf}
cp ${unvalidated_conf} collectd.conf
# collectd -t fails if BaseDir does not exist.
substituteInPlace collectd.conf --replace ${lib.escapeShellArgs [ baseDirLine ]} 'BaseDir "."'
${package}/bin/collectd -t -C collectd.conf
cp ${unvalidated_conf} $out
'' else unvalidated_conf;
package =
if cfg.buildMinimalPackage
then minimalPackage
else cfg.package;
minimalPackage = cfg.package.override {
enabledPlugins = [ "syslog" ] ++ builtins.attrNames cfg.plugins;
};
in {
options.services.collectd = with types; {
enable = mkEnableOption "collectd agent";
validateConfig = mkOption {
default = true;
description = lib.mdDoc ''
Validate the syntax of collectd configuration file at build time.
Disable this if you use the Include directive on files unavailable in
the build sandbox, or when cross-compiling.
'';
type = types.bool;
};
package = mkOption {
default = pkgs.collectd;
defaultText = literalExpression "pkgs.collectd";
description = lib.mdDoc ''
Which collectd package to use.
'';
type = types.package;
};
buildMinimalPackage = mkOption {
default = false;
description = ''
Build a minimal collectd package with only the configured `services.collectd.plugins`
'';
type = bool;
};
user = mkOption {
default = "collectd";
description = lib.mdDoc ''
User under which to run collectd.
'';
type = nullOr str;
};
dataDir = mkOption {
default = "/var/lib/collectd";
description = lib.mdDoc ''
Data directory for collectd agent.
'';
type = path;
};
autoLoadPlugin = mkOption {
default = false;
description = lib.mdDoc ''
Enable plugin autoloading.
'';
type = bool;
};
include = mkOption {
default = [];
description = lib.mdDoc ''
Additional paths to load config from.
'';
type = listOf str;
};
plugins = mkOption {
default = {};
example = { cpu = ""; memory = ""; network = "Server 192.168.1.1 25826"; };
description = lib.mdDoc ''
Attribute set of plugin names to plugin config segments
'';
type = attrsOf lines;
};
extraConfig = mkOption {
default = "";
description = lib.mdDoc ''
Extra configuration for collectd. Use mkBefore to add lines before the
default config, and mkAfter to add them below.
'';
type = lines;
};
};
config = mkIf cfg.enable {
# 1200 is after the default (1000) but before mkAfter (1500).
services.collectd.extraConfig = lib.mkOrder 1200 ''
${baseDirLine}
AutoLoadPlugin ${boolToString cfg.autoLoadPlugin}
Hostname "${config.networking.hostName}"
LoadPlugin syslog
<Plugin "syslog">
LogLevel "info"
NotifyLevel "OKAY"
</Plugin>
${concatStrings (mapAttrsToList (plugin: pluginConfig: ''
LoadPlugin ${plugin}
<Plugin "${plugin}">
${pluginConfig}
</Plugin>
'') cfg.plugins)}
${concatMapStrings (f: ''
Include "${f}"
'') cfg.include}
'';
systemd.tmpfiles.rules = [
"d '${cfg.dataDir}' - ${cfg.user} - - -"
];
systemd.services.collectd = {
description = "Collectd Monitoring Agent";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${package}/sbin/collectd -C ${conf} -f";
User = cfg.user;
Restart = "on-failure";
RestartSec = 3;
};
};
users.users = optionalAttrs (cfg.user == "collectd") {
collectd = {
isSystemUser = true;
group = "collectd";
};
};
users.groups = optionalAttrs (cfg.user == "collectd") {
collectd = {};
};
};
}