[Nix-dev] A Journey into the Haskell NG infrastructure: Part I

Peter Simons simons at cryp.to
Sat Jan 10 16:19:21 CET 2015


Hi,

 > It would be nice to have an example of how to use shell.nix and
 > default.nix together.

starting with Nixpkgs version 11ac18a, you can do the following.

 1) To create an interactive build environment that can compile a local Haskell
    project, run:

      $ cabal2nix --shell . >shell.nix && nix-shell

    The new shell will be set up with an appropriate ghcWithPackages wrapper
    that about knows all dependencies for this package, and required system
    libraries will also be configured to be found without extra parameters to
    "cabal configure".

 2) For recent Hackage packages, you can do that without using cabal2nix, even,
    by running:

      $ nix-shell '<nixpkgs>' -A haskellngPackages.hspec.env

    This will give you the same kind of environment as (1), but this time the
    environment is setup based on the information we have in Nixpkgs, so you
    don't have to generate a cabal file at all.

To illustrate these points, here is an example session. Note that my user has
no 'ghc':

 | $ ghc
 | The program ‘ghc’ is currently not installed. You can install it by typing:
 |   nix-env -i ghc

Anyway, let's build 'hspec'. First, we get the source code:

 | $ curl http://hackage.haskell.org/package/hspec-2.1.2/hspec-2.1.2.tar.gz | tar xz
 |   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 |                                  Dload  Upload   Total   Spent    Left  Speed
 | 100  5200  100  5200    0     0  75337      0 --:--:-- --:--:-- --:--:-- 76470
 |
 | $ cd hspec-2.1.2

Now enter the build environment based on the package data from Nixpkgs:

 | $ nix-shell '<nixpkgs>' -A haskellngPackages.hspec.env

And compile:

 | [nix-shell:/tmp/hspec-2.1.2]$ ghc --make Setup
 | [1 of 1] Compiling Main             ( Setup.lhs, Setup.o )
 | Linking Setup ...
 |
 | [nix-shell:/tmp/hspec-2.1.2]$ ./Setup configure
 | Configuring hspec-2.1.2...
 |
 | [nix-shell:/tmp/hspec-2.1.2]$ ./Setup build
 | Building hspec-2.1.2...
 | Preprocessing library hspec-2.1.2...
 | [1 of 7] Compiling Test.Hspec.HUnit ( src/Test/Hspec/HUnit.hs, dist/build/Test/Hspec/HUnit.o )
 | [2 of 7] Compiling Test.Hspec.Formatters ( src/Test/Hspec/Formatters.hs, dist/build/Test/Hspec/Formatters.o )
 | [3 of 7] Compiling Test.Hspec.Discover ( src/Test/Hspec/Discover.hs, dist/build/Test/Hspec/Discover.o )
 | [4 of 7] Compiling Test.Hspec.Runner ( src/Test/Hspec/Runner.hs, dist/build/Test/Hspec/Runner.o )
 | [5 of 7] Compiling Test.Hspec.Core  ( src/Test/Hspec/Core.hs, dist/build/Test/Hspec/Core.o )
 | [6 of 7] Compiling Test.Hspec       ( src/Test/Hspec.hs, dist/build/Test/Hspec.o )
 | [7 of 7] Compiling Test.Hspec.QuickCheck ( src/Test/Hspec/QuickCheck.hs, dist/build/Test/Hspec/QuickCheck.o )
 | In-place registering hspec-2.1.2...
 |
 | [nix-shell:/tmp/hspec-2.1.2]$ exit
 | exit

Of course, you can use cabal-install if you happen to have that in your user
profile. I just wanted to show the simplest possible use case that works
without it.

I hope this helps,
Peter



More information about the nix-dev mailing list