|
1 |
| -{ mkHackageIndex, pkgs, runCommand, nix-tools, cabal-install, ghc, hpack }: |
| 1 | +{ mkHackageIndex, pkgs, runCommand, nix-tools, cabal-install, ghc, hpack, symlinkJoin }: |
2 | 2 | let defaultGhc = ghc;
|
3 | 3 | defaultCabalInstall = cabal-install;
|
4 | 4 | in { hackageIndexState, src, ghc ? defaultGhc, cabal-install ? defaultCabalInstall }:
|
|
10 | 10 | type == "directory" ||
|
11 | 11 | pkgs.lib.any (i: (pkgs.lib.hasSuffix i path)) [ ".project" ".cabal" "package.yaml" ];
|
12 | 12 | };
|
13 |
| - plan = runCommand "plan" { |
| 13 | + plan = if (builtins.compareVersions cabal-install.version "2.4.0.0") < 0 |
| 14 | + # cabal-install versions before 2.4 will generate insufficient plan information. |
| 15 | + then throw "cabal-install (current version: ${cabal-install.version}) needs to be at least 2.4 for plan-to-nix to work without cabal-to-nix" |
| 16 | + else runCommand "plan" { |
14 | 17 | buildInputs = [ ghc hpack ];
|
15 | 18 | } ''
|
16 | 19 | tmp=$(mktemp -d)
|
|
19 | 22 | chmod +w -R .
|
20 | 23 | find . -name package.yaml -exec hpack "{}" \;
|
21 | 24 | HOME=${mkHackageIndex hackageIndexState} ${cabal-install}/bin/cabal new-configure
|
22 |
| - HOME=$out ${nix-tools}/bin/plan-to-nix --plan-json dist-newstyle/cache/plan.json -o nix-plan |
23 |
| - cp -r nix-plan $out |
| 25 | +
|
| 26 | + export LANG=C.utf8 # Needed or stack-to-nix will die on unicode inputs |
| 27 | + mkdir -p $out |
| 28 | +
|
| 29 | + # ensure we have all our .cabal files (also those generated from package.yaml) files. |
| 30 | + # otherwise we'd need to be careful about putting the `cabal-generator = hpack` into |
| 31 | + # the nix expression. As we already called `hpack` on all `package.yaml` files we can |
| 32 | + # skip that step and just package the .cabal files up as well. |
| 33 | + # |
| 34 | + # This is also important as `plan-to-nix` will look for the .cabal files when generating |
| 35 | + # the relevant `pkgs.nix` file with the local .cabal expressions. |
| 36 | + ${pkgs.rsync}/bin/rsync -a --prune-empty-dirs --include '*/' --include '*.cabal' --exclude '*' $tmp/ $out/ |
| 37 | +
|
| 38 | + # make sure the path's in the plan.json are relative to $out instead of $tmp |
| 39 | + # this is necessary so that plan-to-nix relative path logic can work. |
| 40 | + substituteInPlace $tmp/dist-newstyle/cache/plan.json --replace "$tmp" "$out" |
| 41 | +
|
| 42 | + # run `plan-to-nix` in $out. This should produce files right there with the |
| 43 | + # proper relative paths. |
| 44 | + (cd $out && ${nix-tools}/bin/plan-to-nix --plan-json $tmp/dist-newstyle/cache/plan.json -o .) |
| 45 | +
|
| 46 | + # move pkgs.nix to default.nix ensure we can just nix `import` the result. |
| 47 | + mv $out/pkgs.nix $out/default.nix |
24 | 48 | '';
|
25 | 49 | in
|
26 | 50 | runCommand "plan-and-src" {} ''
|
27 | 51 | mkdir $out
|
28 |
| - cp -r ${src}/* $out |
29 |
| - ln -sf ${plan} $out/nix-plan |
| 52 | + # todo: should we clean `src` to drop any .git, .nix, ... other irelevant files? |
| 53 | + ${pkgs.xorg.lndir}/bin/lndir -silent "${src}" "$out" |
| 54 | + ${pkgs.rsync}/bin/rsync -a ${plan}/ $out/ |
30 | 55 | ''
|
0 commit comments