From 2ce4972a298aff82fdf24e65a8f54a0c8b8beaec Mon Sep 17 00:00:00 2001 From: Matthias Thym Date: Fri, 16 Feb 2024 11:35:26 +0100 Subject: [PATCH] Extend yamllint hook with more options --- modules/hooks.nix | 68 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 66e37cbf..a20e0528 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -1227,19 +1227,53 @@ in }; yamllint = { - relaxed = mkOption { - type = types.bool; - description = lib.mdDoc "Whether to use the relaxed configuration."; - default = false; - }; + # `list-files` is not useful for a pre-commit hook as it always exits with exit code 0 + # `no-warnings` is not useful for a pre-commit hook as it exits with exit code 2 and the hook + # therefore fails when warnings level problems are detected but there is no output + configuration = mkOption { + type = types.str; + description = lib.mdDoc "Multiline-string configuration passed as config file. If set, configuration file set in `yamllint.settings.configPath` gets ignored."; + default = ""; + example = '' + --- + + extends: relaxed + rules: + indentation: enable + ''; + }; + configData = mkOption { + type = types.str; + description = lib.mdDoc "Serialized YAML object describing the configuration."; + default = ""; + example = "{extends: relaxed, rules: {line-length: {max: 120}}}"; + }; configPath = mkOption { type = types.str; - description = lib.mdDoc "Path to the YAML configuration file."; - # an empty string translates to use default configuration of the - # underlying yamllint binary + description = lib.mdDoc "Path to a custom configuration file."; + # An empty string translates to yamllint looking for a configuration file in the + # following locations (by order of preference): + # a file named .yamllint, .yamllint.yaml or .yamllint.yml in the current working directory + # a filename referenced by $YAMLLINT_CONFIG_FILE, if set + # a file named $XDG_CONFIG_HOME/yamllint/config or ~/.config/yamllint/config, if present default = ""; }; + format = mkOption { + type = types.enum [ "parsable" "standard" "colored" "github" "auto" ]; + description = lib.mdDoc "Format for parsing output."; + default = "auto"; + }; + preset = mkOption { + type = types.enum [ "default" "relaxed" ]; + description = lib.mdDoc "The configuration preset to use."; + default = "default"; + }; + strict = mkOption { + type = types.bool; + description = lib.mdDoc "Return non-zero exit code on warnings as well as errors."; + default = true; + }; }; }; @@ -2512,15 +2546,22 @@ in yamllint = { name = "yamllint"; - description = "Yaml linter."; + description = "A linter for YAML files."; types = [ "file" "yaml" ]; entry = let + configFile = builtins.toFile "yamllint.yaml" "${settings.yamllint.configuration}"; cmdArgs = - mkCmdArgs [ - [ (settings.yamllint.relaxed) "-d relaxed" ] - [ (settings.yamllint.configPath != "") "-c ${settings.yamllint.configPath}" ] - ]; + mkCmdArgs + (with settings.yamllint; [ + # Priorize multiline configuration over serialized configuration and configuration file + [ (configuration != "") "--config-file ${configFile}" ] + [ (configData != "" && configuration == "") "--config-data \"${configData}\"" ] + [ (configPath != "" && configData == "" && configuration == "" && preset == "default") "--config-file ${configPath}" ] + [ (format != "auto") "--format ${format}" ] + [ (preset != "default" && configuration == "") "--config-data ${preset}" ] + [ strict "--strict" ] + ]); in "${tools.yamllint}/bin/yamllint ${cmdArgs}"; }; @@ -2531,6 +2572,5 @@ in entry = "${tools.zprint}/bin/zprint '{:search-config? true}' -w"; types_or = [ "clojure" "clojurescript" "edn" ]; }; - }; }