diff --git a/README.md b/README.md index 8c48c5c9..d0c17900 100644 --- a/README.md +++ b/README.md @@ -304,6 +304,7 @@ clang-format supports. - [check-yaml](https://github.com/pre-commit/pre-commit-hooks/blob/main/pre_commit_hooks/check_yaml.py) - [sort-simple-yaml](https://github.com/pre-commit/pre-commit-hooks/blob/main/pre_commit_hooks/sort_simple_yaml.py) +- [yamlfmt](https://github.com/google/yamlfmt) - [yamllint](https://github.com/adrienverge/yamllint) ### TOML diff --git a/modules/hooks.nix b/modules/hooks.nix index d4b73d09..5590f3ab 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -1601,6 +1601,25 @@ in }; }; }; + yamlfmt = mkOption { + description = lib.mdDoc "yamlfmt hook"; + type = types.submodule { + imports = [ hookModule ]; + options.settings = { + configPath = + mkOption { + type = types.str; + description = lib.mdDoc "Path to a custom configuration file."; + # An empty string translates to yamlfmt looking for a configuration file in the + # following locations (by order of preference): + # a file named .yamlfmt, yamlfmt.yml, yamlfmt.yaml, .yamlfmt.yaml or .yamlfmt.yml in the current working directory + # See details [here](https://github.com/google/yamlfmt/blob/main/docs/config-file.md#config-file-discovery) + default = ""; + example = ".yamlfmt"; + }; + }; + }; + }; yamllint = mkOption { description = lib.mdDoc "yamllint hook"; type = types.submodule { @@ -3413,6 +3432,27 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol "${hooks.vale.package}/bin/vale${cmdArgs} ${hooks.vale.settings.flags}"; types = [ "text" ]; }; + yamlfmt = + { + name = "yamlfmt"; + description = "Formatter for YAML files."; + types = [ "file" "yaml" ]; + package = tools.yamlfmt; + entry = + let + cmdArgs = + mkCmdArgs + (with hooks.yamlfmt.settings; [ + # Exit non-zero on changes + [ true "-lint" ] + # But do not print the diff + [ true "-quiet" ] + # See https://github.com/google/yamlfmt/blob/main/docs/config-file.md#config-file-discovery + [ (configPath != "") "-conf ${configPath}" ] + ]); + in + "${hooks.yamlfmt.package}/bin/yamlfmt ${cmdArgs}"; + }; yamllint = { name = "yamllint"; diff --git a/nix/tools.nix b/nix/tools.nix index bb9549f4..a04dc1bf 100644 --- a/nix/tools.nix +++ b/nix/tools.nix @@ -72,6 +72,7 @@ , typos , typstfmt , zprint +, yamlfmt , yamllint , go , go-tools @@ -146,6 +147,7 @@ in typos typstfmt vale + yamlfmt yamllint zprint ;