on c52845f help sign in
466 lines 18 kB history raw
1[Haskell.nix][] contains a library of functions for creating buildable
2package sets from their Nix expression descriptions. The library is
3what you get when importing [Haskell.nix][]. It might be helpful to
4load the library in the [Nix REPL](../tutorials/development.md#using-nix-repl) to
5test things.
7 * [Data structures](#data-structures) — the kinds of data that you will encounter working with [Haskell.nix][].
8 * [Top-level attributes](#top-level-attributes) — Functions and derivations defined in the Haskell.nix attrset.
9 * [Package-set functions](#package-set-functions) — Helper functions defined on the `hsPkgs` package set.
11# Data structures
13## Package Set
15The result of `mkPkgSet`. This is an application of the NixOS module
16system.
18```
20 options = { ... };
21 config = {
22 hsPkgs = { ... };
23 packages = { ... };
24 compiler = {
25 version = "X.Y.Z";
26 nix-name = "ghcXYZ";
27 packages = { ... };
28 };
29 };
31```
33| Attribute | Type | Description |
34|----------------|------|-----------------------------------------------------|
35| `options` | Module options | The combination of all options set through the `modules` argument passed to `mkPkgsSet`. |
36| `config` | | The result of evaluating and applying the `options` with [Haskell.nix][] |
37| `.hsPkgs` | Attrset of [Haskell Packages](#haskell-package) | Buildable packages, created from `packages` |
38| `.packages` | Attrset of [Haskell Package descriptions](#haskell-package-descriptions) | Configuration for each package in `hsPkgs` |
39| `.compiler` | Attrset | |
43## Haskell Package description
45The _Haskell package descriptions_ are values of the
46`pkgSet.config.packages` attrset. These are not derivations, but just
47the configuration for building an individual package. The
48configuration options are described under `packages.<name>` in [Module
49options](./modules.md).
51## Component description
53The _component descriptions_ are values of the
54`pkgSet.config.packages.<package>.components` attrset. These are not
55derivations, but just the configuration for building an individual
56component. The configuration options are described under
57`packages.<name>.components.*` in [Module options](./modules.md).
60## Haskell Package
62In [Haskell.nix][], a _Haskell package_ is a derivation which has a
63`components` attribute. This derivation is actually just for the
64package `Setup.hs` script, and isn't very interesting. To actually use
65the package, look within the components structure.
67```
68components = {
69 library = COMPONENT;
70 exes = { NAME = COMPONENT; };
71 tests = { NAME = COMPONENT; };
72 benchmarks = { NAME = COMPONENT; };
74```
76## Component
78In [Haskell.nix][], a _component_ is a derivation corresponding to a
79[Cabal component](https://www.haskell.org/cabal/users-guide/developing-packages.html)
80of a package.
82## Identifier
84A package identifier is an attrset pair of `name` and `version`.
86## Extras
88Extras allow adding more packages to the package set. These will be
89functions taking a single parameter `hackage`. They should return an
90attrset of package descriptions.
92## Modules
94Modules are the primary method of configuring building of the package
95set. They are either:
971. an attrset containing [option declarations](./modules.md), or
982. a function that returns an attrset containing option declarations.
100If using the function form of a module, the following named parameters
101will be passed to it:
103| Argument | Type | Description |
104|------------------|------|---------------------|
105| `haskellLib` | attrset | The [haskellLib](#haskelllib) utility functions. |
106| `pkgs` | | The Nixpkgs collection. |
107| `pkgconfPkgs` | | A mapping of cabal build-depends names to Nixpkgs packages. (TODO: more information about this) |
108| `buildModules` | | |
109| `config` | | |
110| `options` | | |
113# Top-level attributes
115## project
117Function that accepts attribute set with a `src` attribute and looks for `stack.yaml` file relative to it.
119If file exists, it calls [stackProject](#stackproject) function. Otherwise it will call [cabalProject](#cabalproject) function.
121**Example**:
123```nix
124pkgs.haskell-nix.project {
125 # 'cleanGit' cleans a source directory based on the files known by git
126 src = pkgs.haskell-nix.haskellLib.cleanGit {
127 name = "haskell-nix-project";
128 src = ./.;
129 };
131```
133## stackProject
135A function calling [callStackToNix](#callstacktonix) with all arguments.
137Then feeding its result into [mkStackPkgSet](#mkstackpkgset) passing also
138`pkg-def-extras` and `modules` arguments.
140**Return value**:
142| Attribute | Type | Description |
143|-------------------|--------------------------------------------------|----------------------------------------------------------------------------|
144| `hsPkgs` | Attrset of [Haskell Packages](#haskell-package) | Buildable packages, created from `packages` |
145| `pkg-set` | Attrset | [`pkgSet`](#package-set) |
146| `stack-nix` | | `projectNix` attribute of [`callStackToNix`](#callstacktonix) return value |
147| `shellFor` | Function | [`shellFor`](#shellfor) |
148| `ghcWithHoogle` | Function | [`ghcWithHoogle`](#ghcwithhoogle) |
149| `ghcWithPackages` | Function | [`ghcWithPackages`](#ghcwithpackages) |
152## cabalProject
154A function calling [callCabalProjectToNix](#callcabalprojecttonix) with all arguments.
156Then feeding its result into [mkCabalProjectPkgSet](#mkcabalprojectpkgset) passing also
157`pkg-def-extras`, `extra-hackages` and `modules` arguments.
159**Return value**:
161| Attribute | Type | Description |
162|-------------------|--------------------------------------------------|-----------------------------------------------------------------------------|
163| `hsPkgs` | Attrset of [Haskell Packages](#haskell-package) | Buildable packages, created from `packages` |
164| `pkg-set` | Attrset | [`pkgSet`](#package-set) |
165| `plan-nix` | | `projectNix` attribute of [`callCabalProjectToNix`](#callcabalprojecttonix) return value |
166| `index-state` | | `index-state` attribute of [`callCabalProjectToNix`](#callcabalprojecttonix) return value |
167| `shellFor` | Function | [`shellFor`](#shellfor) |
168| `ghcWithHoogle` | Function | [`ghcWithHoogle`](#ghcwithhoogle) |
169| `ghcWithPackages` | Function | [`ghcWithPackages`](#ghcwithpackages) |
173## mkStackPkgSet
175Creates a [package set](#package-set) based on the `pkgs.nix` output
176of `stack-to-nix`.
178```nix
179mkStackPkgSet =
180 { stack-pkgs, pkg-def-extras ? [], modules ? []}: ...
181```
183| Argument | Type | Description |
184|------------------|------|---------------------|
185| `stack-pkgs` | | `import ./pkgs.nix` — The imported file generated by `stack‑to‑nix`. |
186| `pkg‑def‑extras` | List of [Extras](#extras) | For overriding the package set. |
187| `modules` | List of [Modules](#modules) | For overriding the package set. |
189**Return value**: a [`pkgSet`](#package-set)
191## mkCabalProjectPkgSet
193Creates a [package set](#package-set) based on the `pkgs.nix` output
194of `plan-to-nix`.
196```nix
197mkCabalProjectPkgSet =
198 { plan-pkgs, pkg-def-extras ? [], modules ? []}: ...
199```
201| Argument | Type | Description |
202|------------------|------|---------------------|
203| `plan-pkgs` | | `import ./pkgs.nix` — The imported file generated by `plan‑to‑nix`. |
204| `pkg‑def‑extras` | List of [Extras](#extras) | For overriding the package set. |
205| `modules` | List of [Modules](#modules) | For overriding the package set. |
207**Return value**: a [`pkgSet`](#package-set)
209## mkPkgSet
211This is the base function used by both `mkStackPkgSet` and
212`mkCabalProjectPkgSet`.
214**Return value**: a [`pkgSet`](#package-set)
216## snapshots
218This is an attrset of `hsPkgs` packages from Stackage.
220## haskellPackages
222A `hsPkgs` package set, which is one of the recent LTS Haskell
223releases from [`snapshots`](#snapshots).
225The chosen LTS is updated occasionally in [Haskell.nix][], though a
226manual process.
228## nix-tools
230A derivation containing the `nix-tools` [command-line tools](commands.md).
232## callStackToNix
234Runs `stack-to-nix` and produces the output needed for
235`importAndFilterProject`.
237**Example**:
239```nix
240 pkgSet = mkStackPkgSet {
241 stack-pkgs = (importAndFilterProject (callStackToNix {
242 src = ./.;
243 })).pkgs;
244 pkg-def-extras = [];
245 modules = [];
246 };
247```
250## callCabalProjectToNix
252Runs `cabal new-configure` and `plan-to-nix` and produces the output
253needed for `importAndFilterProject`.
255**Example**:
257```nix
258 pkgSet = mkCabalProjectPkgSet {
259 plan-pkgs = (importAndFilterProject (callCabalProjectToNix {
260 index-state = "2019-04-30T00:00:00Z";
261 src = ./.;
262 })).pkgs;
263```
265| Argument | Type | Description |
266|----------------------|------|---------------------|
267| `name` | String | Optional name for better error messages. |
268| `src` | Path | Location of the cabal project files. |
269| `compiler-nix-name` | String | The name of the ghc compiler to use eg. "ghc884" |
270| `index-state` | Timestamp | Optional hackage index-state, eg. "2019-10-10T00:00:00Z". |
271| `index-sha256` | Sha256 | Optional hash of the truncated hackage index-state. |
272| `plan-sha256` | Sha256 | Optional hash of the plan-to-nix output (makes the plan-to-nix step a fixed output derivation). |
273| `cabalProject` | String | Optional cabal project file contents (defaults to readFile "${src}/cabal.project"). |
274| `cabalProjectLocal` | String | Optional cabal project file contents (defaults to readFile "${src}/cabal.project.local"). |
275| `cabalProjectFreeze` | String | Optional cabal project file contents (defaults to readFile "${src}/cabal.project.freeze"). |
276| `ghc` | | Deprecated. Use `compiler-nix-name` instead. Optional ghc to use |
277| `nix-tools` | | Optional nix-tools to use |
278| `hpack` | | Optional hpack to use |
279| `cabal-install` | | Optional cabal-install to use |
280| `configureArgs` | String | Optional extra arguments to pass to `cabal new-configure` (--enable-tests is included by default, include `--disable-tests` to override that). |
282## importAndFilterProject
284Imports from a derivation created by `callStackToNix`
285or `callCabalProjectToNix`.
287The result is an attrset with the following values:
289| Attribute | Type | Description |
290|----------------|------|-----------------------------------------------------|
291| `pkgs` | attrset | that can be passed to `mkStackPkgSet` (as `stack-pkgs`) or `mkCabalProjectPkgSet` (as `plan-pkgs`). |
292| `nix` | | this can be built and cached so that the amount built in the evaluation phase is not too great (helps to avoid timeouts on Hydra). |
294## hackage
295## stackage
297## fetchExternal
299## cleanSourceHaskell
301```
302cleanSourceHaskell = { src, name ? null }: ...
303```
305Filters a source tree removing common filenames that are not Haskell
306build sources.
308This can avoid unecessary rebuilds when these files change.
310It's recommended to provide `name` so that the source derivation
311remains constant regardless of how it was fetched.
313Example:
315```
316src = pkgs.haskell-nix.cleanSourceHaskell {
317 src = ./.;
318 name = "myproject-src";
319};
320```
322## haskellSourceFilter
324```
325haskellSourceFilter = name: type: ...
326```
328This is a source filter function which cleans common build products
329and files not needed to do a Haskell build from a source directory.
331It should be used with `pkgs.lib.cleanSourceWith`. Alternatively,
332use the convenience function [`cleanSourceHaskell`](#cleansourcehaskell).
335## haskellLib
337Assorted functions for operating on [Haskell.nix][] data. This is
338distinct from `pkgs.haskell.lib` in the current Nixpkgs Haskell
339Infrastructure.
341### collectComponents, collectComponents'
343Extracts a selection of components from a Haskell [package set](#package-set).
345This can be used to filter out all test suites or benchmarks of
346your project, so that they can be built in Hydra (see check if you
347want to run the tests as well as build them).
349`collectComponents'` is an alias of `collectComponents` without
350predicate for filtering.
352```
353collectComponents =
354 group: packageSel: haskellPackages: ...
355collectComponents' = group: collectComponents (_: true)
356```
359| Argument | Type | Description |
360|-------------------|--------|---------------------|
361| `group` | String | A [sub-component type](#subComponentTypes). |
362| `packageSel` | A function `Package -> Bool` | A predicate to filter packages with. |
363| `haskellPackages` | [Package set](#package-set) | All packages in the build. |
365**Return value**: a recursive attrset mapping package names → component names → components.
367**Example**:
369```nix
370tests = collectComponents "tests" (package: package.identifier.name == "mypackage") hsPkgs;
371```
373Will result in moving derivations from `hsPkgs.mypackage.components.tests.unit-tests`
374to `tests.mypackage.unit-tests`.
376### collectChecks, collectChecks'
378These are just like `collectComponents` and `collectComponents'`, except that they collect
379the `checks` attributes of packages (which aren't components, and so can't be collected
380by the other functions.
382#### check
384This function turns a derivation that builds a test into one to run it.
386| Argument | Type | Description |
387|-------------------|--------|---------------------|
388| `drv` | Derivation | One of `$pkg.components.tests.$test`. |
390For convenience `$pkg.components.tests` are mapped with this function
391to `$pkg.checks`.
393This function is intended for use with `tests` but it should also work
394for `exes` and `benchmarks` if you just want to run them to make sure
395they execute.
397#### subComponentTypes
399Sub-component types identify [components](#component) and are one of:
401 - `sublibs`
402 - `foreignlibs`
403 - `exes`
404 - `tests`
405 - `benchmarks`
407# Package-set functions
409These functions exist within the `hsPkgs` package set.
411## shellFor
413Create a `nix-shell` [development
414environment](../tutorials/development.md) for developing one or more
415packages with `ghci` or `cabal v2-build` (but not Stack).
417```
418shellFor =
419 { packages, withHoogle ? true, exactDeps ? false, ...}: ...
420```
423| Argument | Type | Description |
424|----------------|------|---------------------|
425| `packages` | Function | Package selection function. It takes a list of [Haskell packages](#haskell-package) and returns a subset of these packages. |
426| `components` | Function | Similar to `packages`, by default all the components of the selected packages are selected. |
427| `additional` | Function | Similar to `packages`, but the selected packages are built and included in `ghc-pkg list` (not just their dependencies). |
428| `withHoogle` | Boolean | Whether to build a Hoogle documentation index and provide the `hoogle` command. |
429| `exactDeps` | Boolean | Prevents the Cabal solver from choosing any package dependency other than what are in the package set. |
430| `tools` | Function | AttrSet of tools to make available e.g. `{ cabal = "3.2.0.0"; }` or `{ cabal = { version = "3.2.0.0"; }; }`. If an AttrSet is provided for a tool, the additional arguments will be passed to the function creating the derivation for that tool. So you can provide an `index-state` or a `materialized` argument like that `{ cabal = { version = "3.2.0.0"; index-state = "2020-10-30T00:00:00Z"; materialized = ./cabal.materialized; }; }` for example. You can specify and materialize the version of hoogle used to construct the hoogle index by including something like `{ hoogle = { version = "5.0.17.15"; index-state = "2020-05-31T00:00:00Z"; materialized = ./hoogle.materialized; }`. Uses a default version of hoogle if omitted. |
431| `{ ... }` | Attrset | All the other arguments are passed to [`mkDerivation`](https://nixos.org/nixpkgs/manual/#sec-using-stdenv). |
433**Return value**: a derivation
435!!! warning
437 `exactDeps = true` will set the `CABAL_CONFIG` environment variable
438 to disable remote package servers. This is a
439 [known limitation](../dev/removing-with-package-wrapper.md)
440 which we would like to solve. Use `exactDeps = false` if this is a
441 problem.
444## ghcWithPackages
446Creates a `nix-shell` [development
447environment](../tutorials/development.md) including the given
448packages selected from this package set.
450**Parameter**: a package selection function.
452**Return value**: a derivation
454**Example**:
456```
457haskell.haskellPackages.ghcWithPackages (ps: with ps; [ lens conduit ])
458```
460## ghcWithHoogle
462The same as `ghcWithPackages`, except, a `hoogle` command with a
463Hoogle documentation index of the packages will be included in the
464shell.
466[haskell.nix]: https://github.com/input-output-hk/haskell.nix