From 3148767e5bb842dd9de4cd6e92f8918ba38e006d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Dani=C3=ABl=20Maat?= Date: Thu, 23 Jul 2026 21:45:41 +0800 Subject: [PATCH 1/2] feat(pre-commits): Make prek show colors in CI --- nix/general/workflows/check-pre-commit-hooks.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/nix/general/workflows/check-pre-commit-hooks.nix b/nix/general/workflows/check-pre-commit-hooks.nix index 7fc79e9..1888add 100644 --- a/nix/general/workflows/check-pre-commit-hooks.nix +++ b/nix/general/workflows/check-pre-commit-hooks.nix @@ -40,6 +40,7 @@ in shell = "nix develop .#standards --command bash {0}"; run = "prek --all-files --show-diff-on-failure"; env = { + PREK_COLOR = "always"; # On some CI runners, the cache would time out, causing the pipeline to fail. # Since the official documentation (https://treefmt.com/usage/#ci-integration) # recommends using `----no-cache` anyway, we add it here. From 790772886b1a27eb78fed0bdc3374fff9763d3cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Dani=C3=ABl=20Maat?= Date: Thu, 23 Jul 2026 21:42:38 +0800 Subject: [PATCH 2/2] feat(pre-commits): Default hooks to run during pre-commit only This is necessary to allow downstream projects to run code generation and such in pre-push hooks, while still making it run in CI. --- .github/workflows/check-pre-commit-hooks.yml | 3 +- .pre-commit-config.yaml | 21 ++++++ nix/general/devshell.nix | 7 ++ .../workflows/check-pre-commit-hooks.nix | 2 +- nix/modules/prek-pre-commit.nix | 66 ++++++++++++++++++- 5 files changed, 96 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check-pre-commit-hooks.yml b/.github/workflows/check-pre-commit-hooks.yml index 6d5141f..ba82272 100644 --- a/.github/workflows/check-pre-commit-hooks.yml +++ b/.github/workflows/check-pre-commit-hooks.yml @@ -10,9 +10,10 @@ jobs: - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 - uses: cachix/install-nix-action@8aa03977d8d733052d78f4e008a241fd1dbf36b3 - env: + PREK_COLOR: always TREEFMT_NO_CACHE: "1" name: Run pre-commit hooks - run: prek --all-files --show-diff-on-failure + run: prek --all-files --show-diff-on-failure --stage pre-push shell: nix develop .#standards --command bash {0} name: Make sure all pre-commit hooks pass "on": diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7005a67..dfa6037 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,26 +1,43 @@ repos: - hooks: - id: check-added-large-files + stages: [] - id: check-case-conflict + stages: [] - id: check-illegal-windows-names + stages: [] - id: end-of-file-fixer + stages: [] - id: fix-byte-order-marker + stages: [] - id: check-json + stages: [] - id: check-json5 + stages: [] - id: check-toml + stages: [] - id: check-vcs-permalinks + stages: [] - id: check-xml + stages: [] - args: - --fix=lf id: mixed-line-ending + stages: [] - id: check-symlinks + stages: [] - id: destroyed-symlinks + stages: [] - id: check-merge-conflict + stages: [] - id: detect-private-key + stages: [] - exclude_types: - rust id: check-shebang-scripts-are-executable + stages: [] - id: check-executables-have-shebangs + stages: [] repo: builtin - hooks: - args: @@ -31,6 +48,7 @@ repos: id: typos language: system name: typos + stages: [] types: - text - description: Ensure that files set up with the filegen module are up-to-date @@ -39,12 +57,14 @@ repos: language: system name: filegen pass_filenames: false + stages: [] - description: Format *all* files entry: treefmt id: treefmt language: system name: treefmt require_serial: true + stages: [] repo: local - hooks: - args: @@ -57,4 +77,5 @@ repos: language: system name: flake-follows pass_filenames: false + stages: [] repo: local diff --git a/nix/general/devshell.nix b/nix/general/devshell.nix index 20e1291..770a826 100644 --- a/nix/general/devshell.nix +++ b/nix/general/devshell.nix @@ -32,6 +32,13 @@ importingFlake: { package = config.prek-pre-commit.package.wrapper; } + { + name = "prek --stage pre-push"; + help = "*Also* slow pre-commit hooks"; + category = "[[lints and checks]]"; + package = config.prek-pre-commit.package.wrapper; + } + { name = "prek -s main -o HEAD"; help = "Run pre-commit hooks on all commits in the current branch"; diff --git a/nix/general/workflows/check-pre-commit-hooks.nix b/nix/general/workflows/check-pre-commit-hooks.nix index 1888add..fc21e7e 100644 --- a/nix/general/workflows/check-pre-commit-hooks.nix +++ b/nix/general/workflows/check-pre-commit-hooks.nix @@ -38,7 +38,7 @@ in { name = "Run pre-commit hooks"; shell = "nix develop .#standards --command bash {0}"; - run = "prek --all-files --show-diff-on-failure"; + run = "prek --all-files --show-diff-on-failure --stage pre-push"; env = { PREK_COLOR = "always"; # On some CI runners, the cache would time out, causing the pipeline to fail. diff --git a/nix/modules/prek-pre-commit.nix b/nix/modules/prek-pre-commit.nix index 3f34f5e..d533c1e 100644 --- a/nix/modules/prek-pre-commit.nix +++ b/nix/modules/prek-pre-commit.nix @@ -27,6 +27,12 @@ in description = '' `prek` configuration for each workspace. + Any hooks defined *must* run either under the `pre-commit` or `pre-push` + stage to be picked up by CI. + + `pre-push` hooks are intended to be used for heavier work that we don't + want to run on every commit (so that rebases stay spiffy). + Note that any hooks that depend on system packages being installed should have their packages installed via the `prek-pre-commit.package.runtimePkgs` option. @@ -34,7 +40,65 @@ in See the [`prek` documentation](https://prek.j178.dev/workspace/) for details. ''; default = { }; - type = types.attrsOf (types.submodule { freeformType = settingsFormat.type; }); + type = types.attrsOf ( + types.submodule { + freeformType = settingsFormat.type; + + options.repos = lib.mkOption { + description = '' + The prek `repos` to configure. + + See https://prek.j178.dev/reference/configuration/#repos-required + ''; + + type = lib.types.listOf ( + lib.types.submodule { + freeformType = settingsFormat.type; + + options.hooks = lib.mkOption { + description = '' + The prek `hooks` to configure for this repo. + + See https://prek.j178.dev/reference/configuration/#hook-entries + ''; + default = [ ]; + + type = lib.types.listOf ( + lib.types.submodule { + freeformType = settingsFormat.type; + + options.stages = lib.mkOption { + description = '' + The stages during which to run this pre-commit. + + Only `pre-push` is currently supported. + ''; + default = [ ]; + apply = + stages: + if stages != [ ] && stages != [ "pre-push" ] then + builtins.abort '' + Invalid hook stages. + + Currently, only pre-push hooks are allowed. To make CI scripting easier, + we're forcing all other hooks to be defined as running under all stages. + + If you want to use a different stage, or want to explicitly run a specific + hook only during pre-commit, talk to the engineering-standards maintainers. + + '' + else + [ ]; + }; + } + ); + }; + } + ); + default = [ ]; + }; + } + ); }; }; }