@@ -1,25 +1,55 @@
1−# This function cleans common build products and files not needed to do a
1+# This is a source filter function which cleans common build products
2−# haskell build from a source directory.
2+# and files not needed to do a haskell build from a source directory.
3# This can avoid unecessary builds when these files change.
4# This can avoid unecessary builds when these files change.
4−# It has been copied from iohk-nix so that it can be used internally in
5+#
5−# haskell.nix and made available to projects that do not use iohk-nix.
6+# It should be used with "pkgs.lib.cleanSourceWith". Alternatively,
6−# The original version is here:
7+# use the convenience function "cleanSourceHaskell".
7−# https://github.com/input-output-hk/iohk-nix/blob/c01bbd4e0c55a4101e2078d24046e0b2a731f871/clean-source-haskell.nix
8+#
8−{ pkgs }: src:
9+{ lib }:
9− let lib = pkgs.lib;
10+
10− in if (builtins.typeOf src) == "path"
11+rec {
11− then lib.cleanSourceWith {
12+ haskellSourceFilter = name: type:
12− filter = with pkgs.stdenv;
13− name: type: let baseName = baseNameOf (toString name); in ! (
13+ let baseName = baseNameOf (toString name);
14− # Filter out cabal build products.
15+ # Filter out cabal build products.
15− baseName == "dist" || baseName == "dist-newstyle" ||
16+ baseName == "dist" || baseName == "dist-newstyle" ||
16− baseName == "cabal.project.local" ||
17+ baseName == "cabal.project.local" ||
17− lib.hasPrefix ".ghc.environment" baseName ||
18+ lib.hasPrefix ".ghc.environment" baseName ||
18− # Filter out stack build products.
19+ # Filter out stack build products.
19− lib.hasPrefix ".stack-work" baseName ||
20+ lib.hasPrefix ".stack-work" baseName ||
21+ # Filter out files left by ghci
22+ lib.hasSuffix ".hi" baseName ||
23+ # Filter out files generated by "hasktags"
24+ baseName == "TAGS" || baseName == "tags" ||
20− # Filter out files which are commonly edited but don't
25+ # Filter out files which are commonly edited but don't
21− # affect the cabal build.
26+ # affect the cabal build.
22− lib.hasSuffix ".nix" baseName
27+ lib.hasSuffix ".nix" baseName
30+ # Like pkgs.lib.cleanSource, but adds Haskell files to the filter.
31+ cleanSourceHaskell = { src, name ? null }:
33+ clean = lib.cleanSourceWith {
34+ filter = haskellSourceFilter;
24− src = lib.cleanSource src;
35+ src = lib.cleanSource src;
25− } else src
36+ inherit name;
38+ # Copy function from 19.09 for compatibility with 19.03.
39+ # Does require at least Nix 2.0.
41+ isFiltered = src ? _isLibCleanSourceWith;
42+ origSrc = if isFiltered then src.origSrc else src;
43+ filter' = name: type: haskellSourceFilter name type && lib.cleanSourceFilter name type;
44+ name' = if name != null then name else if isFiltered then src.name else baseNameOf src;
48+ outPath = builtins.path { filter = filter'; path = origSrc; name = name'; };
49+ _isLibCleanSourceWith = true;
53+ if (builtins.typeOf src) == "path"
54+ then (if lib.versionAtLeast lib.version "19.09" then clean else compat) else src;