All Projects → cachix → Pre Commit Hooks.nix

cachix / Pre Commit Hooks.nix

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

Labels

Projects that are alternatives of or similar to Pre Commit Hooks.nix

Setup.nix
Nixpkgs based build tools for declarative Python packages [[email protected]]
Stars: ✭ 62 (-32.61%)
Mutual labels:  nix
Nix Tools
Translate Cabals Generic Package Description to a Nix expression
Stars: ✭ 78 (-15.22%)
Mutual labels:  nix
Vuizvui
Nix(OS) expressions used by the OpenLab and its members
Stars: ✭ 87 (-5.43%)
Mutual labels:  nix
Dotfiles
well-tailored NixOS & nix-darwin dotfiles
Stars: ✭ 63 (-31.52%)
Mutual labels:  nix
Home Manager Template
A quick-start template for using home-manager in a more reproducible way.
Stars: ✭ 73 (-20.65%)
Mutual labels:  nix
Dhall Nix
This repository has moved to https://github.com/dhall-lang/dhall-haskell/tree/master/dhall-nix
Stars: ✭ 82 (-10.87%)
Mutual labels:  nix
Nixos Configurations
Stars: ✭ 58 (-36.96%)
Mutual labels:  nix
Upcast
Abandonware
Stars: ✭ 91 (-1.09%)
Mutual labels:  nix
Terranix
terranix is a terraform.json generator with a nix-like feeling
Stars: ✭ 77 (-16.3%)
Mutual labels:  nix
Homies
linux package management
Stars: ✭ 86 (-6.52%)
Mutual labels:  nix
Rien
Predictable Haskell development environments with Cabal and Nix.
Stars: ✭ 65 (-29.35%)
Mutual labels:  nix
Cardano Graphql
GraphQL API for Cardano
Stars: ✭ 66 (-28.26%)
Mutual labels:  nix
Cachix Action
Build software only once and put it in a global cache
Stars: ✭ 85 (-7.61%)
Mutual labels:  nix
Deckdeckgo
The web open source editor for presentations
Stars: ✭ 1,117 (+1114.13%)
Mutual labels:  nix
Rules nixpkgs
Rules for importing Nixpkgs packages into Bazel.
Stars: ✭ 88 (-4.35%)
Mutual labels:  nix
Neovim Nightly Overlay
[[email protected]]
Stars: ✭ 60 (-34.78%)
Mutual labels:  nix
B
Collection of configuration stuff.
Stars: ✭ 80 (-13.04%)
Mutual labels:  nix
Nix Processmgmt
Experimental Nix-based process management framework
Stars: ✭ 92 (+0%)
Mutual labels:  nix
Bauer
An Emacs+Nix IDE
Stars: ✭ 89 (-3.26%)
Mutual labels:  nix
Nixos Configs
My NixOS configs
Stars: ✭ 86 (-6.52%)
Mutual labels:  nix

Seamless integration of pre-commit git hooks with Nix

pre-commit.png

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

  • Trivial 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 hooks for languages like Python, Haskell, Elm, etc.

  • Run hooks as part of development and on your CI

Getting started

  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/pre-commit-hooks.nix/tarball/master");
     in {
       pre-commit-check = nix-pre-commit-hooks.run {
         src = ./.;
         # If your hooks are intrusive, avoid running on each commit with a default_states like this:
         # default_stages = ["manual" "push"];
         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 {
        shellHook = ''
         ${(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

Purescript

Python

Rust

Warning: running clippy after cargo check hides (https://github.com/rust-lang/rust-clippy/issues/4612) all clippy specific errors. Clippy subsumes cargo check so only one of these two checks should by used at the same time.

Shell

HTML

Terraform

  • terraform-format: built-in formatter

Spell checkers

Custom hooks

Sometimes it is useful to add a project specific command as an extra check that is not part of the pre-defined set of hooks provided by this project.

Example configuration:

 let
   nix-pre-commit-hooks = import (builtins.fetchTarball "https://github.com/cachix/pre-commit-hooks.nix/tarball/master");
 in {
   pre-commit-check = nix-pre-commit-hooks.run {
     hooks = {
       # ...

       # Example custom hook for a C project using Make:
       unit-tests = {
         enable = true;

         # The name of the hook (appears on the report table):
         name = "Unit tests";

         # The command to execute (mandatory):
         entry = "make check";

         # The pattern of files to run on (default: "" (all))
         # see also https://pre-commit.com/#hooks-files
         files = "\\.(c|h)$";

         # List of file types to run on (default: [ "file" ] (all files))
         # see also https://pre-commit.com/#filtering-files-with-types
         # You probably only need to specify one of `files` or `types`:
         types = [ "text" "c" ];

         # Exclude files that were matched by these patterns (default: [ ] (none)):
         excludes = [ "irrelevant\\.c" ];

         # The language of the hook - tells pre-commit
         # how to install the hook (default: "system")
         # see also https://pre-commit.com/#supported-languages
         language = "system";

         # Set this to false to not pass the changed files
         # to the command (default: true):
         pass_filenames = false;
       };
     };
   };
 }

Custom hooks are defined with the same schema as pre-defined hooks.

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)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].