Skip to content

ysndr/nix-git-hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Nix Git Hooks

This repository is the accompanying library to my blog post.

The flake provides two library functions mkInstaller and mkUninstaller that can be used to create commands to install and unsinstall git hooks respectively.

Usage

Include the flake as input to your own flake:

inputs.nix-git-hooks.url = "github:ysndr/nix-git-hook";

Define git hooks, for example:

nixFormatHook = pkgs.writeShellScriptBin "check-rust-format-hook" ''
    ${pkgs.nixpkgs-fmt}/bin/nixpkgs-fmt --check flake.nix
    RESULT=$?
    [ $RESULT != 0 ] && echo "Please run \`nixpkgs-fmt\` before committing"
    exit $RESULT
'';

Generate an installer and uninstaller:

# Using the overlay
hookInstaller =
    pkgs.git-hook-installer { pre-commit = [ nixFormatHook ]; };
hookUninstaller = pkgs.git-hook-uninstaller;

# Using the library
hookInstaller =
    nix-git-hooks.lib.mkInstaller pkgs { 
        pre-commit = [ nixFormatHook ];
        # see https://git-scm.com/docs/githooks for more hook types
    };

hookUninstaller = nix-git-hooks.lib.mkUninstaller pkgs;

Include the (un)installer in your shell

pkgs.mkShell {
    packages = [ hookInstaller hookUninstaller ];
    shellHook = ''
        echo "=== Development shell ==="
        echo "Info: Git hooks can be installed using \`install-git-hooks\`"
        # or run `install-git-hooks` automatically
    '';
};

About

Track and install git hooks with nix

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages