on 283f573 help sign in
473 lines 19 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| `stack-nix` | | `projectNix` attribute of [`callStackToNix`](#callstacktonix) return value |
146| `shellFor` | Function | [`shellFor`](#shellfor) |
147| `ghcWithHoogle` | Function | [`ghcWithHoogle`](#ghcwithhoogle) |
148| `ghcWithPackages` | Function | [`ghcWithPackages`](#ghcwithpackages) |
151## cabalProject'
153A function calling [callCabalProjectToNix](#callcabalprojecttonix) with all arguments.
155Then feeding its result into [mkCabalProjectPkgSet](#mkcabalprojectpkgset) passing also
156`pkg-def-extras`, `extra-hackages` and `modules` arguments.
158**Return value**:
160| Attribute | Type | Description |
161|-------------------|--------------------------------------------------|-----------------------------------------------------------------------------|
162| `hsPkgs` | Attrset of [Haskell Packages](#haskell-package) | Buildable packages, created from `packages` |
163| `plan-nix` | | `projectNix` attribute of [`callCabalProjectToNix`](#callcabalprojecttonix) return value |
164| `index-state` | | `index-state` attribute of [`callCabalProjectToNix`](#callcabalprojecttonix) return value |
165| `shellFor` | Function | [`shellFor`](#shellfor) |
166| `ghcWithHoogle` | Function | [`ghcWithHoogle`](#ghcwithhoogle) |
167| `ghcWithPackages` | Function | [`ghcWithPackages`](#ghcwithpackages) |
168| `projectCross` | Attrset | Like `pkgs.pkgsCross.X` from nixpkgs `p.projectCross.X` returns the project results for cross compilation to X. So `p.projectCross.ghcjs.hsPkgs` is the same as `hsPkgs` but compiled with ghcjs |
170## project, cabalProject and stackProject
172These versions of the function are the same as project', cabalProject'
173and stackProject', but `hsPkgs` attributes are also included in the
174return value directly. That way a package can be referenced as
175`(project {...}).foo` instead of `(project' {...}).hsPkgs.foo`.
177## mkStackPkgSet
179Creates a [package set](#package-set) based on the `pkgs.nix` output
180of `stack-to-nix`.
182```nix
183mkStackPkgSet =
184 { stack-pkgs, pkg-def-extras ? [], modules ? []}: ...
185```
187| Argument | Type | Description |
188|------------------|------|---------------------|
189| `stack-pkgs` | | `import ./pkgs.nix` — The imported file generated by `stack‑to‑nix`. |
190| `pkg‑def‑extras` | List of [Extras](#extras) | For overriding the package set. |
191| `modules` | List of [Modules](#modules) | For overriding the package set. |
193**Return value**: a [`pkgSet`](#package-set)
195## mkCabalProjectPkgSet
197Creates a [package set](#package-set) based on the `pkgs.nix` output
198of `plan-to-nix`.
200```nix
201mkCabalProjectPkgSet =
202 { plan-pkgs, pkg-def-extras ? [], modules ? []}: ...
203```
205| Argument | Type | Description |
206|------------------|------|---------------------|
207| `plan-pkgs` | | `import ./pkgs.nix` — The imported file generated by `plan‑to‑nix`. |
208| `pkg‑def‑extras` | List of [Extras](#extras) | For overriding the package set. |
209| `modules` | List of [Modules](#modules) | For overriding the package set. |
211**Return value**: a [`pkgSet`](#package-set)
213## mkPkgSet
215This is the base function used by both `mkStackPkgSet` and
216`mkCabalProjectPkgSet`.
218**Return value**: a [`pkgSet`](#package-set)
220## snapshots
222This is an attrset of `hsPkgs` packages from Stackage.
224## haskellPackages
226A `hsPkgs` package set, which is one of the recent LTS Haskell
227releases from [`snapshots`](#snapshots).
229The chosen LTS is updated occasionally in [Haskell.nix][], though a
230manual process.
232## nix-tools
234A derivation containing the `nix-tools` [command-line tools](commands.md).
236## callStackToNix
238Runs `stack-to-nix` and produces the output needed for
239`importAndFilterProject`.
241**Example**:
243```nix
244 pkgSet = mkStackPkgSet {
245 stack-pkgs = (importAndFilterProject (callStackToNix {
246 src = ./.;
247 })).pkgs;
248 pkg-def-extras = [];
249 modules = [];
250 };
251```
254## callCabalProjectToNix
256Runs `cabal new-configure` and `plan-to-nix` and produces the output
257needed for `importAndFilterProject`.
259**Example**:
261```nix
262 pkgSet = mkCabalProjectPkgSet {
263 plan-pkgs = (importAndFilterProject (callCabalProjectToNix {
264 index-state = "2019-04-30T00:00:00Z";
265 src = ./.;
266 })).pkgs;
267```
269| Argument | Type | Description |
270|----------------------|------|---------------------|
271| `name` | String | Optional name for better error messages. |
272| `src` | Path | Location of the cabal project files. |
273| `compiler-nix-name` | String | The name of the ghc compiler to use eg. "ghc884" |
274| `index-state` | Timestamp | Optional hackage index-state, eg. "2019-10-10T00:00:00Z". |
275| `index-sha256` | Sha256 | Optional hash of the truncated hackage index-state. |
276| `plan-sha256` | Sha256 | Optional hash of the plan-to-nix output (makes the plan-to-nix step a fixed output derivation). |
277| `cabalProject` | String | Optional cabal project file contents (defaults to readFile "${src}/cabal.project"). |
278| `cabalProjectLocal` | String | Optional cabal project file contents (defaults to readFile "${src}/cabal.project.local"). |
279| `cabalProjectFreeze` | String | Optional cabal project file contents (defaults to readFile "${src}/cabal.project.freeze"). |
280| `ghc` | | Deprecated. Use `compiler-nix-name` instead. Optional ghc to use |
281| `nix-tools` | | Optional nix-tools to use |
282| `hpack` | | Optional hpack to use |
283| `cabal-install` | | Optional cabal-install to use |
284| `configureArgs` | String | Optional extra arguments to pass to `cabal new-configure` (--enable-tests is included by default, include `--disable-tests` to override that). |
286## importAndFilterProject
288Imports from a derivation created by `callStackToNix`
289or `callCabalProjectToNix`.
291The result is an attrset with the following values:
293| Attribute | Type | Description |
294|----------------|------|-----------------------------------------------------|
295| `pkgs` | attrset | that can be passed to `mkStackPkgSet` (as `stack-pkgs`) or `mkCabalProjectPkgSet` (as `plan-pkgs`). |
296| `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). |
298## hackage
299## stackage
301## fetchExternal
303## cleanSourceHaskell
305```
306cleanSourceHaskell = { src, name ? null }: ...
307```
309Filters a source tree removing common filenames that are not Haskell
310build sources.
312This can avoid unecessary rebuilds when these files change.
314It's recommended to provide `name` so that the source derivation
315remains constant regardless of how it was fetched.
317Example:
319```
320src = pkgs.haskell-nix.cleanSourceHaskell {
321 src = ./.;
322 name = "myproject-src";
323};
324```
326## haskellSourceFilter
328```
329haskellSourceFilter = name: type: ...
330```
332This is a source filter function which cleans common build products
333and files not needed to do a Haskell build from a source directory.
335It should be used with `pkgs.lib.cleanSourceWith`. Alternatively,
336use the convenience function [`cleanSourceHaskell`](#cleansourcehaskell).
339## haskellLib
341Assorted functions for operating on [Haskell.nix][] data. This is
342distinct from `pkgs.haskell.lib` in the current Nixpkgs Haskell
343Infrastructure.
345### collectComponents, collectComponents'
347Extracts a selection of components from a Haskell [package set](#package-set).
349This can be used to filter out all test suites or benchmarks of
350your project, so that they can be built in Hydra (see check if you
351want to run the tests as well as build them).
353`collectComponents'` is an alias of `collectComponents` without
354predicate for filtering.
356```
357collectComponents =
358 group: packageSel: haskellPackages: ...
359collectComponents' = group: collectComponents (_: true)
360```
363| Argument | Type | Description |
364|-------------------|--------|---------------------|
365| `group` | String | A [sub-component type](#subComponentTypes). |
366| `packageSel` | A function `Package -> Bool` | A predicate to filter packages with. |
367| `haskellPackages` | [Package set](#package-set) | All packages in the build. |
369**Return value**: a recursive attrset mapping package names → component names → components.
371**Example**:
373```nix
374tests = collectComponents "tests" (package: package.identifier.name == "mypackage") hsPkgs;
375```
377Will result in moving derivations from `hsPkgs.mypackage.components.tests.unit-tests`
378to `tests.mypackage.unit-tests`.
380### collectChecks, collectChecks'
382These are just like `collectComponents` and `collectComponents'`, except that they collect
383the `checks` attributes of packages (which aren't components, and so can't be collected
384by the other functions.
386#### check
388This function turns a derivation that builds a test into one to run it.
390| Argument | Type | Description |
391|-------------------|--------|---------------------|
392| `drv` | Derivation | One of `$pkg.components.tests.$test`. |
394For convenience `$pkg.components.tests` are mapped with this function
395to `$pkg.checks`.
397This function is intended for use with `tests` but it should also work
398for `exes` and `benchmarks` if you just want to run them to make sure
399they execute.
401#### subComponentTypes
403Sub-component types identify [components](#component) and are one of:
405 - `sublibs`
406 - `foreignlibs`
407 - `exes`
408 - `tests`
409 - `benchmarks`
411# Project functions
413These functions are included in the `project` return values.
414In the past they also existed within `project.hsPkgs`,
415but have now been removed from there.
417## shellFor
419Create a `nix-shell` [development
420environment](../tutorials/development.md) for developing one or more
421packages with `ghci` or `cabal v2-build` (but not Stack).
423```
424shellFor =
425 { packages, withHoogle ? true, exactDeps ? false, ...}: ...
426```
429| Argument | Type | Description |
430|----------------|------|---------------------|
431| `packages` | Function | Package selection function. It takes a list of [Haskell packages](#haskell-package) and returns a subset of these packages. |
432| `components` | Function | Similar to `packages`, by default all the components of the selected packages are selected. |
433| `additional` | Function | Similar to `packages`, but the selected packages are built and included in `ghc-pkg list` (not just their dependencies). |
434| `withHoogle` | Boolean | Whether to build a Hoogle documentation index and provide the `hoogle` command. |
435| `exactDeps` | Boolean | Prevents the Cabal solver from choosing any package dependency other than what are in the package set. |
436| `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. |
437| `inputsFrom` | List | List of other shells to include in this one. The `buildInputs` and `nativeBuildInputs` of each will be included using [mkShell](https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell).
438| `{ ... }` | Attrset | All the other arguments are passed to [`mkDerivation`](https://nixos.org/nixpkgs/manual/#sec-using-stdenv). |
440**Return value**: a derivation
442!!! warning
444 `exactDeps = true` will set the `CABAL_CONFIG` environment variable
445 to disable remote package servers. This is a
446 [known limitation](../dev/removing-with-package-wrapper.md)
447 which we would like to solve. Use `exactDeps = false` if this is a
448 problem.
451## ghcWithPackages
453Creates a `nix-shell` [development
454environment](../tutorials/development.md) including the given
455packages selected from this package set.
457**Parameter**: a package selection function.
459**Return value**: a derivation
461**Example**:
463```
464haskell.haskellPackages.ghcWithPackages (ps: with ps; [ lens conduit ])
465```
467## ghcWithHoogle
469The same as `ghcWithPackages`, except, a `hoogle` command with a
470Hoogle documentation index of the packages will be included in the
471shell.
473[haskell.nix]: https://github.com/input-output-hk/haskell.nix