Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the module system #19

Merged
merged 7 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
roberth marked this conversation as resolved.
Show resolved Hide resolved
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