1 { config, pkgs, lib, ... }:
6 cfg = config.services.collectd;
8 baseDirLine = ''BaseDir "${cfg.dataDir}"'';
9 unvalidated_conf = pkgs.writeText "collectd-unvalidated.conf" cfg.extraConfig;
11 conf = if cfg.validateConfig then
12 pkgs.runCommand "collectd.conf" {} ''
13 echo testing ${unvalidated_conf}
14 cp ${unvalidated_conf} collectd.conf
15 # collectd -t fails if BaseDir does not exist.
16 substituteInPlace collectd.conf --replace ${lib.escapeShellArgs [ baseDirLine ]} 'BaseDir "."'
17 ${package}/bin/collectd -t -C collectd.conf
18 cp ${unvalidated_conf} $out
19 '' else unvalidated_conf;
22 if cfg.buildMinimalPackage
26 minimalPackage = cfg.package.override {
27 enabledPlugins = [ "syslog" ] ++ builtins.attrNames cfg.plugins;
31 options.services.collectd = with types; {
32 enable = mkEnableOption "collectd agent";
34 validateConfig = mkOption {
37 Validate the syntax of collectd configuration file at build time.
38 Disable this if you use the Include directive on files unavailable in
39 the build sandbox, or when cross-compiling.
44 package = mkPackageOption pkgs "collectd" { };
46 buildMinimalPackage = mkOption {
49 Build a minimal collectd package with only the configured `services.collectd.plugins`
57 User under which to run collectd.
63 default = "/var/lib/collectd";
65 Data directory for collectd agent.
70 autoLoadPlugin = mkOption {
73 Enable plugin autoloading.
81 Additional paths to load config from.
88 example = { cpu = ""; memory = ""; network = "Server 192.168.1.1 25826"; };
90 Attribute set of plugin names to plugin config segments
95 extraConfig = mkOption {
98 Extra configuration for collectd. Use mkBefore to add lines before the
99 default config, and mkAfter to add them below.
106 config = mkIf cfg.enable {
107 # 1200 is after the default (1000) but before mkAfter (1500).
108 services.collectd.extraConfig = lib.mkOrder 1200 ''
110 AutoLoadPlugin ${boolToString cfg.autoLoadPlugin}
111 Hostname "${config.networking.hostName}"
119 ${concatStrings (mapAttrsToList (plugin: pluginConfig: ''
126 ${concatMapStrings (f: ''
131 systemd.tmpfiles.rules = [
132 "d '${cfg.dataDir}' - ${cfg.user} - - -"
135 systemd.services.collectd = {
136 description = "Collectd Monitoring Agent";
137 after = [ "network.target" ];
138 wantedBy = [ "multi-user.target" ];
141 ExecStart = "${package}/sbin/collectd -C ${conf} -f";
143 Restart = "on-failure";
148 users.users = optionalAttrs (cfg.user == "collectd") {
155 users.groups = optionalAttrs (cfg.user == "collectd") {