on iko/ghcjs-patch-master help sign in
cleanSourceHaskell: Add more filters (#314)

* cleanSourceHaskell: Add more filters * cleanSourceHaskell: Add a "name" parameter, split away the filter * cleanSourceHaskell: Make it compatible with nixpkgs-19.03 * Adjust usage of cleanSourceHaskell in nix-tools

commit2e4b4ebbb3a0b627fc5e80b11db508306e8bb767 authorRodney Lorrimar <rodney.lorrimar@iohk.io> dateMon 18 Nov 2019, 16:46 (7 years ago) parent5fa379e
5 files changed+106−31
changelog.md+6 docs/reference/library.md+34 lib/clean-source-haskell.nix+54−24 nix-tools/default.nix+8−5 overlays/haskell.nix+4−2
changelog.md+6
@@ -1,6 +1,12 @@
11This file contains a summary of changes to Haskell.nix and `nix-tools`
22that will impact users.
33
4+## November 18, 2019
5+ * Changed the `cleanSourceHaskell` to accept an attrset of `src` and
6+ (optional) `name` parameters. This allows you to keep the source
7+ derivation name constant, so that your builds are always
8+ cached. Usage of `cleanSourceHaskell` will need to be updated.
9+
410## October 12, 2019
511 * [`shellFor`](https://input-output-hk.github.io/haskell.nix/reference/library/#shellfor) no longer sets `CABAL_CONFIG` by default.
612 This avoids surprising users, but means that Cabal may select a plan which is different to your Haskell.nix package set.
docs/reference/library.md+34
@@ -241,6 +241,40 @@ The result is an attrset with the following values:
241241
242242## cleanSourceHaskell
243243
244+```
245+cleanSourceHaskell = { src, name ? null }: ...
246+```
247+
248+Filters a source tree removing common filenames that are not Haskell
249+build sources.
250+
251+This can avoid unecessary rebuilds when these files change.
252+
253+It's recommended to provide `name` so that the source derivation
254+remains constant regardless of how it was fetched.
255+
256+Example:
257+
258+```
259+src = pkgs.haskell-nix.cleanSourceHaskell {
260+ src = ./.;
261+ name = "myproject-src";
262+};
263+```
264+
265+## haskellSourceFilter
266+
267+```
268+haskellSourceFilter = name: type: ...
269+```
270+
271+This is a source filter function which cleans common build products
272+and files not needed to do a Haskell build from a source directory.
273+
274+It should be used with `pkgs.lib.cleanSourceWith`. Alternatively,
275+use the convenience function [`cleanSourceHaskell`](#cleansourcehaskell).
276+
277+
244278## haskellLib
245279
246280Assorted functions for operating on [Haskell.nix][] data. This is
lib/clean-source-haskell.nix+54−24
@@ -1,25 +1,55 @@
1−# This function cleans common build products and files not needed to do a
2−# haskell build from a source directory.
1+# This is a source filter function which cleans common build products
2+# and files not needed to do a haskell build from a source directory.
3+#
34# 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−# haskell.nix and made available to projects that do not use iohk-nix.
6−# The original version is here:
7−# https://github.com/input-output-hk/iohk-nix/blob/c01bbd4e0c55a4101e2078d24046e0b2a731f871/clean-source-haskell.nix
8−{ pkgs }: src:
9− let lib = pkgs.lib;
10− in if (builtins.typeOf src) == "path"
11− then lib.cleanSourceWith {
12− filter = with pkgs.stdenv;
13− name: type: let baseName = baseNameOf (toString name); in ! (
14− # Filter out cabal build products.
15− baseName == "dist" || baseName == "dist-newstyle" ||
16− baseName == "cabal.project.local" ||
17− lib.hasPrefix ".ghc.environment" baseName ||
18− # Filter out stack build products.
19− lib.hasPrefix ".stack-work" baseName ||
20− # Filter out files which are commonly edited but don't
21− # affect the cabal build.
22− lib.hasSuffix ".nix" baseName
23− );
24− src = lib.cleanSource src;
25− } else src
5+#
6+# It should be used with "pkgs.lib.cleanSourceWith". Alternatively,
7+# use the convenience function "cleanSourceHaskell".
8+#
9+{ lib }:
10+
11+rec {
12+ haskellSourceFilter = name: type:
13+ let baseName = baseNameOf (toString name);
14+ in ! (
15+ # Filter out cabal build products.
16+ baseName == "dist" || baseName == "dist-newstyle" ||
17+ baseName == "cabal.project.local" ||
18+ lib.hasPrefix ".ghc.environment" baseName ||
19+ # Filter out stack build products.
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" ||
25+ # Filter out files which are commonly edited but don't
26+ # affect the cabal build.
27+ lib.hasSuffix ".nix" baseName
28+ );
29+
30+ # Like pkgs.lib.cleanSource, but adds Haskell files to the filter.
31+ cleanSourceHaskell = { src, name ? null }:
32+ let
33+ clean = lib.cleanSourceWith {
34+ filter = haskellSourceFilter;
35+ src = lib.cleanSource src;
36+ inherit name;
37+ };
38+ # Copy function from 19.09 for compatibility with 19.03.
39+ # Does require at least Nix 2.0.
40+ compat = let
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;
45+ in {
46+ inherit origSrc;
47+ filter = filter';
48+ outPath = builtins.path { filter = filter'; path = origSrc; name = name'; };
49+ _isLibCleanSourceWith = true;
50+ name = name';
51+ };
52+ in
53+ if (builtins.typeOf src) == "path"
54+ then (if lib.versionAtLeast lib.version "19.09" then clean else compat) else src;
55+}
nix-tools/default.nix+8−5
@@ -4,11 +4,14 @@
44, ... }@args:
55
66let
7− src = cleanSourceHaskell (fetchExternal {
8− name = "nix-tools-src";
9− specJSON = ./nix-tools-src.json;
10− override = "nix-tools-src";
11− });
7+ src = cleanSourceHaskell {
8+ src = fetchExternal {
9+ name = "nix-tools-src";
10+ specJSON = ./nix-tools-src.json;
11+ override = "nix-tools-src";
12+ };
13+ };
14+
1215 # we need to patch Cabal, as its data directory logic is broken for component builds, which haskell.nix
1316 # uses excessively. See issue https://github.com/haskell/cabal/issues/5862, and the fix for Cabal 3.0
1417 # in https://github.com/haskell/cabal/pull/6055. We apply the haskell/cabal#6055 here.
overlays/haskell.nix+4−2
@@ -31,8 +31,10 @@ self: super: {
3131 # overridden with NIX_PATH.
3232 fetchExternal = import ../lib/fetch-external.nix;
3333
34− # Function for cleaning haskell source directories pulled from iohk-nix
35− cleanSourceHaskell = import ../lib/clean-source-haskell.nix { pkgs = self; };
34+ # Functions for cleaning Haskell source directories.
35+ inherit (import ../lib/clean-source-haskell.nix { inherit (self) lib; })
36+ haskellSourceFilter
37+ cleanSourceHaskell;
3638
3739 # All packages from Hackage as Nix expressions
3840 hackageSrc = fetchExternal {