GithubHelp home page GithubHelp logo

home-damager's Introduction

Git hooks

Install Nix packages as Git hooks from your Nix shell environment.

Heavily inspired by a hack to get treefmt into a pre-commit hook.

Installation

nix-shell -p npins
npins init
npins add github fricklerhandwerk git-hooks -b main
## default.nix
let
  sources = import ./npins;
in
{
  pkgs ? import sources.nixpkgs { inherit system; config = { }; overlays = [ ]; },
  git-hooks ? pkgs.callPackage sources.git-hooks { },
  system ? builtins.currentSystem,
}:
let
  inherit (git-hooks) lib;
in
pkgs.mkShellNoCC {
  shellHook = ''
#    # add Git hooks here
  '';
}

lib.git-hooks.pre-commit

pre-commit :: Derivation -> Path

pre-commit takes a derivaton with a pre-commit hook, and returns a path to an executable that will install the hook.

The derivation must have meta.mainProgram set to the name of the executable in $out/bin/ that implements the hook. The hook is installed in the Git repository that surrounds the working directory of the Nix invocation, and will get run roughly like this:

git stash push --keep-index
hook
git stash pop

Example

Add a pre-commit hook

Entering this shell environment will install a Git hook that prints Hello, world! to the console before each commit:

pkgs.mkShellNoCC {
  shellHook = ''
    ${lib.git-hooks.pre-commit pkgs.hello}
  '';
}

lib.git-hooks.wrap.abort-on-change

abort-on-change :: Derivation -> Derivation

Wrap a hook such that the commit is aborted if the hook changes staged files.

Example

Wrap a pre-commit hook to abort on changed files

The cursed hook will add an empty line to each staged file. Wrapping it in abort-on-change will prevent files thus changed from being committed.

let
  cursed = pkgs.writeShellApplication {
    name = "pre-commit-hook";
    runtimeInputs = with pkgs; [ git ];
    text = ''
      for f in $(git diff --name-only --cached); do
        echo >> "$f"
      done
    '';
  };
in
pkgs.mkShellNoCC {
  shellHook = ''
    ${with lib.git-hooks; pre-commit (wrap.abort-on-change cursed)}
  '';
}

home-damager's People

Contributors

fricklerhandwerk avatar wamirez avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

wamirez

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.