3`haskell.nix` can automatically translate your
4[Cabal](https://cabal.readthedocs.io/en/latest/cabal-project.html)
5or [Stack](https://docs.haskellstack.org/en/stable/README/#quick-start-guide)
6project and its dependencies into Nix code.
8Assuming you have [Nix](https://nixos.org/download.html) installed, you can
9start setting up your project.
11## Setting up the Cachix binary cache
13You can **avoid compiling GHC and nix-tools** by configuring
14[Cachix](https://cachix.org) so you can benefit from the binary cache built by
18$ nix-env -iA cachix -f https://cachix.org/api/v1/install
19installing 'cachix-0.3.8'
20building '/nix/store/bh176xhpk4wrjm56iahm86wf85jaz23v-user-environment.drv'...
21created 42 symlinks in user environment
24Configured https://iohk.cachix.org binary cache in ~/.config/nix/nix.conf
29The following work with `stack.yaml` and `cabal.project` based
35{ # Fetch the latest haskell.nix and import its default.nix
36 haskellNix ? import (builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz") {}
38# haskell.nix provides access to the nixpkgs pins which are used by our CI,
39# hence you will be more likely to get cache hits when using these.
40# But you can also just use your own, e.g. '<nixpkgs>'.
41, nixpkgsSrc ? haskellNix.sources.nixpkgs-2003
43# haskell.nix provides some arguments to be passed to nixpkgs, including some
44# patches and also the haskell.nix functionality itself as an overlay.
45, nixpkgsArgs ? haskellNix.nixpkgsArgs
47# import nixpkgs with overlays
48, pkgs ? import nixpkgsSrc nixpkgsArgs
49}: pkgs.haskell-nix.project {
50 # 'cleanGit' cleans a source directory based on the files known by git
51 src = pkgs.haskell-nix.haskellLib.cleanGit {
52 name = "haskell-nix-project";
58!!! note "git dependencies"
59 If you have git dependencies in your project, you'll need
60 to [calculate sha256 hashes for them](./source-repository-hashes.md).
62### Working with a project
64Top-level attributes are Haskell packages (incl. dependencies) part of your project.
66To build the library component of a package in the project run:
69nix-build -A your-package-name.components.library
72There are also other components such as `exes`, `tests`, `benchmarks` and `all`.
73To build an executable:
76nix-build -A your-package-name.components.exes.your-exe-name
79To open a shell for use with `cabal` run:
83cabal new-repl your-package-name:library:your-package-name
84cabal new-build your-package-name
87To open a shell for use with `stack` see [the following issue](https://github.com/input-output-hk/haskell.nix/issues/689#issuecomment-643832619).
89### Pinning the [haskell.nix][] version
91For simplicity's sake we will use `fetchTarball` for the examples in
92this documentation. This will always get the latest version, and is
93similar to an auto-updating Nix channel.
95However, in your own project, you may wish to pin [haskell.nix][] (as
96you would pin Nixpkgs). This will make your builds reproducible, more
97predictable, and faster (because the fixed version is cached).
99Straightforward way of doing this is to change the branch name to a revision.
102{ # Fetch a specific haskell.nix and import its default.nix
103 haskellNix ? import (builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/f1a94a4c82a2ab999a67c3b84269da78d89f0075.tar.gz") {}
108There are other possible schemes for pinning. See
109[Bumping Hackage and Stackage snapshots](./hackage-stackage.md) and
110[Nix tutorial on reproducibility using pinning](https://nix.dev/tutorials/towards-reproducibility-pinning-nixpkgs.html).
114There are a number of things to explore further in the tutorials section.
116[haskell.nix]: https://github.com/input-output-hk/haskell.nix
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list