on iko/ghcjs-patch-master help sign in
Support Nix <2.4 (#1418)

* Downgrades the error to a warning. * Adds `filterPath` as a way to disable filtering to have caching work between Nix <2.4 and >=2.4. * Disables filtering on `haskell-nix.hackage-project` based functions (should make all the GHC derivations the same). I think it would also be possible to make a `filterPath` implementation that recursively used `builtins.readDir` and called the `filter` function to implement filtering on Nix <2.4, but I am not sure if it is worth it. Anyone that needs the old behaviour for project packages (consistent filtering between <2.4 and >=2.4, but not between store and local) replace: ``` project = pkgs.haskell-nix.cabalProject { src = pkgs.haskell-nix.haskellLib.cleanGit { src = ./.; }; }; ``` ``` project = pkgs.haskell-nix.cabalProject { src = pkgs.haskell-nix.haskellLib.cleanGit { src = { outPath = ./.; filterPath = { path, ... }@args: if builtins.hasContext (toString path) then path else builtins.path args; }; }; }; ```

commit8eb5d29d374ec017a3352fea7498a414b5aef33f authorHamish Mackenzie <Hamish.K.Mackenzie@gmail.com> dateTue 29 Mar 2022, 01:24 (5 years ago) parent9660601
2 files changed+44−7
lib/clean-source-with.nix+36−5 overlays/haskell.nix+8−2
lib/clean-source-with.nix+36−5
@@ -2,10 +2,7 @@
22# https://github.com/NixOS/nixpkgs/blob/1d9d31a0eb8e8358830528538a391df52f6a075a/lib/sources.nix#L41
33# It adds a subDir argument in a way that allows descending into a subdirectory
44# to compose with cleaning the source with a filter.
5−{ lib }:
6−if lib.versionOlder builtins.nixVersion "2.4"
7−then throw "Nix version 2.4 or higher is required for Haskell.nix"
8−else rec {
5+{ lib }: rec {
96
107 # Like `builtins.filterSource`, except it will compose with itself,
118 # allowing you to chain multiple calls together without any
@@ -53,6 +50,33 @@ else rec {
5350 # (and forward a `name` argument) can use this to make
5451 # the message to the use more meaningful.
5552 #
53+ # Nix <2.4 Suport with filterPath
54+ #
55+ # As of Nix >=2.4 filtering is now possible on all paths (including those in the store).
56+ # If you still need to support older versions of Nix and want to maximise cache hits for your project
57+ # you may want to consider using setting the `filterPath` attribute on `src` (must be done on
58+ # the original src not the output of another `cleanSourceWith`).
59+ #
60+ # To disable filtering completely replace:
61+ #
62+ # src = ./.;
63+ #
64+ # with:
65+ #
66+ # src = { outPath = ./.; filterPath = { path, ... }: path; };
67+ #
68+ # To disable filtering only when the path is in the store use (when Nix <2.4 is unable to filter it):
69+ #
70+ # src = {
71+ # outPath = ./.;
72+ # filterPath = { path, ... }@args:
73+ # if builtins.hasContext (toString path) then path else builtins.path args;
74+ # };
75+ #
76+ # Currently `haskell-nix.hackage-project` (used by `hackage-package`, `tool` and `tools`) disables
77+ # filtering. See `overlays/haskell.nix` for details.
78+ #
79+ # For more see https://github.com/input-output-hk/haskell.nix/pull/1418
5680 cleanSourceWith = { filter ? _path: _type: true, src, subDir ? "", name ? null
5781 , caller ? "cleanSourceWith", includeSiblings ? false }:
5882 let
@@ -106,10 +130,17 @@ else rec {
106130 __trace (
107131 "WARNING: `${caller}` called on ${toString src} without a `name`. "
108132 + "Consider adding `name = \"${baseNameOf src}\";`") "source";
133+ filterPath = origSrc.filterPath or (
134+ if builtins.compareVersions builtins.nixVersion "2.4" >= 0
135+ then builtins.path
136+ else __trace "WARNING: Using nix <2.4 will result in inconsistent filtering of ${toString origSrc} (see lib/clean-source-with.nix)"
137+ (if builtins.hasContext or (lib.hasPrefix builtins.storeDir) (toString origSrc)
138+ then { path, ... }: path
139+ else builtins.path));
109140 in {
110141 inherit origSrc origSubDir origSrcSubDir;
111142 filter = filter';
112− outPath = (builtins.path { filter = filter'; path = origSrc; name = name'; }) + origSubDir;
143+ outPath = (filterPath { filter = filter'; path = origSrc; name = name'; }) + origSubDir;
113144 _isLibCleanSourceWithEx = true;
114145 # It is only safe for older cleanSourceWith to filter this one
115146 # if the we are still looking at the root of origSrc
overlays/haskell.nix+8−2
@@ -471,14 +471,20 @@ final: prev: {
471471 inherit (rev) sha256;
472472 };
473473 revSuffix = final.lib.optionalString (rev.revNum > 0) "-r${toString rev.revNum}";
474− in let src = final.buildPackages.pkgs.runCommand "${name}-${version'}${revSuffix}-src" { } (''
474+ in let src = final.buildPackages.pkgs.runCommand "${name}-${version'}${revSuffix}-src" {} (''
475475 tmp=$(mktemp -d)
476476 cd $tmp
477477 tar xzf ${tarball}
478478 mv "${name}-${version'}" $out
479479 '' + final.lib.optionalString (rev.revNum > 0) ''
480480 cp ${cabalFile} $out/${name}.cabal
481− '');
481+ '') // {
482+ # TODO remove once nix >=2.4 is widely adopted (will trigger rebuilds of everything).
483+ # Disable filtering keeps pre ond post nix 2.4 behaviour the same. This means that
484+ # the same `alex`, `happy` and `hscolour` are used to build GHC. It also means that
485+ # that `tools` in the shell will be built the same.
486+ filterPath = { path, ... }: path;
487+ };
482488 in cabalProject' (
483489 (final.haskell-nix.hackageQuirks { inherit name; version = version'; }) //
484490 builtins.removeAttrs args [ "version" "revision" ] // { inherit src; });