[Nix-dev] Haskell Infrastructure - Nix-Shell with Hoogle from cabal2nix

Andreas Herrmann andreash87 at gmx.ch
Sun Feb 14 14:48:54 CET 2016


Hi,

I'm trying to understand how to use the Haskell-infrastructure properly.

Suppose I have an existing Haskell project with the following cabal file:

test.cabal
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
name:                test
version:             0.1.0.0
license:             BSD3
license-file:        LICENSE
build-type:          Simple
cabal-version:       >=1.10

executable test
  main-is:             Main.hs
  build-depends:       base, bytestring
  default-language:    Haskell2010
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I can use `cabal2nix` to generate a Nix derivation for it. The tool produces
the following file:

default.nix
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ mkDerivation, base, bytestring, stdenv }:
mkDerivation {
  pname = "test";
  version = "0.1.0.0";
  src = ./.;
  isLibrary = false;
  isExecutable = true;
  executableHaskellDepends = [ base bytestring ];
  license = stdenv.lib.licenses.bsd3;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

And in order to build that package I can create the following Nix file:

pkg.nix
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ nixpkgs ? import <nixpkgs> { }, compiler ? "default" }:
let
  inherit (nixpkgs) pkgs;
  haskellPackages = if compiler == "default"
                        then pkgs.haskellPackages
                        else pkgs.haskell.packages.${compiler};
in
haskellPackages.callPackage ./. { }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

And then build the package with `nix-build ./pkg.nix`.

In order to create a development environment, where I have `cabal-install`
available as well, I can write the following Nix file:

shell.nix
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ nixpkgs ? import <nixpkgs> { }, compiler ? "default" }:
let
  inherit (nixpkgs) pkgs;
  haskellPackages = if compiler == "default"
                        then pkgs.haskellPackages
                        else pkgs.haskell.packages.${compiler};
  inherit (pkgs.haskell.lib) addBuildTools;
  pkg = import ./pkg.nix { inherit nixpkgs compiler; };
in
(addBuildTools pkg (with haskellPackages; [ cabal-install ])).env
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# My Question

How could I instead get a development environment with a Hoogle package
database of all the dependencies. I know that I could manually create the
following Nix file and use it with `nix-shell ./shell-manual.nix`.

shell-manual.nix
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ nixpkgs ? import <nixpkgs> { }, compiler ? "default" }:
let
  inherit (nixpkgs) pkgs;
  haskellPackages = if compiler == "default"
                        then pkgs.haskellPackages
                        else pkgs.haskell.packages.${compiler};
  ghc = haskellPackages.ghcWithHoogle (pkgs: with pkgs; [
          base bytestring cabal-install
        ]);
in
pkgs.stdenv.mkDerivation {
  name = "dummy";
  buildInputs = [ ghc ];
  shellHook = "eval $(egrep ^export ${ghc}/bin/ghc)";
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

However, then I would have to manually maintain an extra dependency list. Is
there any way of transforming the Nix-Shell environment produced by `pkg.env`
in `shell.nix` above into one that adds a Hoogle database of all the included
packages?

Best, Andreas


More information about the nix-dev mailing list