Skip to content

Commit ae18fc3

Browse files
committed
survey: Make Cabal patches more robust.
See comment for details, and NixOS/nixpkgs#48567.
1 parent d32d120 commit ae18fc3

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

survey/default.nix

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -218,31 +218,43 @@ let
218218

219219
# Cherry-picking cabal fixes
220220

221-
# Note:
222-
# Be careful when changing any of the patches (both direct and
223-
# `file://` based), the above SHA might incorrectly cache it:
224-
# https://github.com/NixOS/nixpkgs/issues/48567
225-
# You may have to arbitrarily change the SHAs to see your changes
226-
# reflected.
227-
# TODO: Work around this by using `runCommand` or similar on a
228-
# plain fetchpatch, invoking `filterdiff` directly.
229-
makeCabalPatch = { name, url, fetchedSha256, processedSha256 }:
221+
makeCabalPatch = { name, url, sha256 }:
230222
let
231-
patchOnCabalLibraryFilesOnly = pkgs.fetchpatch {
232-
name = "${name}-Cabal-files-only";
233-
inherit url;
234-
sha256 = fetchedSha256;
235-
includes = ["Cabal/*"]; # Note `fetchpatch`'s `filterdiff` invocation runs with `-p1` as of writing
236-
excludes = ["Cabal/ChangeLog.md"]; # Changelog files almost always conflict
237-
};
223+
# We use `runCommand` on a plain patch file instead of using
224+
# `fetchpatch`'s `includes` or `stripLen` features to not run
225+
# into the perils of:
226+
# https://github.com/NixOS/nixpkgs/issues/48567
227+
plainPatchFile = pkgs.fetchpatch { inherit name url sha256; };
228+
229+
# Explanation:
230+
# * A patch created for the cabal project's source tree will
231+
# always have subdirs `Cabal` and `cabal-install`; the
232+
# `Cabal` nix derivation is already the `Cabal` subtree.
233+
# * We run `filterdiff -i` to keep only changes from the patch
234+
# that apply to the `Cabal` subtree.
235+
# * We run `filterdiff -x` to remove Changelog files which
236+
# almost always conflict.
237+
# * `-p1` strips away the `a/` and `b/` before `-i`/`-x` apply.
238+
# * `strip=2` removes e.g `a/Cabal` so that the patch applies
239+
# directly to that source tree, `--add*prefix` adds back the
240+
# `a/` and `b/` that `patch -p1` expects.
241+
patchOnCabalLibraryFilesOnly = pkgs.runCommand "${name}-Cabal-only" {} ''
242+
${pkgs.patchutils}/bin/filterdiff \
243+
-p1 -i 'Cabal/*' -x 'Cabal/ChangeLog.md' \
244+
--strip=2 --addoldprefix=a/ --addnewprefix=b/ \
245+
${plainPatchFile} > $out
246+
247+
if [ ! -s "$out" ]; then
248+
echo "error: Filtered patch '$out' is empty (while the original patch file was not)!" 1>&2
249+
echo "Check your includes and excludes." 1>&2
250+
echo "Normalizd patch file was:" 1>&2
251+
cat "${plainPatchFile}" 1>&2
252+
exit 1
253+
fi
254+
'';
255+
238256
in
239-
pkgs.fetchpatch {
240-
inherit name;
241-
url = "file://${patchOnCabalLibraryFilesOnly}";
242-
sha256 = processedSha256;
243-
stripLen = 2; # strips of `a/Cabal/` (the nix derivation is already built from this subdir)
244-
extraPrefix = "";
245-
};
257+
patchOnCabalLibraryFilesOnly;
246258

247259
applyPatchesToCabalDrv = cabalDrv: pkgs.haskell.lib.overrideCabal cabalDrv (old: {
248260
patches =
@@ -256,8 +268,7 @@ let
256268
(lib.optional (pkgs.lib.versionOlder cabalDrv.version "2.4.0.0") (makeCabalPatch {
257269
name = "5356.patch";
258270
url = "https://github.com/haskell/cabal/commit/fd6ff29e268063f8a5135b06aed35856b87dd991.patch";
259-
fetchedSha256 = "1sklr5ckwllmhzy0hjk284a4n5wad70si9wnqaqc5i7m9jzpg2w2";
260-
processedSha256 = "10qxqshkv044d4s547gxhwwnn4d1bbq52g70nhsfjgpnj1d0qc2r";
271+
sha256 = "1l5zwrbdrra789c2sppvdrw3b8jq241fgavb8lnvlaqq7sagzd1r";
261272
}))
262273
# Patches that as of writing aren't merged yet:
263274
]) ++ [
@@ -270,15 +281,13 @@ let
270281
(makeCabalPatch {
271282
name = "5446.patch";
272283
url = "https://github.com/haskell/cabal/commit/748f07b50724f2618798d200894f387020afc300.patch";
273-
fetchedSha256 = "1zmbalkdbd1xyf0kw5js74bpifhzhm16c98kn7kkgrwql1pbdyp5";
274-
processedSha256 = "0a726x2jkcmid0nynfhh6klgx4yb1igvjrmgnlxwchddqvyzcx86";
284+
sha256 = "1zmbalkdbd1xyf0kw5js74bpifhzhm16c98kn7kkgrwql1pbdyp5";
275285
})
276286
else
277287
(makeCabalPatch {
278288
name = "5446.patch";
279289
url = "https://github.com/haskell/cabal/commit/cb221c23c274f79dcab65aef3756377af113ae21.patch";
280-
fetchedSha256 = "1jfj9la2xgw8b9shqq82ffqa855ghp2ink30m7wvlxk5n1nl0jy9";
281-
processedSha256 = "0mpbia4dml52j5mjgyybl64czg470x8rqx0l9ap878sk09ikkgs5";
290+
sha256 = "02qalj5y35lq22f19sz3c18syym53d6bdqzbnx9f6z3m7xg591p1";
282291
})
283292
)
284293
# TODO Move this into the above section when merged in some Cabal version:
@@ -290,15 +299,13 @@ let
290299
(makeCabalPatch {
291300
name = "5451.patch";
292301
url = "https://github.com/haskell/cabal/commit/b66be72db3b34ea63144b45fcaf61822e0fade87.patch";
293-
fetchedSha256 = "0hndkfb96ry925xzx85km8y8pfv5ka5jz3jvy3m4l23jsrsd06c9";
294-
processedSha256 = "0ci7ch6csgm9282qh9pdypz6gzwwv8j0vlyfa8135zd8zpdx2zsw";
302+
sha256 = "0hndkfb96ry925xzx85km8y8pfv5ka5jz3jvy3m4l23jsrsd06c9";
295303
})
296304
else
297305
(makeCabalPatch {
298306
name = "5451.patch";
299307
url = "https://github.com/haskell/cabal/commit/0aeb541393c0fce6099ea7b0366c956e18937791.patch";
300-
fetchedSha256 = "0pa9r79730n1kah8x54jrd6zraahri21jahasn7k4ng30rnnidgz";
301-
processedSha256 = "1x1rgr9psrsrm73ml0gla89x6x5wfizv99579j43daqib0ivhr71";
308+
sha256 = "0pa9r79730n1kah8x54jrd6zraahri21jahasn7k4ng30rnnidgz";
302309
})
303310
)
304311
];

0 commit comments

Comments
 (0)