on iko/ghcjs-patch-master help sign in
Add haskellLib.collectComponents (#224)
commitd4869aab48bc29c13dc44e0ec4c5b1fd953b1b94 authorRodney Lorrimar <rodney.lorrimar@iohk.io> dateSat 24 Aug 2019, 18:29 (7 years ago) parent0498d68
3 files changed+66−5
changelog.md+3 docs/reference/library.md+45−5 lib/default.nix+18
changelog.md+3
@@ -1,6 +1,9 @@
11This file contains a summary of changes to Haskell.nix and `nix-tools`
22that will impact users.
33
4+## August 9, 2019
5+ * Add the [`haskellLib.collectComponents`](https://input-output-hk.github.io/haskell.nix/reference/library/#haskellLib) function.
6+
47## June 21, 2019
58 * Add `ghcWithPackages` and `ghcWithHoogle` to hsPkgs ([documentation](https://input-output-hk.github.io/haskell.nix/reference/library/#package-set-functions).
69 * Benchmark components can now build successfully.
docs/reference/library.md+45−5
@@ -73,7 +73,7 @@ components = {
7373 all = COMPONENT;
7474}
7575```
76−
76+
7777## Component
7878
7979In [Haskell.nix][], a _component_ is a derivation corresponding to a
@@ -104,7 +104,7 @@ set. They are either:
104104If using the function form of a module, the following named parameters
105105will be passed to it:
106106
107−| Argument | Type | Description |
107+| Argument | Type | Description |
108108|------------------|------|---------------------|
109109| `haskellLib` | attrset | The [haskellLib](#haskelllib) utility functions. |
110110| `pkgs` | | The Nixpkgs collection. |
@@ -126,7 +126,7 @@ mkStackPkgSet =
126126 { stack-pkgs, pkg-def-extras ? [], modules ? []}: ...
127127```
128128
129−| Argument | Type | Description |
129+| Argument | Type | Description |
130130|------------------|------|---------------------|
131131| `stack-pkgs` | | `import ./pkgs.nix` — The imported file generated by `stack‑to‑nix`. |
132132| `pkg‑def‑extras` | List of [Extras](#extras) | For overriding the package set. |
@@ -144,7 +144,7 @@ mkCabalProjectPkgSet =
144144 { plan-pkgs, pkg-def-extras ? [], modules ? []}: ...
145145```
146146
147−| Argument | Type | Description |
147+| Argument | Type | Description |
148148|------------------|------|---------------------|
149149| `plan-pkgs` | | `import ./pkgs.nix` — The imported file generated by `plan‑to‑nix`. |
150150| `pkg‑def‑extras` | List of [Extras](#extras) | For overriding the package set. |
@@ -192,6 +192,46 @@ Assorted functions for operating on [Haskell.nix][] data. This is
192192distinct from `pkgs.haskell.lib` in the current Nixpkgs Haskell
193193Infrastructure.
194194
195+### collectComponents
196+
197+Extracts a selection of components from a Haskell [package set](#package-set).
198+
199+This can be used to filter out all test suites or benchmarks of
200+your project, so that they can be built in Hydra.
201+
202+```
203+collectComponents =
204+ group: packageSel: haskellPackages: ...
205+```
206+
207+
208+| Argument | Type | Description |
209+|-------------------|--------|---------------------|
210+| `group` | String | A [sub-component type](#subComponentTypes). |
211+| `packageSel` | A function `Package -> Bool` | A predicate to filter packages with. |
212+| `haskellPackages` | [Package set](#package-set) | All packages in the build. |
213+
214+**Return value**: a recursive attrset mapping package names → component names → components.
215+
216+**Example**:
217+
218+```nix
219+tests = collectComponents "tests" (package: package.identifier.name == "mypackage") hsPkgs;
220+```
221+
222+Will result in moving derivations from `hsPkgs.mypackage.components.tests.unit-tests`
223+to `tests.mypackage.unit-tests`.
224+
225+#### subComponentTypes
226+
227+Sub-component types identify [components](#component) and are one of:
228+
229+ - `sublibs`
230+ - `foreignlibs`
231+ - `exes`
232+ - `tests`
233+ - `benchmarks`
234+
195235# Package-set functions
196236
197237These functions exist within the `hsPkgs` package set.
@@ -208,7 +248,7 @@ shellFor =
208248```
209249
210250
211−| Argument | Type | Description |
251+| Argument | Type | Description |
212252|----------------|------|---------------------|
213253| `packages` | Function | Package selection function. It takes a list of [Haskell packages](#haskell-package) and returns a subset of these packages. |
214254| `withHoogle` | Boolean | Whether to build a Hoogle documentation index and provide the `hoogle` command. |
lib/default.nix+18
@@ -94,4 +94,22 @@ with haskellLib;
9494 operator = {val,...}: makePairs val.config.depends;
9595 };
9696 in map ({val,...}: val) closure;
97+
98+ # Extracts a selection of components from a Haskell package set.
99+ #
100+ # This can be used to filter out all test suites or benchmarks of
101+ # your project, so that they can be built in Hydra.
102+ #
103+ # For example:
104+ #
105+ # tests = collectComponents "tests" (package: package.identifier.name == "mypackage") hsPkgs;
106+ #
107+ # Will result in moving:
108+ # from: hsPkgs.mypackage.components.tests.unit-tests
109+ # to: tests.mypackage.unit-tests
110+ #
111+ collectComponents = group: packageSel: haskellPackages:
112+ (mapAttrs (_: package: package.components.${group} // { recurseForDerivations = true; })
113+ (filterAttrs (name: package: (package.isHaskell or false) && packageSel package) haskellPackages))
114+ // { recurseForDerivations = true; };
97115}