1 { config, pkgs, lib, ... }:
3 cfg = config.services.collectd;
5 baseDirLine = ''BaseDir "${cfg.dataDir}"'';
6 unvalidated_conf = pkgs.writeText "collectd-unvalidated.conf" cfg.extraConfig;
8 conf = if cfg.validateConfig then
9 pkgs.runCommand "collectd.conf" {} ''
10 echo testing ${unvalidated_conf}
11 cp ${unvalidated_conf} collectd.conf
12 # collectd -t fails if BaseDir does not exist.
13 substituteInPlace collectd.conf --replace ${lib.escapeShellArgs [ baseDirLine ]} 'BaseDir "."'
14 ${package}/bin/collectd -t -C collectd.conf
15 cp ${unvalidated_conf} $out
16 '' else unvalidated_conf;
19 if cfg.buildMinimalPackage
23 minimalPackage = cfg.package.override {
24 enabledPlugins = [ "syslog" ] ++ builtins.attrNames cfg.plugins;
28 options.services.collectd = with lib.types; {
29 enable = lib.mkEnableOption "collectd agent";
31 validateConfig = lib.mkOption {
34 Validate the syntax of collectd configuration file at build time.
35 Disable this if you use the Include directive on files unavailable in
36 the build sandbox, or when cross-compiling.
41 package = lib.mkPackageOption pkgs "collectd" { };
43 buildMinimalPackage = lib.mkOption {
46 Build a minimal collectd package with only the configured `services.collectd.plugins`
54 User under which to run collectd.
59 dataDir = lib.mkOption {
60 default = "/var/lib/collectd";
62 Data directory for collectd agent.
67 autoLoadPlugin = lib.mkOption {
70 Enable plugin autoloading.
75 include = lib.mkOption {
78 Additional paths to load config from.
83 plugins = lib.mkOption {
85 example = { cpu = ""; memory = ""; network = "Server 192.168.1.1 25826"; };
87 Attribute set of plugin names to plugin config segments
92 extraConfig = lib.mkOption {
95 Extra configuration for collectd. Use mkBefore to add lines before the
96 default config, and mkAfter to add them below.
103 config = lib.mkIf cfg.enable {
104 # 1200 is after the default (1000) but before mkAfter (1500).
105 services.collectd.extraConfig = lib.mkOrder 1200 ''
107 AutoLoadPlugin ${lib.boolToString cfg.autoLoadPlugin}
108 Hostname "${config.networking.hostName}"
116 ${lib.concatStrings (lib.mapAttrsToList (plugin: pluginConfig: ''
123 ${lib.concatMapStrings (f: ''
128 systemd.tmpfiles.rules = [
129 "d '${cfg.dataDir}' - ${cfg.user} - - -"
132 systemd.services.collectd = {
133 description = "Collectd Monitoring Agent";
134 after = [ "network.target" ];
135 wantedBy = [ "multi-user.target" ];
138 ExecStart = "${package}/sbin/collectd -C ${conf} -f";
140 Restart = "on-failure";
145 users.users = lib.optionalAttrs (cfg.user == "collectd") {
152 users.groups = lib.optionalAttrs (cfg.user == "collectd") {