Skip to content

Commit

Permalink
Extend yamllint hook with more options
Browse files Browse the repository at this point in the history
  • Loading branch information
totoroot committed Feb 16, 2024
1 parent 5df5a70 commit 2ce4972
Showing 1 changed file with 54 additions and 14 deletions.
68 changes: 54 additions & 14 deletions modules/hooks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
};

Expand Down Expand Up @@ -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}";
};
Expand All @@ -2531,6 +2572,5 @@ in
entry = "${tools.zprint}/bin/zprint '{:search-config? true}' -w";
types_or = [ "clojure" "clojurescript" "edn" ];
};

};
}

0 comments on commit 2ce4972

Please sign in to comment.