Skip to content

Commit 54189ae

Browse files
committed
Extend yamllint hook with more options
1 parent 7d47a32 commit 54189ae

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

modules/hooks.nix

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,19 +1520,53 @@ in
15201520
type = types.submodule {
15211521
imports = hookModule;
15221522
options.settings = {
1523-
relaxed = mkOption {
1524-
type = types.bool;
1525-
description = lib.mdDoc "Whether to use the relaxed configuration.";
1526-
default = false;
1527-
};
1523+
# `list-files` is not useful for a pre-commit hook as it always exits with exit code 0
1524+
# `no-warnings` is not useful for a pre-commit hook as it exits with exit code 2 and the hook
1525+
# therefore fails when warnings level problems are detected but there is no output
1526+
configuration = mkOption {
1527+
type = types.str;
1528+
description = lib.mdDoc "Multiline-string configuration passed as config file. If set, configuration file set in `yamllint.settings.configPath` gets ignored.";
1529+
default = "";
1530+
example = ''
1531+
---
1532+
1533+
extends: relaxed
15281534
1535+
rules:
1536+
indentation: enable
1537+
'';
1538+
};
1539+
configData = mkOption {
1540+
type = types.str;
1541+
description = lib.mdDoc "Serialized YAML object describing the configuration.";
1542+
default = "";
1543+
example = "{extends: relaxed, rules: {line-length: {max: 120}}}";
1544+
};
15291545
configPath = mkOption {
15301546
type = types.str;
1531-
description = lib.mdDoc "Path to the YAML configuration file.";
1532-
# an empty string translates to use default configuration of the
1533-
# underlying yamllint binary
1547+
description = lib.mdDoc "Path to a custom configuration file.";
1548+
# An empty string translates to yamllint looking for a configuration file in the
1549+
# following locations (by order of preference):
1550+
# a file named .yamllint, .yamllint.yaml or .yamllint.yml in the current working directory
1551+
# a filename referenced by $YAMLLINT_CONFIG_FILE, if set
1552+
# a file named $XDG_CONFIG_HOME/yamllint/config or ~/.config/yamllint/config, if present
15341553
default = "";
15351554
};
1555+
format = mkOption {
1556+
type = types.enum [ "parsable" "standard" "colored" "github" "auto" ];
1557+
description = lib.mdDoc "Format for parsing output.";
1558+
default = "auto";
1559+
};
1560+
preset = mkOption {
1561+
type = types.enum [ "default" "relaxed" ];
1562+
description = lib.mdDoc "The configuration preset to use.";
1563+
default = "default";
1564+
};
1565+
strict = mkOption {
1566+
type = types.bool;
1567+
description = lib.mdDoc "Return non-zero exit code on warnings as well as errors.";
1568+
default = true;
1569+
};
15361570
};
15371571
};
15381572
};
@@ -2974,16 +3008,23 @@ in
29743008
yamllint =
29753009
{
29763010
name = "yamllint";
2977-
description = "Yaml linter.";
3011+
description = "Linter for YAML files.";
29783012
types = [ "file" "yaml" ];
29793013
package = tools.yamllint;
29803014
entry =
29813015
let
3016+
configFile = builtins.toFile "yamllint.yaml" "${hooks.yamllint.settings.configuration}";
29823017
cmdArgs =
2983-
mkCmdArgs [
2984-
[ (hooks.yamllint.settings.relaxed) "-d relaxed" ]
2985-
[ (hooks.yamllint.settings.configPath != "") "-c ${hooks.yamllint.settings.configPath}" ]
2986-
];
3018+
mkCmdArgs
3019+
(with hooks.yamllint.settings; [
3020+
# Priorize multiline configuration over serialized configuration and configuration file
3021+
[ (configuration != "") "--config-file ${configFile}" ]
3022+
[ (configData != "" && configuration == "") "--config-data \"${configData}\"" ]
3023+
[ (configPath != "" && configData == "" && configuration == "" && preset == "default") "--config-file ${configPath}" ]
3024+
[ (format != "auto") "--format ${format}" ]
3025+
[ (preset != "default" && configuration == "") "--config-data ${preset}" ]
3026+
[ strict "--strict" ]
3027+
]);
29873028
in
29883029
"${hooks.yamllint.package}/bin/yamllint ${cmdArgs}";
29893030
};

0 commit comments

Comments
 (0)