Skip to content

Commit e35aed5

Browse files
authored
Merge pull request #398 from totoroot/extend-yamllint-hook
Extend `yamllint` hook
2 parents 642dce0 + 9715094 commit e35aed5

File tree

1 file changed

+79
-27
lines changed

1 file changed

+79
-27
lines changed

modules/hooks.nix

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
let
33
inherit (config) hooks tools settings;
44
cfg = config;
5-
inherit (lib) flatten mapAttrs mapAttrsToList mkDefault mkOption mkRenamedOptionModule types;
5+
inherit (lib) flatten mapAttrs mapAttrsToList mkDefault mkOption mkRemovedOptionModule mkRenamedOptionModule types;
66

77
cargoManifestPathArg =
88
lib.optionalString
@@ -27,16 +27,27 @@ in
2727
imports =
2828
# Rename `settings.<name>.package` to `hooks.<name>.package`
2929
map (name: mkRenamedOptionModule [ "settings" name "package" ] [ "hooks" name "package" ]) [ "alejandra" "eclint" "flynt" "mdl" "treefmt" ]
30-
# Manually rename options that had a package option
30+
# These options were renamed in 20fbe2c9731810b1020572a2cb6cbf64e3dd3873 to avoid shadowing
31+
++ map (name: mkRenamedOptionModule [ "settings" name "config" ] [ "hooks" name "settings" "configuration" ]) [ "lua-ls" "markdownlint" "typos" "vale" ]
32+
++ [
33+
(mkRemovedOptionModule [ "settings" "yamllint" "relaxed" ] ''
34+
This option has been removed. Use `hooks.yamllint.settings.preset = "relaxed"`.
35+
'')
36+
]
37+
# Manually rename options that had a package or a config option
3138
++ flatten (mapAttrsToList (name: map (o: mkRenamedOptionModule [ "settings" name o ] [ "hooks" name "settings" o ])) {
3239
"alejandra" = [ "check" "exclude" "threads" "verbosity" ];
3340
"eclint" = [ "fix" "summary" "color" "exclude" "verbosity" ];
3441
"flynt" = [ "aggressive" "binPath" "dry-run" "exclude" "fail-on-change" "line-length" "no-multiline" "quiet" "string" "transform-concats" "verbose" ];
3542
"mdl" = [ "configPath" "git-recurse" "ignore-front-matter" "json" "rules" "rulesets" "show-aliases" "warnings" "skip-default-ruleset" "style" "tags" "verbose" ];
43+
"lua-ls" = [ "checklevel" ];
44+
"typos" = [ "binary" "color" "configPath" "diff" "exclude" "format" "hidden" "ignored-words" "locale" "no-check-filenames" "no-check-files" "no-unicode" "quiet" "verbose" "write" ];
45+
"vale" = [ "configPath" "flags" ];
46+
"yamllint" = [ "configPath" ];
3647
})
3748
# Rename the remaining `settings.<name>` to `hooks.<name>.settings`
3849
++ map (name: mkRenamedOptionModule [ "settings" name ] [ "hooks" name "settings" ])
39-
[ "ansible-lint" "autoflake" "clippy" "cmake-format" "credo" "deadnix" "denofmt" "denolint" "dune-fmt" "eslint" "flake8" "headache" "hlint" "hpack" "isort" "latexindent" "lua-ls" "lychee" "markdownlint" "mkdocs-linkcheck" "mypy" "nixfmt" "ormolu" "php-cs-fixer" "phpcbf" "phpcs" "phpstan" "prettier" "psalm" "pylint" "pyright" "pyupgrade" "revive" "rome" "statix" "typos" "vale" "yamllint" ];
50+
[ "ansible-lint" "autoflake" "clippy" "cmake-format" "credo" "deadnix" "denofmt" "denolint" "dune-fmt" "eslint" "flake8" "headache" "hlint" "hpack" "isort" "latexindent" "lychee" "mkdocs-linkcheck" "mypy" "nixfmt" "ormolu" "php-cs-fixer" "phpcbf" "phpcs" "phpstan" "prettier" "psalm" "pylint" "pyright" "pyupgrade" "revive" "rome" "statix" ];
4051

4152
options.hookModule = lib.mkOption {
4253
type = types.deferredModule;
@@ -594,7 +605,7 @@ in
594605
"The diagnostic check level";
595606
default = "Warning";
596607
};
597-
config = mkOption {
608+
configuration = mkOption {
598609
type = types.attrs;
599610
description = lib.mdDoc
600611
"See https://github.com/LuaLS/lua-language-server/wiki/Configuration-File#luarcjson";
@@ -628,7 +639,7 @@ in
628639
type = types.submodule {
629640
imports = [ hookModule ];
630641
options.settings = {
631-
config =
642+
configuration =
632643
mkOption {
633644
type = types.attrs;
634645
description = lib.mdDoc
@@ -1416,7 +1427,7 @@ in
14161427
description = lib.mdDoc "When to use generate output.";
14171428
default = "auto";
14181429
};
1419-
config =
1430+
configuration =
14201431
mkOption {
14211432
type = types.str;
14221433
description = lib.mdDoc "Multiline-string configuration passed as config file. If set, config set in `typos.settings.configPath` gets ignored.";
@@ -1537,7 +1548,7 @@ in
15371548
type = types.submodule {
15381549
imports = [ hookModule ];
15391550
options.settings = {
1540-
config =
1551+
configuration =
15411552
mkOption {
15421553
type = types.str;
15431554
description = lib.mdDoc "Multiline-string configuration passed as config file.";
@@ -1568,19 +1579,53 @@ in
15681579
type = types.submodule {
15691580
imports = [ hookModule ];
15701581
options.settings = {
1571-
relaxed = mkOption {
1572-
type = types.bool;
1573-
description = lib.mdDoc "Whether to use the relaxed configuration.";
1574-
default = false;
1575-
};
1582+
# `list-files` is not useful for a pre-commit hook as it always exits with exit code 0
1583+
# `no-warnings` is not useful for a pre-commit hook as it exits with exit code 2 and the hook
1584+
# therefore fails when warnings level problems are detected but there is no output
1585+
configuration = mkOption {
1586+
type = types.str;
1587+
description = lib.mdDoc "Multiline-string configuration passed as config file. If set, configuration file set in `yamllint.settings.configPath` gets ignored.";
1588+
default = "";
1589+
example = ''
1590+
---
1591+
1592+
extends: relaxed
15761593
1594+
rules:
1595+
indentation: enable
1596+
'';
1597+
};
1598+
configData = mkOption {
1599+
type = types.str;
1600+
description = lib.mdDoc "Serialized YAML object describing the configuration.";
1601+
default = "";
1602+
example = "{extends: relaxed, rules: {line-length: {max: 120}}}";
1603+
};
15771604
configPath = mkOption {
15781605
type = types.str;
1579-
description = lib.mdDoc "Path to the YAML configuration file.";
1580-
# an empty string translates to use default configuration of the
1581-
# underlying yamllint binary
1606+
description = lib.mdDoc "Path to a custom configuration file.";
1607+
# An empty string translates to yamllint looking for a configuration file in the
1608+
# following locations (by order of preference):
1609+
# a file named .yamllint, .yamllint.yaml or .yamllint.yml in the current working directory
1610+
# a filename referenced by $YAMLLINT_CONFIG_FILE, if set
1611+
# a file named $XDG_CONFIG_HOME/yamllint/config or ~/.config/yamllint/config, if present
15821612
default = "";
15831613
};
1614+
format = mkOption {
1615+
type = types.enum [ "parsable" "standard" "colored" "github" "auto" ];
1616+
description = lib.mdDoc "Format for parsing output.";
1617+
default = "auto";
1618+
};
1619+
preset = mkOption {
1620+
type = types.enum [ "default" "relaxed" ];
1621+
description = lib.mdDoc "The configuration preset to use.";
1622+
default = "default";
1623+
};
1624+
strict = mkOption {
1625+
type = types.bool;
1626+
description = lib.mdDoc "Return non-zero exit code on warnings as well as errors.";
1627+
default = true;
1628+
};
15841629
};
15851630
};
15861631
};
@@ -2483,7 +2528,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
24832528
let
24842529
# .luarc.json has to be in a directory,
24852530
# or lua-language-server will hang forever.
2486-
luarc = pkgs.writeText ".luarc.json" (builtins.toJSON hooks.lua-ls.settings.config);
2531+
luarc = pkgs.writeText ".luarc.json" (builtins.toJSON hooks.lua-ls.settings.configuration);
24872532
luarc-dir = pkgs.stdenv.mkDerivation {
24882533
name = "luarc";
24892534
unpackPhase = "true";
@@ -2547,7 +2592,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
25472592
name = "markdownlint";
25482593
description = "Style checker and linter for markdown files.";
25492594
package = tools.markdownlint-cli;
2550-
entry = "${hooks.markdownlint.package}/bin/markdownlint -c ${pkgs.writeText "markdownlint.json" (builtins.toJSON hooks.markdownlint.settings.config)}";
2595+
entry = "${hooks.markdownlint.package}/bin/markdownlint -c ${pkgs.writeText "markdownlint.json" (builtins.toJSON hooks.markdownlint.settings.configuration)}";
25512596
files = "\\.md$";
25522597
};
25532598
mdl =
@@ -3262,15 +3307,15 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
32623307
entry =
32633308
let
32643309
# Concatenate config in config file with section for ignoring words generated from list of words to ignore
3265-
config = "${hooks.typos.settings.config}" + lib.strings.optionalString (hooks.typos.settings.ignored-words != [ ]) "\n\[default.extend-words\]" + lib.strings.concatMapStrings (x: "\n${x} = \"${x}\"") hooks.typos.settings.ignored-words;
3266-
configFile = builtins.toFile "typos-config.toml" config;
3310+
configuration = "${hooks.typos.settings.configuration}" + lib.strings.optionalString (hooks.typos.settings.ignored-words != [ ]) "\n\[default.extend-words\]" + lib.strings.concatMapStrings (x: "\n${x} = \"${x}\"") hooks.typos.settings.ignored-words;
3311+
configFile = builtins.toFile "typos-config.toml" configuration;
32673312
cmdArgs =
32683313
mkCmdArgs
32693314
(with hooks.typos.settings; [
32703315
[ binary "--binary" ]
32713316
[ (color != "auto") "--color ${color}" ]
3272-
[ (config != "") "--config ${configFile}" ]
3273-
[ (configPath != "" && config == "") "--config ${configPath}" ]
3317+
[ (configuration != "") "--config ${configFile}" ]
3318+
[ (configPath != "" && configuration == "") "--config ${configPath}" ]
32743319
[ diff "--diff" ]
32753320
[ (exclude != "") "--exclude ${exclude} --force-exclude" ]
32763321
[ (format != "long") "--format ${format}" ]
@@ -3306,7 +3351,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33063351
mkCmdArgs
33073352
(with hooks.vale.settings; [
33083353
[ (configPath != "") " --config ${configPath}" ]
3309-
[ (config != "" && configPath == "") " --config ${configFile}" ]
3354+
[ (configuration != "" && configPath == "") " --config ${configFile}" ]
33103355
]);
33113356
in
33123357
"${hooks.vale.package}/bin/vale${cmdArgs} ${hooks.vale.settings.flags}";
@@ -3315,16 +3360,23 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33153360
yamllint =
33163361
{
33173362
name = "yamllint";
3318-
description = "Yaml linter.";
3363+
description = "Linter for YAML files.";
33193364
types = [ "file" "yaml" ];
33203365
package = tools.yamllint;
33213366
entry =
33223367
let
3368+
configFile = builtins.toFile "yamllint.yaml" "${hooks.yamllint.settings.configuration}";
33233369
cmdArgs =
3324-
mkCmdArgs [
3325-
[ (hooks.yamllint.settings.relaxed) "-d relaxed" ]
3326-
[ (hooks.yamllint.settings.configPath != "") "-c ${hooks.yamllint.settings.configPath}" ]
3327-
];
3370+
mkCmdArgs
3371+
(with hooks.yamllint.settings; [
3372+
# Priorize multiline configuration over serialized configuration and configuration file
3373+
[ (configuration != "") "--config-file ${configFile}" ]
3374+
[ (configData != "" && configuration == "") "--config-data \"${configData}\"" ]
3375+
[ (configPath != "" && configData == "" && configuration == "" && preset == "default") "--config-file ${configPath}" ]
3376+
[ (format != "auto") "--format ${format}" ]
3377+
[ (preset != "default" && configuration == "") "--config-data ${preset}" ]
3378+
[ strict "--strict" ]
3379+
]);
33283380
in
33293381
"${hooks.yamllint.package}/bin/yamllint ${cmdArgs}";
33303382
};

0 commit comments

Comments
 (0)