Skip to content

Commit 1afb871

Browse files
committed
Add cabalProjectToNix
1 parent 4d74f43 commit 1afb871

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

default.nix

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# It's also possible to override these sources with NIX_PATH.
77
, hackageSourceJSON ? ./hackage-src.json
88
, stackageSourceJSON ? ./stackage-src.json
9+
, hackageIndexState ? null
10+
, recentNixpkgs ? import <nixpkgs> {}
911
}:
1012

1113
let
@@ -38,12 +40,24 @@ let
3840
# overridden with NIX_PATH.
3941
fetchExternal = import ./lib/fetch-external.nix;
4042

43+
hackageIndex = import ./lib/hackageIndex.nix {
44+
inherit (pkgs) runCommand;
45+
inherit (recentNixpkgs) cabal-install;
46+
indexState = hackageIndexState;
47+
};
48+
4149
# All packages from Hackage as Nix expressions
42-
hackage = import (fetchExternal {
43-
name = "hackage-exprs-source";
44-
specJSON = hackageSourceJSON;
45-
override = "hackage";
46-
});
50+
hackage = if hackageIndexState == null
51+
then import (fetchExternal {
52+
name = "hackage-exprs-source";
53+
specJSON = hackageSourceJSON;
54+
override = "hackage";
55+
})
56+
else import (import ./lib/callHackageToNix.nix {
57+
inherit (pkgs) runCommand;
58+
inherit (import ./. {}) nix-tools;
59+
inherit hackageIndex;
60+
});
4761

4862
# The set of all Stackage snapshots
4963
stackage = import (fetchExternal {
@@ -118,6 +132,19 @@ let
118132
update-stackage = self.callPackage ./scripts/update-stackage.nix {};
119133
update-pins = self.callPackage ./scripts/update-pins.nix {};
120134
};
135+
136+
# Make this handy overridable fetch function available.
137+
inherit fetchExternal;
138+
139+
# Takes a haskell src directory runs cabal new-configure and plan-to-nix.
140+
# Resulting nix files are added to nix-plan subdirectory.
141+
cabalProjectToNix = import ./lib/cabalProjectToNix.nix {
142+
inherit pkgs hackageIndex;
143+
inherit (pkgs) runCommand;
144+
inherit (recentNixpkgs) cabal-install ghc;
145+
inherit (recentNixpkgs.haskellPackages) hpack;
146+
inherit (import ./. {}) nix-tools;
147+
};
121148
});
122149

123150
in

lib/cabalProjectToNix.nix

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{ pkgs, runCommand, nix-tools, cabal-install
2+
, hackageIndex, ghc, hpack
3+
} :
4+
{src} :
5+
let
6+
cabalFiles =
7+
builtins.filterSource (path: type:
8+
type == "directory" ||
9+
pkgs.lib.any (i: (pkgs.lib.hasSuffix i path)) [ ".project" ".cabal" "package.yaml" ])
10+
src;
11+
plan = runCommand "plan" {
12+
buildInputs = [ ghc hpack ];
13+
} ''
14+
tmp=$(mktemp -d)
15+
cd $tmp
16+
cp -r ${cabalFiles}/* .
17+
chmod +w -R .
18+
find . -name package.yaml -exec hpack "{}" \;
19+
HOME=${hackageIndex} ${cabal-install}/bin/cabal new-configure
20+
HOME=$out ${nix-tools}/bin/plan-to-nix --plan-json dist-newstyle/cache/plan.json -o nix-plan
21+
cp -r nix-plan $out
22+
'';
23+
in
24+
runCommand "plan-and-src" {} ''
25+
mkdir $out
26+
cp -r ${src}/* $out
27+
ln -sf ${plan} $out/nix-plan
28+
''

lib/callHackageToNix.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{ runCommand, nix-tools
2+
, hackageIndex
3+
} : runCommand "hackage-nix" {} ''
4+
HOME=${hackageIndex} ${nix-tools}/bin/hackage-to-nix $out
5+
''

lib/hackageIndex.nix

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{ runCommand, cabal-install
2+
, indexState ? "2019-04-24T21:34:04Z"
3+
} :
4+
let
5+
# To avoid downloading more data than necessary this will provide a base.
6+
cachedState = runCommand "hackage-${builtins.substring 0 4 indexState}" {} ''
7+
mkdir -p $out
8+
HOME=$out ${cabal-install}/bin/cabal new-update 'hackage.haskell.org,${builtins.substring 0 4 indexState}-01-01T00:00:00Z'
9+
'';
10+
in runCommand "hackage-${builtins.replaceStrings [":"] [""] indexState}" {} ''
11+
mkdir -p $out
12+
cp -r ${cachedState}/.cabal $out
13+
chmod +w -R $out/.cabal
14+
sed -i.back -e "s|${cachedState}|$out|g" $out/.cabal/config
15+
HOME=$out ${cabal-install}/bin/cabal new-update 'hackage.haskell.org,${indexState}'
16+
''

0 commit comments

Comments
 (0)