haskell.nix icon indicating copy to clipboard operation
haskell.nix copied to clipboard

Add `pre-commit-hooks.nix` for codequality

Open MangoIV opened this issue 3 years ago • 5 comments

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-fmt or alejandra
  • shellcheck

It provides

  • a check that can be run automatically in the checks attribute
  • a shellHook that runs all formatters and linters that have been selected upon each commit
  • a pre-commit script 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
    );
 }; 

MangoIV avatar Oct 01 '22 12:10 MangoIV

Sorry, what do you want to add this to? To the haskell.nix repo? or what?

michaelpj avatar Oct 03 '22 09:10 michaelpj

Generally I'm :-1: on adding more stuff automatigally to haskell.nix projects. haskell.nix has way too much stuff in it already.

michaelpj avatar Oct 03 '22 09:10 michaelpj

Sorry, what do you want to add this to? To the haskell.nix repo

Yes, for formatting and linting in the Haskell.nix repo.

MangoIV avatar Oct 03 '22 10:10 MangoIV

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.

michaelpj avatar Oct 03 '22 10:10 michaelpj

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

MangoIV avatar Oct 03 '22 11:10 MangoIV

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.

stale[bot] avatar Jan 31 '23 11:01 stale[bot]