Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using hoogle in a haskell development environment on nix

I'm trying to use hoogle in a Haskell development environment exactly like the one described by O'Charles' wiki:

I have modified shell.nix like below in order to use hoogleLocal, but it doesn't seem to install the hoogle binary for me.

let
  pkgs = import <nixpkgs> {};

  # I'm attempting to use hoogle here, but it is not working.
  haskellPackages =
    let callPackage = pkgs.lib.callPackageWith haskellPackages;
    in pkgs.recurseIntoAttrs (pkgs.haskellPackages.override {
      extension = self: super: {
        thiscurrentpackage = self.callPackage ./. {};
        hoogleLocal = pkgs.haskellPackages.hoogleLocal.override {
          packages = self.thiscurrentpackage;
        };
      };
    });
in pkgs.myEnvFun {
  name = haskellPackages.thiscurrentpackage.name;
  buildInputs = [
    (haskellPackages.ghcWithPackages (hs: ([
      hs.cabalInstall
      hs.ghcMod
      hs.yesodBin
      # This doesn't appear to install the hoogle binary?
      hs.hoogleLocal
    ] ++ hs.thiscurrentpackage.propagatedNativeBuildInputs)))
  ];
}

In the resulting shell, the hoogle binary is not available.

If I include hs.hoogle to the buildInputs, the hoogle binary is installed but it can't find the databases. Below is what happens when I try to use it.

$ nix-shell
......
$ hoogle Monad
Could not find some databases: default
Searching in:
  .
    /nix/store/91y9q2y5a2ws8xgcsx1gkhfagc0f2qz6-haskell-hoogle-ghc7.8.3-4.2.36-shared/share/x86_64-linux-ghc-7.8.3/hoogle-4.2.36/databases

    There are no available databases, generate them with: hoogle data
$ hoogle data
    hoogle: /nix/store/91y9q2y5a2ws8xgcsx1gkhfagc0f2qz6-haskell-hoogle-ghc7.8.3-4.2.36-shared/share/x86_64-linux-ghc-7.8.3/hoogle-4.2.36/databases:
changeWorkingDirectory: does not exist (No such file or directory)
$

How do I get this working correctly for a setup like described by O'Charles?

Edit: The original shell.nix is the same one from this answer.

like image 465
illabout Avatar asked Jan 01 '15 06:01

illabout


2 Answers

Here's what my Nix Haskell dev environment looks like

in ~/.nixpkgs/config.nix:

The environment helper function

First off, define a haskellEnvFun function for building Haskell environments:

packageOverrides = super: rec {

haskellEnvFun = { withHoogle ? false, compiler ? null, name }:
  let hp = if compiler != null
             then super.haskell.packages.${compiler}
             else haskellPackages;

      ghcWith = if withHoogle
                  then hp.ghcWithHoogle
                  else hp.ghcWithPackages;

  in super.buildEnv {
    name = name;
    paths = [(ghcWith myHaskellPackages)];
  };

Defining some environments

Call this function to define two environments: one for running the Hoogle builder on changes, and one without:

haskellEnvHoogle = haskellEnvFun {
  name = "haskellEnvHoogle";
  withHoogle = true;
};

haskellEnv = haskellEnvFun {
  name = "haskellEnv";
  withHoogle = false;
};

Packages

Define all the packages you want to use in your local Haskell dev environment:

myHaskellPackages = hp: with hp; [
  Boolean
  HTTP
  HUnit
  MissingH
  QuickCheck
  SafeSemaphore
  Spock
  aeson
  async
  attoparsec
  bifunctors
  blaze-builder
  blaze-builder-conduit
  blaze-builder-enumerator
  blaze-html
  blaze-markup
  blaze-textual
  cased
  cassava
  cereal
  comonad
  comonad-transformers
  directory_1_2_4_0
  dlist
  dlist-instances
  doctest
  exceptions
  fingertree
  foldl
  free
  hamlet
  hashable
  hspec
  hspec-expectations
  html
  http-client
  http-date
  http-types
  io-memoize
  keys
  language-c
  language-javascript
  language-bash
  lens
  lens-action
  lens-aeson
  lens-datetime
  lens-family
  lens-family-core
  lifted-async
  lifted-base
  linear
  list-extras
  list-t
  logict
  mime-mail
  mime-types
  mmorph
  monad-control
  monad-coroutine
  monad-loops
  monad-par
  monad-par-extras
  monad-stm
  monadloc
  mongoDB
  monoid-extras
  network
  newtype
  numbers
  optparse-applicative
  parsec
  parsers
  pcg-random
  persistent
  persistent-mongoDB
  persistent-template
  pipes
  pipes-async
  pipes-attoparsec
  pipes-binary
  pipes-bytestring
  pipes-concurrency
  pipes-csv
  pipes-extras
  pipes-group
  pipes-http
  pipes-mongodb
  pipes-network
  pipes-parse
  pipes-safe
  pipes-shell
  pipes-text
  posix-paths
  postgresql-simple
  pretty-show
  profunctors
  random
  reducers
  reflection
  regex-applicative
  regex-base
  regex-compat
  regex-posix
  regular
  relational-record
  resourcet
  retry
  rex
  safe
  sbv
  scotty
  semigroupoids
  semigroups
  shake
  shakespeare
  shelly
  simple-reflect
  speculation
  split
  spoon
  stm
  stm-chans
  stm-stats
  streaming
  streaming-bytestring
  streaming-wai
  strict
  stringsearch
  strptime
  syb
  system-fileio
  system-filepath
  tagged
  taggy
  taggy-lens
  tar
  tardis
  tasty
  tasty-hspec
  tasty-hunit
  tasty-quickcheck
  tasty-smallcheck
  temporary
  test-framework
  test-framework-hunit
  text
  text-format
  time
  tinytemplate
  transformers
  transformers-base
  turtle
  uniplate
  unix-compat
  unordered-containers
  uuid
  vector
  void
  wai
  wai-conduit
  warp
  wreq
  xhtml
  yaml
  zippers
  zlib
];

Shell helpers

In your ~/.profile define a couple bash functions to load those environments for convenience:

env-type () {
  envtype="$1"
  shift
  nix-shell -Q -p $envtype "$@"
}

haskell-env () {
  env-type "haskellEnv" "$@"
}

haskell-env-hoogle () {
  env-type "haskellEnvHoogle" "$@"
}

Hoogle

Call haskell-env-hoogle in your shell. This will build all of your packages + docs and load you into an environment with hoogle in scope. At this point I usually type:

hoogle server --local -p 8080 &> /tmp/hoogle.log & disown

to launch a hoogle server the the background. Eventually I want to have a systemd service for this so that I can just nixos-rebuild to regen docs and launch the server automatically.

Emacs

For emacs I've set the haskell-hoogle-url to http://localhost:8080/?hoogle=%s, so that I can get local hoogle docs for keywords under my cursor. I use spacemacs so I just type , h h for this functionality.

You can see my full nixpkgs config here: https://github.com/jb55/nix-files/blob/659798f2ca81fb7ad0cb5a29de576024ee16eef8/nixpkgs/config.nix#L20

Hope that helps.

like image 109
William Casarin Avatar answered Sep 20 '22 00:09

William Casarin


haskellPackages.hoogleLocal appears to be out of date; it doesn't exist anymore.

William Casarin's answer appears to be assuming you will have a single "haskell development environment" you use, rather than using nix-shell to have different dev environments for different projects.

What I've just figured out how to do instead is to write my shell.nix to override ghc.withPackages and ghcWithPackages to be ghc.withHoogle, so that when nix-shell creates an environment with a GHC that knows about all the necessary packages it also creates a hoogle database that knows about the same packages.

Here's my shell.nix1:

{ nixpkgs ? import <nixpkgs> {}, compiler ? "default", withHoogle ? true }:

let

  inherit (nixpkgs) pkgs;

  f = import ./default.nix;

  packageSet = (
    if compiler == "default"
      then  pkgs.haskellPackages
      else  pkgs.haskell.packages.${compiler}
  );

  haskellPackages = (
    if withHoogle
      then  packageSet.override {
              overrides = (self: super:
                {
                  ghc = super.ghc // { withPackages = super.ghc.withHoogle; };
                  ghcWithPackages = self.ghc.withPackages;
                }
              );
            }
      else  packageSet
  );

  drv = haskellPackages.callPackage f {};

in

  if pkgs.lib.inNixShell then drv.env else drv

I'm newish to nix, but I believe this should be pretty much "project independent"; I can use cabal2nix . > default.nix to generate a nix package from my cabal file when I change it, without having to touch shell.nix.

I haven't actually used this in real development yet, only a dummy project I was using to try to figure out how to get hoogle working in nix-shell.


1The skeleton of this was what cabal2nix --shell spits out, with the project-specific guts removed and replaced with f = import ./default.nix instead of embedding the nixified cabal package again.

like image 42
Ben Avatar answered Sep 22 '22 00:09

Ben