cabal2nix icon indicating copy to clipboard operation
cabal2nix copied to clipboard

cabal2nix inside of nix expression

Open Profpatsch opened this issue 6 years ago • 1 comments

In my default.nix, I have a derivation like this:

  src = pkgs.nix-gitignore.gitignoreSource [".git/" "*.nix"] ./.;

  generatedNix = pkgs.runCommand "pkg-default.nix" {}
    ''
      cp -r ${src}/* .
      cp -r ${builtins.path { path = ./.git; name = "dotgit"; }} .git

      ${pkgs.cabal2nix}/bin/cabal2nix . > $out
      sed -e 's|src = ./.|src = ${src}|' -i $out
    '';

I filter the src with the contents of the .gitignore file (which uses builtins.filterSource under the hood), then I want to run cabal2nix on the cabal file in the filtered source.

Ideally, something like cabal2nix --src $out file://$src > $out would just work out of the box and generate a nix file in $out.

Instead, there’s two things one has to manually fix:

  • cabal2nix tries multiple cvs systems and fails if it can’t find any, so we have to copy the whole .git to the store and copy it into the build environment
  • cabal2nix uses the path it’s given for the src field, which is ./., so I need to patch the generated source file. I suggest adding a --src flag to specify it manually.

Profpatsch avatar Sep 10 '19 10:09 Profpatsch

Because somebody asked on IRC, the workaround we use currently is as follows:

  1. import the gitignore-filtered (pkgs.nix-gitignore.gitignoreSource) source code into the nix store (fully, every time some code changes)
  2. Run pkgs.callCabal2nix on that imported code, so the generated cabal file that references ./. is in the nix store, and ./. would be imported into the nix store, but it already is, so that import becomes a no-op.

Profpatsch avatar Apr 27 '20 06:04 Profpatsch