Skip to content

git-hook

1 post with the tag “git-hook”

Integrate git hooks with treefmt and devshell

Background

I was looking for a way to integrate pre-commit-hooks.nix and numtide/devshell, I came across this github issue. It seems zimbatm added git.hooks extra module to support git hook integration in devshell. So I decide to give a try.

Code

I am configuring my devshell using nix, rather than toml file.

I think for toml version, you should just do

devshell.toml

imports = ["git/hooks"]
git.hooks.enable = true
git.hooks.pre-comment = "treefmt"

assuming you already added devshell overlay, we need to import git extra module, and enable git.hooks and add script for the hook we want to use. Here i am using numtide/treefmt.

devShell = pkgs.devshell.mkShell {
imports = [ (pkgs.devshell.extraModulesDir + "/git/hooks.nix") ];
git.hooks.enable = true;
git.hooks.pre-commit.text = "${pkgs.treefmt}/bin/treefmt";
}

assuming you have a treefmt.toml in your project root directory, you should be to ready to go