on 38cce7c help sign in
haskell.nix/docs/tutorials/getting-started.md
120 lines 4.2 kB history raw
1# Getting started
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
15CI:
17```bash
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
23$ cachix use iohk
24Configured https://iohk.cachix.org binary cache in ~/.config/nix/nix.conf
25```
27## Scaffolding
29The following work with `stack.yaml` and `cabal.project` based
30projects.
32Add `default.nix`:
34```nix
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";
53 src = ./.;
54 };
55 # For `cabal.project` based projects specify the GHC version to use.
56 compiler-nix-name = "ghc884"; # Not used for `stack.yaml` based projects.
58```
60!!! note "git dependencies"
61 If you have git dependencies in your project, you'll need
62 to [calculate sha256 hashes for them](./source-repository-hashes.md).
64### Working with a project
66Top-level attributes are Haskell packages (incl. dependencies) part of your project.
68To build the library component of a package in the project run:
70```shell
71nix-build -A your-package-name.components.library
72```
74There are also other components such as `exes`, `tests`, `benchmarks` and `all`.
75To build an executable:
77```shell
78nix-build -A your-package-name.components.exes.your-exe-name
79```
81To open a shell for use with `cabal` run:
83```shell
84nix-shell -A shellFor
85cabal new-repl your-package-name:library:your-package-name
86cabal new-build your-package-name
87```
89To open a shell for use with `stack` see [the following issue](https://github.com/input-output-hk/haskell.nix/issues/689#issuecomment-643832619).
91### Pinning the [haskell.nix][] version
93For simplicity's sake we will use `fetchTarball` for the examples in
94this documentation. This will always get the latest version, and is
95similar to an auto-updating Nix channel.
97However, in your own project, you may wish to pin [haskell.nix][] (as
98you would pin Nixpkgs). This will make your builds reproducible, more
99predictable, and faster (because the fixed version is cached).
101Straightforward way of doing this is to change the branch name to a revision.
103```nix
104{ # Fetch a specific haskell.nix and import its default.nix
105 haskellNix ? import (builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/f1a94a4c82a2ab999a67c3b84269da78d89f0075.tar.gz") {}
107...
108```
110There are other possible schemes for pinning. See
111[Bumping Hackage and Stackage snapshots](./hackage-stackage.md) and
112[Nix tutorial on reproducibility using pinning](https://nix.dev/tutorials/towards-reproducibility-pinning-nixpkgs.html).
114## Going forward
116Read through [project](../reference/library.md#project) function reference to see how the API works.
118There are a number of things to explore further in the tutorials section.
120[haskell.nix]: https://github.com/input-output-hk/haskell.nix