on iko/ghcjs-patch-master help sign in
Use fetchGit for source-repo-package without sha (#554)
1f94c06Hamish Mackenzie
docs/user-guide/source-repository-hashes.md+24
@@ -48,3 +48,27 @@ extra-deps:
48 # nix-sha256: 003lm3pm024vhbfmii7xcdd9v2rczpflxf7gdl2pyxia7p014i8z
48 # nix-sha256: 003lm3pm024vhbfmii7xcdd9v2rczpflxf7gdl2pyxia7p014i8z
49```
49```
50
50
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+33−9
@@ -16,6 +16,13 @@
16 # If the tests and benchmarks are not needed and they
16 # If the tests and benchmarks are not needed and they
17 # causes the wrong plan to be choosen, then we can use
17 # causes the wrong plan to be choosen, then we can use
18 # `configureArgs = "--disable-tests --disable-benchmarks";`
18 # `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}";
19, ...
26, ...
20}@args:
27}@args:
21# cabal-install versions before 2.4 will generate insufficient plan information.
28# cabal-install versions before 2.4 will generate insufficient plan information.
@@ -94,21 +101,38 @@ let
94 (builtins.head pair)
101 (builtins.head pair)
95 (builtins.elemAt pair 1))) blockLines);
102 (builtins.elemAt pair 1))) blockLines);
96
103
97− fetchRepo = repo: (pkgs.fetchgit {
104+ hashPath = path:
98− url = repo.location;
105+ builtins.readFile (pkgs.runCommand "hash-path" { preferLocalBuild = true; }
99− rev = repo.tag;
106+ "echo -n $(${pkgs.nix}/bin/nix-hash --type sha256 --base32 ${path}) > $out");
100− sha256 = repo."--sha256";
101− }) + (if repo.subdir or "" == "" then "" else "/" + repo.subdir);
102
107
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+
103− # Parse a source-repository-package and fetch it if it containts
129+ # Parse a source-repository-package and fetch it if has `type: git`
104− # a line of the form
105− # --shar256: <<SHA256>>
106 parseBlock = block:
130 parseBlock = block:
107 let
131 let
108 x = span (pkgs.lib.strings.hasPrefix " ") (pkgs.lib.splitString "\n" block);
132 x = span (pkgs.lib.strings.hasPrefix " ") (pkgs.lib.splitString "\n" block);
109 attrs = parseBlockLines x.fst;
133 attrs = parseBlockLines x.fst;
110 in
134 in
111− if attrs."--sha256" or "" == ""
135+ if attrs."type" or "" != "git"
112 then {
136 then {
113 sourceRepo = [];
137 sourceRepo = [];
114 otherText = "\nsource-repository-package\n" + block;
138 otherText = "\nsource-repository-package\n" + block;
test/default.nix+1
@@ -174,6 +174,7 @@ let
174 ghc-options-stack = callTest ./ghc-options/stack.nix {};
174 ghc-options-stack = callTest ./ghc-options/stack.nix {};
175 exe-only = callTest ./exe-only { inherit util; };
175 exe-only = callTest ./exe-only { inherit util; };
176 stack-source-repo = callTest ./stack-source-repo {};
176 stack-source-repo = callTest ./stack-source-repo {};
177+ lookup-sha256 = callTest ./lookup-sha256 {};
177
178
178 unit = unitTests;
179 unit = unitTests;
179 };
180 };
test/lookup-sha256/default.nixnew file+16
@@ -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+