@@ -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