From 0bddfbb0633e8c2536a4a9720ae1d7806df89873 Mon Sep 17 00:00:00 2001 From: ari Date: Mon, 19 Oct 2026 20:11:20 -0400 Subject: [PATCH] Wire hooks into devenv and flake checks Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_019dpKwZWtahQU4V2NhpVmLt --- devenv.hooks.nix | 38 ++++++++++++++++++++++++++++ devenv.nix | 2 ++ flake.nix | 5 +++- nix/checks.nix | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 devenv.hooks.nix create mode 100644 nix/checks.nix diff --git a/devenv.hooks.nix b/devenv.hooks.nix new file mode 100644 index 0000000..0881bab --- /dev/null +++ b/devenv.hooks.nix @@ -0,0 +1,38 @@ +# Git hooks. The checks themselves live in dev/hooks/ (shared with the Nix +# flake checks in nix/checks.nix); this file only wires them into git-hooks. +# Hooks install when a devenv shell is entered. See docs/hooks.md. +{ pkgs, ... }: + +{ + packages = [ + pkgs.taplo + pkgs.yamllint + pkgs.shellcheck + pkgs.markdownlint-cli + pkgs.nushell + ]; + + git-hooks.hooks = { + # Fast: fmt, clippy (Rust files only), lint, secret scan. + slinuxm-pre-commit = { + enable = true; + name = "slinuxm pre-commit"; + entry = "dev/hooks/run.sh pre-commit"; + language = "system"; + pass_filenames = false; + always_run = true; + stages = [ "pre-commit" ]; + }; + # Path-filtered tests and slow checks. Skip one run with + # SKIP=slinuxm-pre-push git push (pre-commit framework variable). + slinuxm-pre-push = { + enable = true; + name = "slinuxm pre-push"; + entry = "dev/hooks/run.sh pre-push"; + language = "system"; + pass_filenames = false; + always_run = true; + stages = [ "pre-push" ]; + }; + }; +} diff --git a/devenv.nix b/devenv.nix index 53d0cbd..fa6b7e5 100644 --- a/devenv.nix +++ b/devenv.nix @@ -1,6 +1,8 @@ { pkgs, ... }: { + imports = [ ./devenv.hooks.nix ]; + packages = [ pkgs.git pkgs.pkg-config diff --git a/flake.nix b/flake.nix index 990e8a7..d6fde92 100644 --- a/flake.nix +++ b/flake.nix @@ -28,7 +28,10 @@ module = self.nixosModules.default; }; - checks.${system} = { + checks.${system} = import ./nix/checks.nix { + pkgs = nixpkgs.legacyPackages.${system}; + package = self.packages.${system}.default; + } // { package = self.packages.${system}.default; vm = import ./nix/tests/vm.nix { pkgs = nixpkgs.legacyPackages.${system}; diff --git a/nix/checks.nix b/nix/checks.nix new file mode 100644 index 0000000..997c59d --- /dev/null +++ b/nix/checks.nix @@ -0,0 +1,65 @@ +{ pkgs, package }: + +# Flake checks that mirror the git hooks. The logic lives in dev/hooks/*.sh so +# the devenv hooks and these derivations cannot drift apart. The package's own +# build already runs `cargo test`, so no separate test check exists here. The +# VM tests stay in flake.nix. +let + inherit (pkgs) lib; + + src = lib.cleanSource ../.; + + # Everything the lint scripts shell out to. HOOKS_REQUIRE_TOOLS makes a + # missing tool a failure instead of a skip. + baseTools = with pkgs; [ bash coreutils findutils gnugrep gnused ]; + + lintTools = baseTools ++ (with pkgs; [ + taplo + yamllint + shellcheck + markdownlint-cli + nushell + nix + ]); + + hookCheck = name: tools: script: pkgs.runCommand "slinuxm-${name}" + { + nativeBuildInputs = tools; + HOOKS_REQUIRE_TOOLS = "1"; + } + '' + export HOME=$TMPDIR NIX_STATE_DIR=$TMPDIR/nix-state NIX_LOG_DIR=$TMPDIR/nix-log + cp -r ${src} work + chmod -R u+w work + patchShebangs work/dev/hooks + cd work + ${script} + touch $out + ''; + + # Rust checks reuse the package's vendored dependencies and build inputs. + rustCheck = name: phase: package.overrideAttrs (old: { + pname = "${old.pname}-${name}"; + nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ [ pkgs.clippy pkgs.rustfmt ]; + doCheck = false; + dontCargoBuild = true; + dontCargoInstall = true; + dontCheckForBrokenSymlinks = true; + buildPhase = '' + runHook preBuild + ${phase} + runHook postBuild + ''; + installPhase = "touch $out"; + }); +in +{ + hooks-lint = hookCheck "lint" lintTools "bash dev/hooks/run.sh lint-all"; + hooks-selection = hookCheck "selection" baseTools "bash dev/hooks/test-select.sh"; + rust-fmt = rustCheck "fmt" "cargo fmt --check"; + rust-clippy = rustCheck "clippy" "cargo clippy --all-targets --offline -- -D warnings"; + rust-no-openssl = rustCheck "no-openssl" '' + out=$(cargo tree --offline -i openssl 2>/dev/null || true) + if [ -n "$out" ]; then echo "$out"; echo "openssl is in the dependency tree"; exit 1; fi + ''; +}