@@ -2,10 +2,7 @@
2# https://github.com/NixOS/nixpkgs/blob/1d9d31a0eb8e8358830528538a391df52f6a075a/lib/sources.nix#L41
2# https://github.com/NixOS/nixpkgs/blob/1d9d31a0eb8e8358830528538a391df52f6a075a/lib/sources.nix#L41
3# It adds a subDir argument in a way that allows descending into a subdirectory
3# It adds a subDir argument in a way that allows descending into a subdirectory
4# to compose with cleaning the source with a filter.
4# to compose with cleaning the source with a filter.
5−{ lib }:
5+{ lib }: rec {
6−if lib.versionOlder builtins.nixVersion "2.4"
7−then throw "Nix version 2.4 or higher is required for Haskell.nix"
10 # Like `builtins.filterSource`, except it will compose with itself,
7 # Like `builtins.filterSource`, except it will compose with itself,
11 # allowing you to chain multiple calls together without any
8 # allowing you to chain multiple calls together without any
@@ -53,6 +50,33 @@ else rec {
53 # (and forward a `name` argument) can use this to make
50 # (and forward a `name` argument) can use this to make
54 # the message to the use more meaningful.
51 # the message to the use more meaningful.
53+ # Nix <2.4 Suport with filterPath
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`).
60+ # To disable filtering completely replace:
66+ # src = { outPath = ./.; filterPath = { path, ... }: path; };
68+ # To disable filtering only when the path is in the store use (when Nix <2.4 is unable to filter it):
72+ # filterPath = { path, ... }@args:
73+ # if builtins.hasContext (toString path) then path else builtins.path args;
76+ # Currently `haskell-nix.hackage-project` (used by `hackage-package`, `tool` and `tools`) disables
77+ # filtering. See `overlays/haskell.nix` for details.
79+ # For more see https://github.com/input-output-hk/haskell.nix/pull/1418
56 cleanSourceWith = { filter ? _path: _type: true, src, subDir ? "", name ? null
80 cleanSourceWith = { filter ? _path: _type: true, src, subDir ? "", name ? null
57 , caller ? "cleanSourceWith", includeSiblings ? false }:
81 , caller ? "cleanSourceWith", includeSiblings ? false }:
@@ -106,10 +130,17 @@ else rec {
106 __trace (
130 __trace (
107 "WARNING: `${caller}` called on ${toString src} without a `name`. "
131 "WARNING: `${caller}` called on ${toString src} without a `name`. "
108 + "Consider adding `name = \"${baseNameOf src}\";`") "source";
132 + "Consider adding `name = \"${baseNameOf src}\";`") "source";
133+ filterPath = origSrc.filterPath or (
134+ if builtins.compareVersions builtins.nixVersion "2.4" >= 0
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));
110 inherit origSrc origSubDir origSrcSubDir;
141 inherit origSrc origSubDir origSrcSubDir;
111 filter = filter';
142 filter = filter';
112− outPath = (builtins.path { filter = filter'; path = origSrc; name = name'; }) + origSubDir;
143+ outPath = (filterPath { filter = filter'; path = origSrc; name = name'; }) + origSubDir;
113 _isLibCleanSourceWithEx = true;
144 _isLibCleanSourceWithEx = true;
114 # It is only safe for older cleanSourceWith to filter this one
145 # It is only safe for older cleanSourceWith to filter this one
115 # if the we are still looking at the root of origSrc
146 # if the we are still looking at the root of origSrc