Skip to content

Commit 77c9696

Browse files
committed
Cleanup cabal-project-to-nix
1 parent bc9cc91 commit 77c9696

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ let
140140
inherit (pkgs) runCommand cabal-install ghc;
141141
inherit (pkgs.haskellPackages) hpack;
142142
inherit (self) nix-tools;
143+
inherit (pkgs) symlinkJoin;
143144
};
144145
});
145146

lib/cabalProjectToNix.nix

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ mkHackageIndex, pkgs, runCommand, nix-tools, cabal-install, ghc, hpack }:
1+
{ mkHackageIndex, pkgs, runCommand, nix-tools, cabal-install, ghc, hpack, symlinkJoin }:
22
let defaultGhc = ghc;
33
defaultCabalInstall = cabal-install;
44
in { hackageIndexState, src, ghc ? defaultGhc, cabal-install ? defaultCabalInstall }:
@@ -10,7 +10,10 @@ let
1010
type == "directory" ||
1111
pkgs.lib.any (i: (pkgs.lib.hasSuffix i path)) [ ".project" ".cabal" "package.yaml" ];
1212
};
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" {
1417
buildInputs = [ ghc hpack ];
1518
} ''
1619
tmp=$(mktemp -d)
@@ -19,12 +22,34 @@ let
1922
chmod +w -R .
2023
find . -name package.yaml -exec hpack "{}" \;
2124
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
2448
'';
2549
in
2650
runCommand "plan-and-src" {} ''
2751
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/
3055
''

0 commit comments

Comments
 (0)