Add `pre-commit-hooks.nix` for codequality
I recently used pre-commit-hooks.nix quite often which makes it really easy to ensure codequality in a flake.
It provides very easy interface to code-quality tools like
- statix
- different nix formatters like
nixpkgs-fmtoralejandra - shellcheck
It provides
- a check that can be run automatically in the
checksattribute - a
shellHookthat runs all formatters and linters that have been selected upon each commit - a
pre-commitscript which you can use to run your formatters and linters
Example:
inputs.pre-commit-hooks = {
url = "github:cachix/pre-commit-hooks.nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
outputs =
let
# -- snip util funs --
# configuration is trivial
preCommitCheckFor = system:
pre-commit-hooks.lib.${system}.run
{
src = ./.;
settings = { };
hooks = {
cabal-fmt.enable = false;
fourmolu.enable = false;
hlint.enable = false;
markdownlint.enable = false;
nixpkgs-fmt.enable = true;
shellcheck.enable = true;
statix.enable = true;
stylish-haskell.enable = true;
};
tools = {
fourmolu = fourmoluFor system;
hlint = hlintFor system;
};
};
in
{
devShells = perSystem (system: {
default =
let
pkgs = nixpkgsFor system;
in
pkgs.mkShell {
# adds pre-commit tool and pre-commit hook
inherit (preCommitCheckFor system) shellHook;
};
});
checks = perSystem (system:
{ formatCheck = preCommitCheckFor system; }
# format check is for free
);
};
Sorry, what do you want to add this to? To the haskell.nix repo? or what?
Generally I'm :-1: on adding more stuff automatigally to haskell.nix projects. haskell.nix has way too much stuff in it already.
Sorry, what do you want to add this to? To the haskell.nix repo
Yes, for formatting and linting in the Haskell.nix repo.
I'd be fine with that. It would be superb to get most of our shell code shellcheck-ed, but I'm not sure if we can do that since it's mostly in string literals in nix code. Perhaps an argument for trying to pull it out into separate files.
yes, I agree, that would be nice, that would also make it easier to read.
pre-commit-hooks is also nice in regards to that it forces the developer/ contributor to make their formatting/ linting checks, like that there aren't any PRs whose checks don't pass because of formatting/ linting errors. I think that's a huge plus, I found that I (and another person that uses it and I asked about it) forget it more often than I would like to
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.