Skip to content

hercules-ci/pre-commit-hooks.nix

This branch is 921 commits behind cachix/git-hooks.nix:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

77dd757 · Feb 27, 2020

History

97 Commits
Feb 27, 2020
Feb 6, 2020
Feb 6, 2020
Sep 16, 2019
Nov 6, 2019
Nov 6, 2019
Sep 9, 2019
Feb 21, 2020
Sep 4, 2019
Aug 16, 2019
Aug 19, 2019
Aug 16, 2019

Repository files navigation

Seamless integration of pre-commit git hooks with Nix

pre-commit manages a set of hooks that are executed by git before committing code:

pre-commit.png

The goal is to manage these hooks with Nix and solve the following:

  • Simpler integration for Nix projects (wires up a few things behind the scenes)

  • Provide a low-overhead build of all the tooling available for the hooks to use (naive implementation of calling nix-shell does bring some latency when committing)

  • Common package set of hooks for popular languages like Haskell, Elm, etc.

  • Two trivial Nix functions to run hooks as part of development and on your CI

Installation & Usage

  1. (optional) Use binary caches to avoid compilation:

    $ nix-env -iA cachix -f https://cachix.org/api/v1/install
    $ cachix use pre-commit-hooks
  2. Integrate hooks to be built as part of default.nix:

     let
       nix-pre-commit-hooks = import (builtins.fetchTarball "https://github.com/cachix/nix-pre-commit-hooks/tarball/master");
     in {
       pre-commit-check = nix-pre-commit-hooks.run {
         src = ./.;
         hooks = {
           elm-format.enable = true;
           ormolu.enable = true;
           shellcheck.enable = true;
         };
       };
     }

    Run $ nix-build -A pre-commit-check to perform the checks as a Nix derivation.

  3. Integrate hooks to prepare environment as part of shell.nix:

     (import <nixpkgs> {}).mkShell {
       inherit ((import ./default.nix).pre-commit-check) shellHook;
     }

    Add /.pre-commit-config.yaml to .gitignore.

    Run $ nix-shell to execute shellHook which will:

    • build the tools and .pre-commit-config.yaml config file symlink which references the binaries, for speed and safe garbage collection
    • provide the pre-commit executable that git commit will invoke

Optional

Direnv + Lorri

.envrc

eval "$(lorri direnv)"

# Use system PKI
unset SSL_CERT_FILE
unset NIX_SSL_CERT_FILE

# Install pre-commit hooks
eval "$shellHook"

Hooks

Nix

Haskell

Elm

Shell

Terraform

  • terraform-format: built-in formatter

Contributing hooks

Everyone is encouraged to add new hooks.

Have a look at the existing hooks and the options.

There's no guarantee the hook will be accepted, but the general guidelines are:

  • Nix closure of the tool should be small e.g. < 50MB
  • The tool must not be very specific (e.g. language tooling is OK, but project specific tooling is not)
  • The tool needs to live in a separate repository (even if a simple bash script, unless it's a oneliner)

About

Seamless integration of https://pre-commit.com git hooks with Nix.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Nix 100.0%