nixos/collectd: validate config file syntax at build time

This commit is contained in:
Guillaume Girol 2021-12-22 12:00:00 +00:00 committed by Bjørn Forsman
parent b55a253e15
commit d96a3994cc
2 changed files with 29 additions and 1 deletions

View file

@ -5,7 +5,7 @@ with lib;
let
cfg = config.services.collectd;
conf = pkgs.writeText "collectd.conf" ''
unvalidated_conf = pkgs.writeText "collectd-unvalidated.conf" ''
BaseDir "${cfg.dataDir}"
AutoLoadPlugin ${boolToString cfg.autoLoadPlugin}
Hostname "${config.networking.hostName}"
@ -30,6 +30,15 @@ let
${cfg.extraConfig}
'';
conf = if cfg.validateConfig then
pkgs.runCommand "collectd.conf" {} ''
echo testing ${unvalidated_conf}
# collectd -t fails if BaseDir does not exist.
sed '1s/^BaseDir.*$/BaseDir "."/' ${unvalidated_conf} > collectd.conf
${package}/bin/collectd -t -C collectd.conf
cp ${unvalidated_conf} $out
'' else unvalidated_conf;
package =
if cfg.buildMinimalPackage
then minimalPackage
@ -43,6 +52,16 @@ in {
options.services.collectd = with types; {
enable = mkEnableOption "collectd agent";
validateConfig = mkOption {
default = true;
description = ''
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";

View file

@ -17,6 +17,15 @@ stdenv.mkDerivation rec {
sha256 = "1mh97afgq6qgmpvpr84zngh58m0sl1b4wimqgvvk376188q09bjv";
};
patches = [
# fix -t never printing syntax errors
# should be included in next release
(fetchpatch {
url = "https://github.com/collectd/collectd/commit/3f575419e7ccb37a3b10ecc82adb2e83ff2826e1.patch";
sha256 = "0jwjdlfl0dp7mlbwygp6h0rsbaqfbgfm5z07lr5l26z6hhng2h2y";
})
];
nativeBuildInputs = [ pkg-config autoreconfHook ];
buildInputs = [
libtool