Skip to content

Commit

Permalink
Merge #19
Browse files Browse the repository at this point in the history
19: Use the module system r=roberth a=roberth

Closes #2
Closes #17 

TODO
 - [x] ~expose and test "core" module set from project.nix~
 - [x] ~niv update -b master project.nix when that's done (hercules-ci/project.nix#10
 - [x] changelog entry: (elaborate on) translate `.pre-commit-config.yaml` to hooks argument
 - [x] update README
 - [x] add how to add a hook to README

New
 - Selectively enable tools (#2)
 - Make choice of tool versions overridable
 - User-definable hooks
 - Input validation at the Nix level
 - Use gitignore by default

Fixes
 - Stop installing files all over the place when doing nix-shell ../some-project to borrow a tool
 - Lorri support

Cleanup
 - Get rid of some installation code

Co-authored-by: Robert Hensing <[email protected]>
  • Loading branch information
bors[bot] and roberth authored Oct 2, 2019
2 parents e3661fe + bd07c08 commit c8b1b87
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 142 deletions.
24 changes: 18 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
repos:
- repo: .pre-commit-hooks/
rev: master
hooks:
- id: shellcheck
- id: canonix
# DO NOT MODIFY
# This file was generated by nix-pre-commit-hooks
{
"repos": [
{
"hooks": [
{
"id": "canonix"
},
{
"id": "shellcheck"
}
],
"repo": ".pre-commit-hooks/",
"rev": "master"
}
]
}
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and is a rolling release.

## 2019-10-02

### Added

- The `run` derivation now uses [gitignore](https://github.com/hercules-ci/gitignore#readme)

- Custom hooks can now be added

### Changed

- Hooks configuration is now module-based (using the module system, like NixOS).
`.pre-commit-config.yaml` is now obsolete with `nix-pre-commit-hooks`. Translate it to the `hooks` argument. For example:

```
pre-commit-check = nix-pre-commit-hooks.run {
src = ./.;
hooks = {
elm-format.enable = true;
ormolu.enable = true;
shellcheck.enable = true;
};
};
```

### Fixed

- Some small improvements to the installation script (`shellHook`)
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,32 @@ The goal is to manage these hooks with Nix and solve the following:

# Installation & Usage

1. Create `.pre-commit-config.yaml` with hooks you want to run in your git repository:
```yaml
repos:
- repo: .pre-commit-hooks/
rev: master
hooks:
- id: ormolu
- id: shellcheck
- id: elm-format
```
2. (optional) Use binary caches to avoid compilation:
1. (optional) Use binary caches to avoid compilation:

```bash
$ nix-env -iA cachix -f https://cachix.org/api/v1/install
$ cachix use hercules-ci
```

3. Integrate hooks to be built as part of `default.nix`:
2. Integrate hooks to be built as part of `default.nix`:
```nix
let
inherit (import (builtins.fetchTarball "https://github.com/hercules-ci/gitignore/tarball/master" {})) gitignoreSource;
nix-pre-commit-hooks = import (builtins.fetchTarball "https://github.com/hercules-ci/nix-pre-commit-hooks/tarball/master");
in {
pre-commit-check = nix-pre-commit-hooks.run {
src = gitignoreSource ./.;
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.

2. Integrate hooks to prepare environment as part of `shell.nix`:
3. Integrate hooks to prepare environment as part of `shell.nix`:
```nix
(import <nixpkgs> {}).mkShell {
inherit ((import ./. {}).pre-commit-check) shellHook;
Expand Down Expand Up @@ -109,6 +102,9 @@ eval "$shellHook"

Everyone is encouraged to add new hooks.

<!-- TODO generate option docs -->
Have a look at the [existing hooks](modules/hooks.nix) and the [options](modules/pre-commit.nix).

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`
Expand Down
7 changes: 7 additions & 0 deletions modules/all-modules.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
imports =
[
./pre-commit.nix
./hooks.nix
];
}
83 changes: 83 additions & 0 deletions modules/hooks.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{ config, lib, pkgs, ... }:
let
inherit (config.pre-commit) tools;
in {
config.pre-commit.hooks =
{
hlint =
{
name = "hlint";
description =
"HLint gives suggestions on how to improve your source code.";
entry = "${tools.hlint}/bin/hlint";
files = "\\.l?hs$";
};
ormolu =
{
name = "ormolu";
description = "Haskell code prettifier.";
entry = "${tools.ormolu}/bin/ormolu --mode inplace";
files = "\\.l?hs$";
};
hindent =
{
name = "hindent";
description = "Haskell code prettifier.";
entry = "${tools.hindent}/bin/hindent";
files = "\\.l?hs$";
};
cabal-fmt =
{
name = "cabal-fmt";
description = "Format Cabal files";
entry = "${tools.cabal-fmt}/bin/cabal-fmt --inplace";
files = "\\.cabal$";
};
canonix =
{
name = "canonix";
description = "Nix code prettifier.";
entry = "${tools.canonix}/bin/canonix";
files = "\\.nix$";
};
nixfmt =
{
name = "nixfmt";
description = "Nix code prettifier.";
entry = "${tools.nixfmt}/bin/nixfmt";
files = "\\.nix$";
};
nixpkgs-fmt =
{
name = "nixpkgs-fmt";
description = "Nix code prettifier.";
entry = "${tools.nixpkgs-fmt}/bin/nixpkgs-fmt -i";
files = "\\.nix$";
};
elm-format =
{
name = "elm-format";
description = "Format Elm files";
entry =
"${tools.elm-format}/bin/elm-format --yes --elm-version=0.19";
files = "\\.elm$";
};
shellcheck =
{
name = "shellcheck";
description = "Format shell files";
types =
[
"bash"
];
entry = "${tools.shellcheck}/bin/shellcheck";
};
terraform-format =
{
name = "terraform-format";
description = "Format terraform (.tf) files";
entry = "${tools.terraform-fmt}/bin/terraform-fmt";
files = "\\.tf$";
};
};
}
Loading

0 comments on commit c8b1b87

Please sign in to comment.