1# Manually generating Nix expressions
3We believe that imports from derivations (IFDs) provide tremendous
4value in nix and the aversion towards them stems mostly from
5poor tooling and ci support for them. We do not believe
6that poor tooling or ci support should cripple nix capability
7of abstraction. Hence haskell.nix makes excessive use of
10We do note however that there are users who prefer to
11have IFD-free expressions. For this group of users we
12detail how to expand the IFD dependent high level functions
13into their IFD free building blocks.
15The general structure will be the same, independent of the use of
18Let us assume for now that we have already generated a `pkgs.nix`
19expression (see the links bellow). The following file then produces a package set:
24 # Import the Haskell.nix library,
25 pkgs = import <nixpkgs> (import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {}).nixpkgsArgs;
27 # Import the file you will create in the stack-to-nix or cabal-to-nix step.
28 my-pkgs = import ./pkgs.nix;
30 # Stack projects use this:
31 pkgSet = pkgs.haskell-nix.mkStackPkgSet {
34 # these extras will provide additional packages
35 # ontop of the package set. E.g. extra-deps
36 # for stack packages. or local packages for
40 # specific package overrides would go here
42 # packages.cbors.package.ghcOptions = "-Werror";
43 # packages.cbors.patches = [ ./one.patch ];
44 # packages.cbors.flags.optimize-gmp = false;
45 # It may be better to set flags in stack.yaml instead
46 # (`stack-to-nix` will include them as defaults).
50 # Cabal projects use this:
51 pkgSet = pkgs.haskell-nix.mkCabalProjectPkgSet {
55 # specific package overrides would go here
57 # packages.cbors.package.ghcOptions = "-Werror";
58 # packages.cbors.patches = [ ./one.patch ];
59 # packages.cbors.flags.optimize-gmp = false;
60 # It may be better to set flags in `cabal.project` instead
61 # (`plan-to-nix` will include them as defaults).
65in pkgSet.config.hsPkgs // { _config = pkgSet.config; }
68With this setup you can then start building the components of
72nix build -f default.nix $pkg.components.library
75to build the library for `$pkg` or
78nix build -f default.nix $pkg.components.exes.$exe
81to build a specific executable. The same holds for test suites and benchmarks.
85With [nix-tools installed](./installing-nix-tools.md), we can simply run the
86following command on a stack project:
89stack-to-nix --output . --stack-yaml stack.yaml
92This will produce a `pkgs.nix` file that looks like the following:
95 resolver = "lts-12.17";
99 "o-clock" = hackage.o-clock."0.1.1".revisions.default;
102 my-package = ./my-package.nix;
109This file contains the stackage resolver, as well as set of extra
110packages. The extras specifies which `extra-deps` (here:
111`o-clock-0.1.1`) we wanted to add over the stackage snapshot, and what
112local packages we want (here: `my-package`).
116### Generating `plan.json`
118To get a plan, you need Cabal and GHC. See the [How to install a
119compiler section of the Nixpkgs Manual][compiler] for information
120about how to choose a specific compiler version.
122[compiler]: https://nixos.org/nixpkgs/manual/#how-to-install-a-compiler
124!!! note "Cabal version"
125 The minimum Cabal version is 2.4. This version is available
126 in the NixOS 19.03 release.
128For this example, we will run a `nix-shell` with the default GHC
132nix-shell -p haskellPackages.cabal-install haskellPackages.ghc \
133 --run "cabal new-configure"
136If all goes well, you should now have the file
137`dist-newstyle/cache/plan.json`.
139!!! tip "Specifying the GHC version"
140 To use a specific compiler version, replace `haskellPackages.ghc`
141 with something like `haskell-nix.compiler.ghc865`. The given compiler
142 must exist in your Nixpkgs version, of course. See also the
143 [Nixpkgs Manual][compiler].
145### Using `plan-to-nix`
147With [nix-tools installed](./installing-nix-tools.md), we can then run the
148following command on a Cabal project and its build plan. Omit the
149`--cabal-project` option if you don't have a project file.
152# convert the plan.json file into a pkgs.nix file
153plan-to-nix --output . \
154 --plan-json dist-newstyle/cache/plan.json
155 --cabal-project cabal.project
158This will produce a `pkgs.nix` file that looks like the following:
165 "o-clock" = hackage.o-clock."0.1.1".revisions.default;
172 { packages = { my-package = ./.plan.nix/my-package.nix; }; };
176It has converted Cabal's build plan into a Nix expression that selects
177dependencies from `hackage.nix`. All local packages in the project are
178generated with `cabal-to-nix` and added to the package set
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