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 binary cache
13IMPORTANT: you *must* do this or you *will* build several copies of GHC!
15You can configure Nix to use our binary cache, which is pushed to by CI, so should contain the artifacts that you need.
17You need to add the following sections to `/etc/nix/nix.conf` or, if you are a trusted user, `~/.config/nix/nix.conf` (if you don't know what a "trusted user" is, you probably want to do the former).
20trusted-public-keys = [...] hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= [...]
21substituters = [...] https://hydra.iohk.io [...]
24This can be tricky to get setup properly. If you're still having trouble getting cache hits, consult the corresponding [troubleshooting section](../reference/troubleshooting#why-am-i-building-ghc).
28The following work with `stack.yaml` and `cabal.project` based
34{ # Fetch the latest haskell.nix and import its default.nix
35 haskellNix ? import (builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz") {}
37# haskell.nix provides access to the nixpkgs pins which are used by our CI,
38# hence you will be more likely to get cache hits when using these.
39# But you can also just use your own, e.g. '<nixpkgs>'.
40, nixpkgsSrc ? haskellNix.sources.nixpkgs-2003
42# haskell.nix provides some arguments to be passed to nixpkgs, including some
43# patches and also the haskell.nix functionality itself as an overlay.
44, nixpkgsArgs ? haskellNix.nixpkgsArgs
46# import nixpkgs with overlays
47, pkgs ? import nixpkgsSrc nixpkgsArgs
48}: pkgs.haskell-nix.project {
49 # 'cleanGit' cleans a source directory based on the files known by git
50 src = pkgs.haskell-nix.haskellLib.cleanGit {
51 name = "haskell-nix-project";
54 # Specify the GHC version to use.
55 compiler-nix-name = "ghc8102"; # Not required for `stack.yaml` based projects.
59!!! note "git dependencies"
60 If you have git dependencies in your project, you'll need
61 to [calculate sha256 hashes for them](./source-repository-hashes.md).
63### Working with a project
65Top-level attributes are Haskell packages (incl. dependencies) part of your project.
67To build the library component of a package in the project run:
70nix-build -A your-package-name.components.library
73There are also other components such as `exes`, `tests`, `benchmarks` and `all`.
74To build an executable:
77nix-build -A your-package-name.components.exes.your-exe-name
80To open a shell for use with `cabal` run:
84cabal new-repl your-package-name:library:your-package-name
85cabal new-build your-package-name
88To open a shell for use with `stack` see [the following issue](https://github.com/input-output-hk/haskell.nix/issues/689#issuecomment-643832619).
90### Pinning the [haskell.nix][] version
92For simplicity's sake we will use `fetchTarball` for the examples in
93this documentation. This will always get the latest version, and is
94similar to an auto-updating Nix channel.
96However, in your own project, you may wish to pin [haskell.nix][] (as
97you would pin Nixpkgs). This will make your builds reproducible, more
98predictable, and faster (because the fixed version is cached).
100Straightforward way of doing this is to change the branch name to a revision.
103{ # Fetch a specific haskell.nix and import its default.nix
104 haskellNix ? import (builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/f1a94a4c82a2ab999a67c3b84269da78d89f0075.tar.gz") {}
109There are other possible schemes for pinning. See
110[Bumping Hackage and Stackage snapshots](./hackage-stackage.md) and
111[Nix tutorial on reproducibility using pinning](https://nix.dev/tutorials/towards-reproducibility-pinning-nixpkgs.html).
115Read through [project](../reference/library.md#project) function reference to see how the API works.
117There are a number of things to explore further in the tutorials section.
119[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