Skip to content

Commit 3d02d75

Browse files
hamishmackangerman
authored andcommitted
index-state from cabal.project by default (#169)
* index-state from cabal.project by default Change callCabalProjectToNix to use the `index-state` specified in the `cabal.project` file by default.
1 parent 9643221 commit 3d02d75

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

lib/call-cabal-project-to-nix.nix

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{ dotCabal, pkgs, runCommand, nix-tools, cabal-install, ghc, hpack, symlinkJoin, cacert, index-state-hashes }:
22
let defaultGhc = ghc;
33
defaultCabalInstall = cabal-install;
4-
in { index-state, index-sha256 ? index-state-hashes.${index-state} or null, src, ghc ? defaultGhc, cabal-install ? defaultCabalInstall }:
4+
in { index-state ? null, index-sha256 ? null, src, ghc ? defaultGhc,
5+
cabal-install ? defaultCabalInstall, cabalProject ? null }:
56

6-
# better error message than just assert failed.
7-
assert (if index-sha256 == null then throw "provided sha256 for index-state ${index-state} is null!" else true);
87
# cabal-install versions before 2.4 will generate insufficient plan information.
98
assert (if (builtins.compareVersions cabal-install.version "2.4.0.0") < 0
109
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"
@@ -17,7 +16,41 @@ let
1716
filter = path: type:
1817
type == "directory" ||
1918
pkgs.lib.any (i: (pkgs.lib.hasSuffix i path)) [ ".project" ".cabal" "package.yaml" ]; }
20-
else src;
19+
else src;
20+
21+
# Using origSrc bypasses any cleanSourceWith so that it will work when
22+
# access to the store is restricted. If origSrc was already in the store
23+
# you can pass the project in as a string.
24+
rawCabalProject = if cabalProject != null
25+
then cabalProject
26+
else builtins.readFile ((maybeCleanedSource.origSrc or maybeCleanedSource) + "/cabal.project");
27+
28+
# Look for a index-state: field in the cabal.project file
29+
parseIndexState = rawCabalProject:
30+
let
31+
indexState = pkgs.lib.lists.concatLists (
32+
pkgs.lib.lists.filter (l: l != null)
33+
(builtins.map (l: builtins.match "^index-state: *(.*)" l)
34+
(pkgs.lib.splitString "\n" rawCabalProject)));
35+
in
36+
pkgs.lib.lists.head (indexState ++ [ null ]);
37+
38+
index-state-found = if index-state != null
39+
then index-state
40+
else parseIndexState rawCabalProject;
41+
42+
# Lookup hash for the index state we found
43+
index-sha256-found = if index-sha256 != null
44+
then index-sha256
45+
else index-state-hashes.${index-state-found} or null;
46+
47+
in
48+
assert (if index-state-found == null
49+
then throw "No index state passed and none found in cabal.project" else true);
50+
assert (if index-sha256-found == null
51+
then throw "provided sha256 for index-state ${index-state-found} is null!" else true);
52+
53+
let
2154
plan = runCommand "plan-to-nix-pkgs" {
2255
nativeBuildInputs = [ nix-tools ghc hpack cabal-install pkgs.rsync pkgs.git ];
2356
} ''
@@ -32,7 +65,9 @@ let
3265
find . -name package.yaml -exec hpack "{}" \;
3366
export SSL_CERT_FILE=${cacert}/etc/ssl/certs/ca-bundle.crt
3467
export GIT_SSL_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt
35-
HOME=${dotCabal { inherit index-state; sha256 = index-sha256; }} cabal new-configure \
68+
HOME=${dotCabal {
69+
index-state = index-state-found;
70+
sha256 = index-sha256-found; }} cabal new-configure \
3671
--with-ghc=${ghc.targetPrefix}ghc \
3772
--with-ghc-pkg=${ghc.targetPrefix}ghc-pkg
3873

test/call-cabal-project-to-nix/default.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ let
88
index-state = "2019-04-30T00:00:00Z";
99
# reuse the cabal-simple test project
1010
src = ../cabal-simple;
11+
# Hydra currently has issues reading from files in the store
12+
cabalProject = builtins.readFile ../cabal-simple/cabal.project;
1113
});
1214
};
1315
packages = pkgSet.config.hsPkgs;

0 commit comments

Comments
 (0)