Skip to content

Commit 0a9f42d

Browse files
authored
Merge branch 'master' into feat/nixfmt-indent/main
2 parents 018f1fc + ca5b894 commit 0a9f42d

File tree

9 files changed

+189
-69
lines changed

9 files changed

+189
-69
lines changed

README.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
- Run hooks **as part of development** and **during CI**
1515

16+
- Support for alternative `pre-commit` implementations, like [prek](https://github.com/j178/prek).
17+
1618
## Getting started
1719

1820
### devenv.sh
@@ -22,24 +24,38 @@
2224
2325
{
2426
git-hooks.hooks = {
25-
# lint shell scripts
27+
# Format Nix code
28+
nixfmt.enable = true;
29+
30+
# Format Python code
31+
black.enable = true;
32+
33+
# Lint shell scripts
2634
shellcheck.enable = true;
27-
# execute example shell from Markdown files
35+
36+
# Execute shell examples in Markdown files
2837
mdsh.enable = true;
29-
# format Python code
30-
black.enable = true;
3138
32-
# override a package with a different version
39+
# Override a package with a different version
3340
ormolu.enable = true;
3441
ormolu.package = pkgs.haskellPackages.ormolu;
3542
36-
# some hooks have more than one package, like clippy:
43+
# Some hooks have more than one package, like clippy:
3744
clippy.enable = true;
3845
clippy.packageOverrides.cargo = pkgs.cargo;
3946
clippy.packageOverrides.clippy = pkgs.clippy;
40-
# some hooks provide settings
47+
# Some hooks provide settings
4148
clippy.settings.allFeatures = true;
49+
50+
# Define your own custom hooks
51+
my-custom-hook = {
52+
name = "My own hook";
53+
exec = "on-pre-commit.sh";
54+
};
4255
};
56+
57+
# Use alternative pre-commit implementations
58+
git-hooks.package = pkgs.prek;
4359
}
4460
```
4561

@@ -77,7 +93,7 @@ Given the following `flake.nix` example:
7793
config = self.checks.${system}.pre-commit-check.config;
7894
inherit (config) package configFile;
7995
script = ''
80-
${package}/bin/pre-commit run --all-files --config ${configFile}
96+
${pkgs.lib.getExe package} run --all-files --config ${configFile}
8197
'';
8298
in
8399
pkgs.writeShellScriptBin "pre-commit-run" script
@@ -89,7 +105,7 @@ Given the following `flake.nix` example:
89105
pre-commit-check = inputs.git-hooks.lib.${system}.run {
90106
src = ./.;
91107
hooks = {
92-
nixfmt-rfc-style.enable = true;
108+
nixfmt.enable = true;
93109
};
94110
};
95111
});
@@ -359,6 +375,7 @@ use nix
359375

360376
### Markdown
361377

378+
- [comrak](https://github.com/kivikakk/comrak)
362379
- [markdownlint](https://github.com/DavidAnson/markdownlint)
363380
- [mdformat](https://github.com/hukkin/mdformat)
364381
- [mdl](https://github.com/markdownlint/markdownlint/)
@@ -370,6 +387,7 @@ use nix
370387
- [deadnix](https://github.com/astro/deadnix)
371388
- [flake-checker](https://github.com/DeterminateSystems/flake-checker)
372389
- [nil](https://github.com/oxalica/nil)
390+
- [nixfmt](https://github.com/NixOS/nixfmt/) (supports `nixfmt` >=v1.0)
373391
- [nixfmt-classic](https://github.com/NixOS/nixfmt/tree/v0.6.0)
374392
- [nixfmt-rfc-style](https://github.com/NixOS/nixfmt/)
375393
- [nixpkgs-fmt](https://github.com/nix-community/nixpkgs-fmt)
@@ -474,6 +492,7 @@ use nix
474492

475493
- [actionlint](https://github.com/rhysd/actionlint)
476494
- [action-validator](https://github.com/mpalmer/action-validator)
495+
- [chart-testing](https://github.com/helm/chart-testing)
477496
- [check-added-large-files](https://github.com/pre-commit/pre-commit-hooks/blob/main/pre_commit_hooks/check_added_large_files.py)
478497
- [check-case-conflicts](https://github.com/pre-commit/pre-commit-hooks/blob/main/pre_commit_hooks/check_case_conflict.py)
479498
- [check-executables-have-shebangs](https://github.com/pre-commit/pre-commit-hooks/blob/main/pre_commit_hooks/check_executables_have_shebangs.py)
@@ -489,6 +508,8 @@ use nix
489508
- [end-of-file-fixer](https://github.com/pre-commit/pre-commit-hooks/blob/main/pre_commit_hooks/end_of_file_fixer.py)
490509
- [fix-byte-order-marker](https://github.com/pre-commit/pre-commit-hooks/blob/main/pre_commit_hooks/fix_byte_order_marker.py)
491510
- [headache](https://github.com/frama-c/headache)
511+
- [hledger-fmt](https://github.com/mondeja/hledger-fmt)
512+
- [keep-sorted](https://github.com/google/keep-sorted)
492513
- [mixed-line-endings](https://github.com/pre-commit/pre-commit-hooks/blob/main/pre_commit_hooks/mixed_line_ending.py)
493514
- [mkdocs-linkcheck](https://github.com/byrnereese/linkchecker-mkdocs)
494515
- [openapi-spec-validator](https://github.com/python-openapi/openapi-spec-validator)

flake-module.nix

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ in
3737
Nixpkgs to use in the pre-commit [`settings`](#opt-perSystem.pre-commit.settings).
3838
'';
3939
default = pkgs;
40-
defaultText = lib.literalMD "`pkgs` (module argument)";
40+
defaultText = lib.literalExpression "`pkgs` (module argument)";
4141
};
4242
settings = mkOption {
4343
type = types.submoduleWith {
@@ -49,16 +49,23 @@ in
4949
The git-hooks.nix configuration.
5050
'';
5151
};
52+
shellHook = mkOption {
53+
type = types.str;
54+
description = "A shell hook that installs up the git hooks in a development shell.";
55+
default = cfg.settings.installationScript;
56+
defaultText = lib.literalExpression "bash statements";
57+
readOnly = true;
58+
};
5259
installationScript = mkOption {
5360
type = types.str;
54-
description = "A bash fragment that sets up [pre-commit](https://pre-commit.com/).";
61+
description = "A bash snippet that sets up the git hooks in the current repository.";
5562
default = cfg.settings.installationScript;
56-
defaultText = lib.literalMD "bash statements";
63+
defaultText = lib.literalExpression "bash statements";
5764
readOnly = true;
5865
};
5966
devShell = mkOption {
6067
type = types.package;
61-
description = "A development shell with pre-commit installed and setup.";
68+
description = "A development shell with the git hooks installed and all the packages made available.";
6269
readOnly = true;
6370
};
6471
};
@@ -72,8 +79,8 @@ in
7279
hooks.treefmt.package = lib.mkIf (options?treefmt) (lib.mkOverride 900 config.treefmt.build.wrapper);
7380
};
7481
pre-commit.devShell = pkgs.mkShell {
75-
nativeBuildInputs = cfg.settings.enabledPackages ++ [ cfg.settings.package ];
76-
shellHook = cfg.installationScript;
82+
inherit (cfg.settings) shellHook;
83+
nativeBuildInputs = cfg.settings.enabledPackages;
7784
};
7885
};
7986
});

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/hook.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ in
5757
name = mkOption {
5858
type = types.str;
5959
default = name;
60-
defaultText = lib.literalMD "the attribute name the hook submodule is bound to, same as `id`";
60+
defaultText = lib.literalExpression "the attribute name the hook submodule is bound to, same as `id`";
6161
description =
6262
''
6363
The name of the hook. Shown during hook execution.
@@ -186,7 +186,7 @@ in
186186
Confines the hook to run at a particular stage.
187187
'';
188188
default = default_stages;
189-
defaultText = (lib.literalExpression or lib.literalExample) "default_stages";
189+
defaultText = lib.literalExpression "default_stages";
190190
};
191191

192192
verbose = mkOption {

modules/hooks.nix

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,20 @@ in
680680
};
681681
};
682682
};
683+
hledger-fmt = mkOption {
684+
description = "hledger-fmt hook";
685+
type = types.submodule {
686+
imports = [ hookModule ];
687+
options.settings = {
688+
fix =
689+
mkOption {
690+
type = types.bool;
691+
description = "Fix the files in place.";
692+
default = false;
693+
};
694+
};
695+
};
696+
};
683697
hlint = mkOption {
684698
description = "hlint hook";
685699
type = types.submodule {
@@ -959,7 +973,7 @@ in
959973
};
960974
};
961975
nixfmt = mkOption {
962-
description = "Deprecated nixfmt hook. Use nixfmt-classic or nixfmt-rfc-style instead.";
976+
description = "nixfmt hook";
963977
type = types.submodule {
964978
imports = [ hookModule ];
965979
options.settings = {
@@ -2154,10 +2168,19 @@ in
21542168
lib.optional cfg.hooks.rome.enable ''
21552169
The hook `hooks.rome` has been renamed to `hooks.biome`.
21562170
''
2157-
++ lib.optional cfg.hooks.nixfmt.enable ''
2171+
++ lib.optional (cfg.hooks.nixfmt.enable && lib.versionOlder cfg.hooks.nixfmt.package.version "1.0") ''
21582172
The hook `hooks.nixfmt` has been renamed to `hooks.nixfmt-classic`.
21592173
21602174
The new RFC 166-style nixfmt is available as `hooks.nixfmt-rfc-style`.
2175+
''
2176+
++ lib.optional (cfg.hooks.nixfmt-classic.enable && lib.versionAtLeast cfg.hooks.nixfmt-classic.package.version "1.0") ''
2177+
The hook `hooks.nixfmt-classic` is using an incompatible version of `nixfmt`.
2178+
2179+
Found: ${cfg.hooks.nixfmt-classic.package.version}.
2180+
Expected: < v1.0
2181+
2182+
`hooks.nixfmt-classic` supports versions of `nixfmt` up to `v1.0`.
2183+
For `nixfmt` `v1.0` and newer, switch to `hooks.nixfmt`.
21612184
'';
21622185

21632186
# PLEASE keep this sorted alphabetically.
@@ -2222,6 +2245,7 @@ in
22222245
in
22232246
"${hooks.ansible-lint.package}/bin/ansible-lint ${cmdArgs}";
22242247
files = if hooks.ansible-lint.settings.subdir != "" then "${hooks.ansible-lint.settings.subdir}/" else "";
2248+
pass_filenames = false;
22252249
};
22262250
autoflake =
22272251
{
@@ -2321,6 +2345,15 @@ in
23212345
files = "\\.rs$";
23222346
pass_filenames = false;
23232347
};
2348+
chart-testing =
2349+
{
2350+
name = "chart-testing";
2351+
description = "CLI tool for linting and testing Helm charts";
2352+
files = "^charts/";
2353+
package = tools.chart-testing;
2354+
entry = "${hooks.chart-testing.package}/bin/ct lint --all --skip-helm-dependencies";
2355+
pass_filenames = false;
2356+
};
23242357
checkmake = {
23252358
name = "checkmake";
23262359
description = "Experimental linter/analyzer for Makefiles";
@@ -2555,6 +2588,13 @@ in
25552588
entry = "${hooks.commitizen.package}/bin/cz check --allow-abort --commit-msg-file";
25562589
stages = [ "commit-msg" ];
25572590
};
2591+
comrak = {
2592+
name = "comrak";
2593+
description = "A 100% CommonMark-compatible GitHub Flavored Markdown formatter";
2594+
package = tools.comrak;
2595+
entry = "${lib.getExe hooks.comrak.package} --inplace";
2596+
types = [ "markdown" ];
2597+
};
25582598
conform = {
25592599
name = "conform enforce";
25602600
description = "Policy enforcement for commits.";
@@ -3086,6 +3126,22 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm
30863126
entry = "${hooks.hindent.package}/bin/hindent";
30873127
files = "\\.l?hs(-boot)?$";
30883128
};
3129+
hledger-fmt =
3130+
{
3131+
name = "hledger-fmt";
3132+
description = "Opinionated hledger's journal files formatter.";
3133+
package = tools.hledger-fmt;
3134+
entry =
3135+
let
3136+
cmdArgs = mkCmdArgs (
3137+
with hooks.hledger-fmt.settings; [
3138+
[ fix "--fix" ]
3139+
]
3140+
);
3141+
in
3142+
"${hooks.hledger-fmt.package}/bin/hledger-fmt ${cmdArgs}";
3143+
files = "\\.(hledger|journal|j)$";
3144+
};
30893145
hlint =
30903146
{
30913147
name = "hlint";
@@ -3164,6 +3220,14 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm
31643220
end'
31653221
'';
31663222
};
3223+
keep-sorted =
3224+
{
3225+
name = "keep-sorted";
3226+
description = "Language-agnostic formatter that sorts lines between two markers in a larger file.";
3227+
types = [ "text" ];
3228+
package = tools.keep-sorted;
3229+
entry = "${hooks.keep-sorted.package}/bin/keep-sorted";
3230+
};
31673231
latexindent =
31683232
{
31693233
name = "latexindent";
@@ -4161,7 +4225,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm
41614225
inherit (hooks.typos.settings) config exclude;
41624226
configFile = toml.generate "typos-config.toml" config;
41634227
excludeFlags = lib.concatStringsSep " "
4164-
(lib.map (glob: "--exclude ${glob}") exclude);
4228+
(lib.map (glob: "--exclude '${glob}'") exclude);
41654229
cmdArgs =
41664230
mkCmdArgs
41674231
(with hooks.typos.settings; [
@@ -4310,7 +4374,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.fourm
43104374
cmdArgs =
43114375
mkCmdArgs
43124376
(with hooks.yamllint.settings; [
4313-
# Priorize multiline configuration over serialized configuration and configuration file
4377+
# Prioritize multiline configuration over serialized configuration and configuration file
43144378
[ (configuration != "") "--config-file ${configFile}" ]
43154379
[ (configData != "" && configuration == "") "--config-data \"${configData}\"" ]
43164380
[ (configPath != "" && configData == "" && configuration == "" && preset == "default") "--config-file ${configPath}" ]

0 commit comments

Comments
 (0)