git-hooks.nix icon indicating copy to clipboard operation
git-hooks.nix copied to clipboard

cabal2nix hook

Open NorfairKing opened this issue 3 years ago • 4 comments

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.

NorfairKing avatar Oct 02 '22 01:10 NorfairKing

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.

NorfairKing avatar Oct 02 '22 06:10 NorfairKing

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.

NorfairKing avatar Oct 02 '22 18:10 NorfairKing

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 avatar Oct 04 '22 14:10 roberth

@roberth Having the user use the excludes config is good enough for me for now. See the PR: #170

NorfairKing avatar Oct 05 '22 07:10 NorfairKing