Skip to content

Commit 9edc7f6

Browse files
committed
Add pre-commit default hooks
1 parent 5df5a70 commit 9edc7f6

File tree

2 files changed

+268
-3
lines changed

2 files changed

+268
-3
lines changed

modules/hooks.nix

Lines changed: 267 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,23 @@ in
633633
default = null;
634634
};
635635
};
636+
no-commit-to-branch =
637+
{
638+
branch =
639+
mkOption {
640+
description = lib.mdDoc "Branches to disallow commits to.";
641+
type = types.listOf types.str;
642+
default = [ "main" ];
643+
example = [ "main" "master" ];
644+
};
645+
pattern =
646+
mkOption {
647+
description = lib.mdDoc "RegEx patterns for branch names to disallow commits to.";
648+
type = types.listOf types.str;
649+
default = [ ];
650+
example = [ "ma.*" ];
651+
};
652+
};
636653
ormolu =
637654
{
638655
defaultExtensions =
@@ -1057,6 +1074,21 @@ in
10571074
example = [ "flake.nix" "_*" ];
10581075
};
10591076
};
1077+
sort-file-contents =
1078+
{
1079+
ignore-case =
1080+
mkOption {
1081+
type = types.bool;
1082+
description = lib.mdDoc "Fold lower case to upper case characters.";
1083+
default = false;
1084+
};
1085+
unique =
1086+
mkOption {
1087+
type = types.bool;
1088+
description = lib.mdDoc "Ensure each line is unique.";
1089+
default = false;
1090+
};
1091+
};
10601092
treefmt =
10611093
{
10621094
package = mkOption {
@@ -1356,6 +1388,106 @@ in
13561388
"The version of nixpkgs used by pre-commit-hooks.nix must have `checkmake` in version at least 0.2.2 for it to work on non-Linux systems."
13571389
"${tools.checkmake}/bin/checkmake";
13581390
};
1391+
check-added-large-files =
1392+
{
1393+
name = "check-added-large-files";
1394+
description = "Prevent very large files to be committed (e.g. binaries).";
1395+
entry = "${tools.pre-commit-hooks}/bin/check-added-large-files";
1396+
stages = [ "commit" "push" "manual" ];
1397+
};
1398+
check-builtin-literals =
1399+
{
1400+
name = "check-builtin-literals";
1401+
description = "Require literal syntax when initializing empty or zero builtin types in Python.";
1402+
entry = "${tools.pre-commit-hooks}/bin/check-builtin-literals";
1403+
types = [ "python" ];
1404+
};
1405+
check-case-conflicts =
1406+
{
1407+
name = "check-case-conflicts";
1408+
description = "Check for files that would conflict in case-insensitive filesystems.";
1409+
entry = "${tools.pre-commit-hooks}/bin/check-case-conflict";
1410+
types = [ "file" ];
1411+
};
1412+
check-docstring-first =
1413+
{
1414+
name = "check-docstring-above";
1415+
description = "Check that all docstrings appear above the code.";
1416+
entry = "${tools.pre-commit-hooks}/bin/check-docstring-first";
1417+
types = [ "python" ];
1418+
};
1419+
check-executables-have-shebangs =
1420+
{
1421+
name = "check-executables-have-shebangs";
1422+
description = "Ensure that all non-binary executables have shebangs.";
1423+
entry = "${tools.pre-commit-hooks}/bin/check-executables-have-shebangs";
1424+
types = [ "text" "executable" ];
1425+
stages = [ "commit" "push" "manual" ];
1426+
};
1427+
check-json =
1428+
{
1429+
name = "check-json";
1430+
description = "Check syntax of JSON files.";
1431+
entry = "${tools.pre-commit-hooks}/bin/check-json";
1432+
types = [ "json" ];
1433+
};
1434+
check-merge-conflicts =
1435+
{
1436+
name = "check-merge-conflicts";
1437+
description = "Check for files that contain merge conflict strings.";
1438+
entry = "${tools.pre-commit-hooks}/bin/check-merge-conflict";
1439+
types = [ "text" ];
1440+
};
1441+
check-python =
1442+
{
1443+
name = "check-python";
1444+
description = "Check syntax of Python file by parsing Python abstract syntax tree.";
1445+
entry = "${tools.pre-commit-hooks}/bin/check-ast";
1446+
types = [ "python" ];
1447+
};
1448+
check-shebang-scripts-are-executable =
1449+
{
1450+
name = "check-shebang-scripts-are-executable";
1451+
description = "Ensure that all (non-binary) files with a shebang are executable.";
1452+
entry = "${tools.pre-commit-hooks}/bin/check-shebang-scripts-are-executable";
1453+
types = [ "text" ];
1454+
stages = [ "commit" "push" "manual" ];
1455+
};
1456+
check-symlinks =
1457+
{
1458+
name = "check-symlinks";
1459+
description = "Find broken symlinks.";
1460+
entry = "${tools.pre-commit-hooks}/bin/check-symlinks";
1461+
types = [ "symlink" ];
1462+
};
1463+
check-toml =
1464+
{
1465+
name = "check-toml";
1466+
description = "Check syntax of TOML files.";
1467+
entry = "${tools.pre-commit-hooks}/bin/check-toml";
1468+
types = [ "toml" ];
1469+
};
1470+
check-vcs-permalinks =
1471+
{
1472+
name = "check-vcs-permalinks";
1473+
description = "Ensure that links to VCS websites are permalinks.";
1474+
entry = "${tools.pre-commit-hooks}/bin/check-vcs-permalinks";
1475+
types = [ "text" ];
1476+
};
1477+
check-xml =
1478+
{
1479+
name = "check-xml";
1480+
description = "Check syntax of TOML files.";
1481+
entry = "${tools.pre-commit-hooks}/bin/check-xml";
1482+
types = [ "xml" ];
1483+
};
1484+
check-yaml =
1485+
{
1486+
name = "check-yaml";
1487+
description = "Check syntax of YAML files.";
1488+
entry = "${tools.pre-commit-hooks}/bin/check-yaml";
1489+
types = [ "yaml" ];
1490+
};
13591491
chktex =
13601492
{
13611493
name = "chktex";
@@ -1530,6 +1662,20 @@ in
15301662
in
15311663
"${tools.deno}/bin/deno lint ${cmdArgs}";
15321664
};
1665+
detect-aws-credentials =
1666+
{
1667+
name = "detect-aws-credentials";
1668+
description = "Detect AWS credentials from the AWS cli credentials file.";
1669+
entry = "${tools.pre-commit-hooks}/bin/detect-aws-credentials --allow-missing-credentials";
1670+
types = [ "text" ];
1671+
};
1672+
detect-private-keys =
1673+
{
1674+
name = "detect-private-keys";
1675+
description = "Detect the presence of private keys.";
1676+
entry = "${tools.pre-commit-hooks}/bin/detect-private-key";
1677+
types = [ "text" ];
1678+
};
15331679
dhall-format = {
15341680
name = "dhall-format";
15351681
description = "Dhall code formatter.";
@@ -1593,6 +1739,13 @@ in
15931739
entry = "${tools.editorconfig-checker}/bin/editorconfig-checker";
15941740
types = [ "file" ];
15951741
};
1742+
end-of-file-fixer =
1743+
{
1744+
name = "end-of-file-fixer";
1745+
description = "Ensures that a file is either empty, or ends with a single newline.";
1746+
entry = "${tools.pre-commit-hooks}/bin/end-of-file-fixer";
1747+
types = [ "text" ];
1748+
};
15961749
elm-format =
15971750
{
15981751
name = "elm-format";
@@ -1624,6 +1777,14 @@ in
16241777
entry = "${settings.eslint.binPath} --fix";
16251778
files = "${settings.eslint.extensions}";
16261779
};
1780+
{
1781+
fix-byte-order-marker =
1782+
{
1783+
name = "fix-byte-order-marker";
1784+
description = "Remove UTF-8 byte order marker.";
1785+
entry = "${tools.pre-commit-hooks}/bin/fix-byte-order-marker";
1786+
types = [ "text" ];
1787+
};
16271788
flake8 =
16281789
let
16291790
extendIgnoreStr =
@@ -1660,14 +1821,28 @@ in
16601821
"${settings.flynt.binPath} ${cmdArgs}";
16611822
types = [ "python" ];
16621823
};
1824+
forbid-new-submodules =
1825+
{
1826+
name = "forbid-new-submodules";
1827+
description = "Prevent addition of new Git submodules.";
1828+
entry = "${tools.pre-commit-hooks}/bin/forbid-new-submodules";
1829+
types = [ "directory" ];
1830+
};
1831+
forbid-submodules =
1832+
{
1833+
name = "forbid-submodules";
1834+
description = "Forbid any Git submodule in a repository.";
1835+
entry = "${tools.pre-commit-hooks}/bin/forbid-submodules";
1836+
types = [ "directory" ];
1837+
};
16631838
fourmolu =
16641839
{
16651840
name = "fourmolu";
16661841
description = "Haskell code prettifier.";
16671842
entry =
16681843
"${tools.fourmolu}/bin/fourmolu --mode inplace ${
1669-
lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) settings.ormolu.defaultExtensions)
1670-
}";
1844+
lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) settings.ormolu.defaultExtensions)
1845+
}";
16711846
files = "\\.l?hs(-boot)?$";
16721847
};
16731848
fprettify = {
@@ -2010,6 +2185,12 @@ in
20102185
entry = toString script;
20112186
files = "\\.md$";
20122187
};
2188+
mixed-line-endings = {
2189+
name = "mixed-line-endings";
2190+
description = "Resolve mixed line endings.";
2191+
entry = "${tools.pre-commit-hooks}/bin/mixed-line-endings --fix";
2192+
types = [ "text" ];
2193+
};
20132194
mix-format = {
20142195
name = "mix-format";
20152196
description = "Runs the built-in Elixir syntax formatter";
@@ -2047,6 +2228,13 @@ in
20472228
entry = settings.mypy.binPath;
20482229
files = "\\.py$";
20492230
};
2231+
name-tests-test =
2232+
{
2233+
name = "mypy";
2234+
description = "Verify that Python test files are named correctly.";
2235+
entry = "${tools.pre-commit-hooks}/bin/name-tests-test";
2236+
files = "(^|/)tests/\.+\\.py$";
2237+
};
20502238
nil =
20512239
{
20522240
name = "nil";
@@ -2087,6 +2275,23 @@ in
20872275
entry = "${tools.nixpkgs-fmt}/bin/nixpkgs-fmt";
20882276
files = "\\.nix$";
20892277
};
2278+
no-commit-to-branch =
2279+
{
2280+
name = "no-commit-to-branch";
2281+
description = "Disallow committing to certain branch/branches.";
2282+
pass_filenames = false;
2283+
always_run = true;
2284+
entry =
2285+
let
2286+
cmdArgs =
2287+
mkCmdArgs
2288+
(with settings.no-commit-to-branch; [
2289+
[ (branch != [ ]) "--branch ${lib.strings.concatStringsSep " --branch " branch}" ]
2290+
[ (pattern != [ ]) "--pattern ${lib.strings.concatStringsSep " --pattern " pattern}" ]
2291+
]);
2292+
in
2293+
"${tools.pre-commit-hooks}/bin/no-commit-to-branch ${cmdArgs}";
2294+
};
20902295
ocp-indent =
20912296
{
20922297
name = "ocp-indent";
@@ -2147,6 +2352,13 @@ in
21472352
"${binPath} analyse";
21482353
types = [ "php" ];
21492354
};
2355+
pretty-format-json =
2356+
{
2357+
name = "pretty-format-json";
2358+
description = "Formats JSON files.";
2359+
entry = "${tools.pre-commit-hooks}/bin/pretty-format-json";
2360+
types = [ "json" ];
2361+
};
21502362
pre-commit-hook-ensure-sops = {
21512363
name = "pre-commit-hook-ensure-sops";
21522364
entry =
@@ -2251,6 +2463,13 @@ in
22512463
entry = settings.pyright.binPath;
22522464
files = "\\.py$";
22532465
};
2466+
python-debug-statements =
2467+
{
2468+
name = "python-debug-statements";
2469+
description = "Check for debugger imports and py37+ `breakpoint()` calls in python source.";
2470+
entry = "${tools.pre-commit-hooks}/bin/debug-statement-hook";
2471+
types = [ "python" ];
2472+
};
22542473
pyupgrade =
22552474
{
22562475
name = "pyupgrade";
@@ -2342,6 +2561,44 @@ in
23422561
types = [ "shell" ];
23432562
entry = "${tools.shfmt}/bin/shfmt -w -s -l";
23442563
};
2564+
single-quoted-strings =
2565+
{
2566+
name = "single-quoted-strings";
2567+
description = "Replace double quoted strings with single quoted strings.";
2568+
entry = "${tools.pre-commit-hooks}/bin/double-quote-string-fixer";
2569+
types = [ "python" ];
2570+
};
2571+
sort-file-contents =
2572+
{
2573+
name = "sort-file-contents";
2574+
description = "Sort the lines in specified files (defaults to alphabetical).";
2575+
types = [ "text" ];
2576+
entry =
2577+
let
2578+
cmdArgs =
2579+
mkCmdArgs
2580+
(with settings.sort-file-contents;
2581+
[
2582+
[ ignore-case "--ignore-case" ]
2583+
[ unique "--unique" ]
2584+
]);
2585+
in
2586+
"${tools.pre-commit-hooks}/bin/file-contents-sorter ${cmdArgs}";
2587+
};
2588+
sort-requirements-txt =
2589+
{
2590+
name = "sort-requirements.txt";
2591+
description = "Sort requirements in requirements.txt and constraints.txt files.";
2592+
entry = "${tools.pre-commit-hooks}/bin/requirements-txt-fixer";
2593+
files = "\\.*(requirements|constraints)\\.*\\.txt$";
2594+
};
2595+
sort-simple-yaml =
2596+
{
2597+
name = "sort-simple-yaml";
2598+
description = "Sort simple YAML files which consist only of top-level keys, preserving comments and blocks.";
2599+
entry = "${tools.pre-commit-hooks}/bin/sort-simple-yaml";
2600+
files = "(\\.yaml$)|(\\.yml$)";
2601+
};
23452602
staticcheck =
23462603
{
23472604
name = "staticcheck";
@@ -2447,6 +2704,14 @@ in
24472704
);
24482705
files = "(\\.json$)|(\\.toml$)|(\\.mli?$)";
24492706
};
2707+
trim-trailing-whitespace =
2708+
{
2709+
name = "trim-trailing-whitespace";
2710+
description = "Trim trailing whitespace.";
2711+
types = [ "text" ];
2712+
stages = [ "commit" "push" "manual" ];
2713+
entry = "${tools.pre-commit-hooks}/bin/trailing-whitespace-fixer";
2714+
};
24502715
treefmt =
24512716
{
24522717
name = "treefmt";

nix/tools.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ in
152152
inherit (luaPackages) luacheck;
153153
inherit (nodePackages) eslint markdownlint-cli prettier pyright cspell;
154154
inherit (ocamlPackages) ocp-indent;
155-
inherit (python3Packages) autoflake black flake8 flynt isort mkdocs-linkcheck mypy pylint pyupgrade;
155+
inherit (python3Packages) autoflake black flake8 flynt isort mkdocs-linkcheck mypy pre-commit-hooks pylint pyupgrade;
156156
inherit (php82Packages) php-cs-fixer phpcbf phpcs psalm;
157157
# FIXME: workaround build failure
158158
phpstan = php82Packages.phpstan.overrideAttrs (old: {

0 commit comments

Comments
 (0)