From d7aa2d9da2dd3144a22abb32be582df658178def Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Mon, 25 Aug 2025 21:48:40 +0200 Subject: [PATCH] flake/hooks: Reconfigure Git Hooks 1. Introduce a new hook to run `cargo doc` in the "pre-commit" phase. We have to resort to wrapping the command in a shell script, since we cannot configure environment variables via `.pre-commit.yaml` but need `RUSTDOCFLAGS` so that `cargo doc` actually exists non-zero when there are warnings. 2. Move `cargo check` from the "pre-push" to the "pre-commit" phase, as it is relatively quick, and redundant to run before `clippy`. 3. Let `cargo check` and `cargo doc` run after `rustfmt`. 4. Let a failure of `rustfmt` fail the whole check. 5. Wire through `packageOverrides` to `rustfmt` so we use the same toolchain as the other tools. 6. Additionally enable `cargo clippy` to consider all features. --- flake.nix | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/flake.nix b/flake.nix index c43fbc4a..fd1a3b59 100644 --- a/flake.nix +++ b/flake.nix @@ -173,17 +173,45 @@ settings.rust.check.cargoDeps = pkgs.rustPlatform.importCargoLock {lockFile = ./Cargo.lock;}; hooks = { alejandra.enable = true; - rustfmt.enable = true; + rustfmt = { + enable = true; + fail_fast = true; + packageOverrides.rustfmt = rustup.toolchain; + }; cargo-check = { enable = true; - stages = ["pre-push"]; + name = "cargo check"; + after = ["rustfmt"]; + fail_fast = true; + }; + cargo-doc = let + # We wrap `cargo` in order to set an environment variable that + # gives us a non-zero exit on warning. + command = + pkgs.writeShellScript + "cargo" + "RUSTDOCFLAGS='--deny warnings' ${lib.getExe' rustup.toolchain "cargo"} $@"; + in { + enable = true; + name = "cargo doc"; + after = ["rustfmt"]; + fail_fast = true; + entry = "${command} doc --workspace --all-features --no-deps"; + files = "\\.rs$"; + pass_filenames = false; }; clippy = { enable = true; - stages = ["pre-push"]; - settings.denyWarnings = true; - packageOverrides.cargo = rustup.toolchain; - packageOverrides.clippy = rustup.toolchain; + name = "cargo clippy"; + stages = ["pre-push"]; # Only pre-push, because it takes a while. + settings = { + allFeatures = true; + denyWarnings = true; + }; + packageOverrides = { + cargo = rustup.toolchain; + clippy = rustup.toolchain; + }; }; shellcheck.enable = true; };