Skip to content

Commit 1f94c06

Browse files
authored
Use fetchGit for source-repo-package without sha (#554)
* Downloads using `builtins.fetchGit` when no sha is found. * Adds a `lookupSha256` arg for when we can't change `cabal.project`. * Outputs a message when the sha256 is missing that includes advice on how add one.
1 parent d1102d3 commit 1f94c06

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

docs/user-guide/source-repository-hashes.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,27 @@ extra-deps:
4848
# nix-sha256: 003lm3pm024vhbfmii7xcdd9v2rczpflxf7gdl2pyxia7p014i8z
4949
```
5050

51+
## lookupSha256
52+
53+
In some cases we cannot modify the `cabal.project` file to add the
54+
`--sha256` comments. As an alternative we can pass in a `lookupSha256`
55+
function to get them. For instance pandoc includes a `cabal.project`
56+
file in hackage includes a `source-package-reference` to `pandoc-citeproc`:
57+
58+
```
59+
{ haskell-nix, testSrc } :
60+
let
61+
pandoc = haskell-nix.hackage-package {
62+
name = "pandoc";
63+
version = "2.9.2.1";
64+
index-state = "2020-04-15T00:00:00Z";
65+
# Function that returns a sha256 string by looking up the location
66+
# and tag in a nested attrset
67+
lookupSha256 = { location, tag, ... }:
68+
{ "https://github.com/jgm/pandoc-citeproc"."0.17"
69+
= "0dxx8cp2xndpw3jwiawch2dkrkp15mil7pyx7dvd810pwc22pm2q"; }
70+
."${location}"."${tag}";
71+
};
72+
in
73+
pandoc.components.exes.pandoc
74+
```

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
# If the tests and benchmarks are not needed and they
1717
# causes the wrong plan to be choosen, then we can use
1818
# `configureArgs = "--disable-tests --disable-benchmarks";`
19+
, lookupSha256 ? _: null
20+
# Use the as an alternative to adding `--sha256` comments into the
21+
# cabal.project file:
22+
# lookupSha256 = repo:
23+
# { "https://github.com/jgm/pandoc-citeproc"."0.17"
24+
# = "0dxx8cp2xndpw3jwiawch2dkrkp15mil7pyx7dvd810pwc22pm2q"; }
25+
# ."${repo.location}"."${repo.tag}";
1926
, ...
2027
}@args:
2128
# cabal-install versions before 2.4 will generate insufficient plan information.
@@ -94,21 +101,38 @@ let
94101
(builtins.head pair)
95102
(builtins.elemAt pair 1))) blockLines);
96103

97-
fetchRepo = repo: (pkgs.fetchgit {
98-
url = repo.location;
99-
rev = repo.tag;
100-
sha256 = repo."--sha256";
101-
}) + (if repo.subdir or "" == "" then "" else "/" + repo.subdir);
104+
hashPath = path:
105+
builtins.readFile (pkgs.runCommand "hash-path" { preferLocalBuild = true; }
106+
"echo -n $(${pkgs.nix}/bin/nix-hash --type sha256 --base32 ${path}) > $out");
102107

103-
# Parse a source-repository-package and fetch it if it containts
104-
# a line of the form
105-
# --shar256: <<SHA256>>
108+
# Use pkgs.fetchgit if we have a sha256. Add comment like this
109+
# --shar256: 003lm3pm0000hbfmii7xcdd9v20000flxf7gdl2pyxia7p014i8z
110+
# otherwise use __fetchGit.
111+
fetchRepo = repo:
112+
let sha256 = repo."--sha256" or (lookupSha256 repo);
113+
in (if sha256 != null
114+
then pkgs.fetchgit {
115+
url = repo.location;
116+
rev = repo.tag;
117+
inherit sha256;
118+
}
119+
else
120+
let drv = builtins.fetchGit {
121+
url = repo.location;
122+
ref = repo.tag;
123+
};
124+
in __trace "WARNING: No sha256 found for source-repository-package ${repo.location} ${repo.tag} download may fail in restricted mode (hydra)"
125+
(__trace "Consider adding `--sha256: ${hashPath drv}` to the cabal.project file or passing in a lookupSha256 argument"
126+
drv)
127+
) + (if repo.subdir or "" == "" then "" else "/" + repo.subdir);
128+
129+
# Parse a source-repository-package and fetch it if has `type: git`
106130
parseBlock = block:
107131
let
108132
x = span (pkgs.lib.strings.hasPrefix " ") (pkgs.lib.splitString "\n" block);
109133
attrs = parseBlockLines x.fst;
110134
in
111-
if attrs."--sha256" or "" == ""
135+
if attrs."type" or "" != "git"
112136
then {
113137
sourceRepo = [];
114138
otherText = "\nsource-repository-package\n" + block;

test/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ let
174174
ghc-options-stack = callTest ./ghc-options/stack.nix {};
175175
exe-only = callTest ./exe-only { inherit util; };
176176
stack-source-repo = callTest ./stack-source-repo {};
177+
lookup-sha256 = callTest ./lookup-sha256 {};
177178

178179
unit = unitTests;
179180
};

test/lookup-sha256/default.nix

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{ haskell-nix, testSrc } :
2+
let
3+
pandoc = haskell-nix.hackage-package {
4+
name = "pandoc";
5+
version = "2.9.2.1";
6+
index-state = "2020-04-15T00:00:00Z";
7+
# Function that returns a sha256 string by looking up the location
8+
# and tag in a nested attrset
9+
lookupSha256 = { location, tag, ... }:
10+
{ "https://github.com/jgm/pandoc-citeproc"."0.17"
11+
= "0dxx8cp2xndpw3jwiawch2dkrkp15mil7pyx7dvd810pwc22pm2q"; }
12+
."${location}"."${tag}";
13+
};
14+
in
15+
pandoc.components.exes.pandoc
16+

0 commit comments

Comments
 (0)