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 (lib.mdDoc "collectd agent");
34 validateConfig = mkOption {
36 description = lib.mdDoc ''
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.
45 default = pkgs.collectd;
46 defaultText = literalExpression "pkgs.collectd";
47 description = lib.mdDoc ''
48 Which collectd package to use.
53 buildMinimalPackage = mkOption {
55 description = lib.mdDoc ''
56 Build a minimal collectd package with only the configured `services.collectd.plugins`
63 description = lib.mdDoc ''
64 User under which to run collectd.
70 default = "/var/lib/collectd";
71 description = lib.mdDoc ''
72 Data directory for collectd agent.
77 autoLoadPlugin = mkOption {
79 description = lib.mdDoc ''
80 Enable plugin autoloading.
87 description = lib.mdDoc ''
88 Additional paths to load config from.
95 example = { cpu = ""; memory = ""; network = "Server 192.168.1.1 25826"; };
96 description = lib.mdDoc ''
97 Attribute set of plugin names to plugin config segments
102 extraConfig = mkOption {
104 description = lib.mdDoc ''
105 Extra configuration for collectd. Use mkBefore to add lines before the
106 default config, and mkAfter to add them below.
113 config = mkIf cfg.enable {
114 # 1200 is after the default (1000) but before mkAfter (1500).
115 services.collectd.extraConfig = lib.mkOrder 1200 ''
117 AutoLoadPlugin ${boolToString cfg.autoLoadPlugin}
118 Hostname "${config.networking.hostName}"
126 ${concatStrings (mapAttrsToList (plugin: pluginConfig: ''
133 ${concatMapStrings (f: ''
138 systemd.tmpfiles.rules = [
139 "d '${cfg.dataDir}' - ${cfg.user} - - -"
142 systemd.services.collectd = {
143 description = "Collectd Monitoring Agent";
144 after = [ "network.target" ];
145 wantedBy = [ "multi-user.target" ];
148 ExecStart = "${package}/sbin/collectd -C ${conf} -f";
150 Restart = "on-failure";
155 users.users = optionalAttrs (cfg.user == "collectd") {
162 users.groups = optionalAttrs (cfg.user == "collectd") {