git-hooks.nix
git-hooks.nix copied to clipboard
cabal2nix hook
To prevent having to use IFD using callCabal2nix, it would be nice if there was a hook that would run cabal2nix on each cabal file it finds to generate a default.nix in that directory.
Here's how I would implement it:
cabal2nix = {
enable = true;
name = "cabal2nix";
files = "\\.cabal$";
entry = "${pkgs.writeShellScript "cabal2nix-hook" ''
for cabalFile in $*
do
dir="$(dirname $cabalFile)"
defaultFile="$dir/default.nix"
${pkgs.cabal2nix}/bin/cabal2nix "$cabalFile" > "$defaultFile"
${pkgs.nixpkgs-fmt}/bin/nixpkgs-fmt "$defaultFile"
done
''}";
};
Note that the formatting is in order for this hook to work nicely together with the nixpkgs-fmt hook.
If there was some way of saying this hook must happen before the nixpkgs-fmt hook, then that would be better I think.
I just realised the formatting problem can be solved by ignoring files called default.nix in the formatter hook but that's a bit dirty.
A few hooks pull extra settings from options named settings.<hookname>.foo, so you could make the choice of nix formatter configurable. That's still duplicate configuration, but at least it'd work for all formatters. For a real solution, we either need a way to safely reinvoke pre-commit on the file, or create an option to explicitly represent the choice of nix formatter, which is kind of a breaking change.
@roberth Having the user use the excludes config is good enough for me for now. See the PR: #170