在 nix 上的 haskell 开发环境中使用 hoogle

Using hoogle in a haskell development environment on nix

我正试图在 Haskell 开发环境中使用 hoogle 就像O'Charles' wiki描述的那样:

为了使用 hoogleLocal,我已经像下面那样修改了 shell.nix,但它似乎没有为我安装 hoogle 二进制文件。

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)))
  ];
}

在生成的 shell 中,hoogle 二进制文件不可用。

如果我将 hs.hoogle 添加到 buildInputshoogle 二进制文件已安装,但找不到数据库。下面是我尝试使用它时发生的情况。

$ 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)
$

我如何才能使它像 O'Charles 所描述的那样正确工作?

编辑:原shell.nix与this answer.

相同

这是我的 Nix Haskell 开发环境的样子

~/.nixpkgs/config.nix中:

环境辅助函数

首先,定义一个用于构建 Haskell 环境的 haskellEnvFun 函数:

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)];
  };

定义一些环境

调用这个函数来定义两种环境:一种用于 运行 Hoogle 构建器的变化,一种不用于:

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

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

套餐

定义您要在本地 Haskell 开发环境中使用的所有包:

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 帮手

在您的 ~/.profile 中定义一对 bash 函数来加载这些环境以方便使用:

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

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

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

Hoogle

在您的 shell 中呼叫 haskell-env-hoogle。这将构建您所有的包 + 文档,并将您加载到范围为 hoogle 的环境中。此时我通常输入:

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

在后台启动一个 hoogle 服务器。最终我想为此提供一个 systemd 服务,这样我就可以通过 nixos-rebuild 来重新生成文档并自动启动服务器。

Emacs

对于 emacs,我将 haskell-hoogle-url 设置为 http://localhost:8080/?hoogle=%s,这样我就可以获取光标下关键字的本地 hoogle 文档。我使用 spacemacs,所以我只需键入 , h h 即可实现此功能。

你可以在这里看到我完整的 nixpkgs 配置:https://github.com/jb55/nix-files/blob/659798f2ca81fb7ad0cb5a29de576024ee16eef8/nixpkgs/config.nix#L20

希望对您有所帮助。

haskellPackages.hoogleLocal 似乎已过时;它不再存在了。

William Casarin 的回答似乎是假设您将使用一个 "haskell development environment",而不是使用 nix-shell 为不同的项目提供不同的开发环境。

我刚刚想出的方法是编写我的 shell.nix 来覆盖 ghc.withPackagesghcWithPackages 成为 ghc.withHoogle,这样当 nix-shell 创建一个带有 GHC 的环境,它知道所有必要的包它还创建一个 hoogle 知道相同包的数据库。

这是我的 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

我是 nix 的新手,但我相信这应该差不多 "project independent";当我更改它时,我可以使用 cabal2nix . > default.nix 从我的 cabal 文件生成一个 nix 包,而不必触摸 shell.nix.

我还没有真正在实际开发中使用过它,我只是用一个虚拟项目来尝试弄清楚如何让 hoogle 在 nix-shell 中工作。


1这个框架是 cabal2nix --shell 吐出来的,删除了 project-specific 胆量并用 f = import ./default.nix 代替而不是嵌入又是 nixified cabal 包。

使用@Ben 的回答作为参考,这是我需要对 cabal2nix --shell 文件进行的所需更改的差异:

diff --git a/shell.nix b/shell.nix
index 540ade3..e207d6e 100644
--- a/shell.nix
+++ b/shell.nix
@@ -1,4 +1,4 @@
-{ nixpkgs ? import <nixpkgs> {}, compiler ? "default", doBenchmark ? false }:
+{ nixpkgs ? import <nixpkgs> {}, compiler ? "default", doBenchmark ? false , withHoogle ? true}:

 let

@@ -21,10 +21,23 @@ let
         license = stdenv.lib.licenses.bsd3;
       };

-  haskellPackages = if compiler == "default"
+  haskellPackages' = if compiler == "default"
                        then pkgs.haskellPackages
                        else pkgs.haskell.packages.${compiler};

+  haskellPackages = (
+    if withHoogle
+    then  haskellPackages'.override {
+      overrides = (self: super:
+        {
+          ghc = super.ghc // { withPackages = super.ghc.withHoogle; };
+          ghcWithPackages = self.ghc.withPackages;
+        }
+      );
+    }
+    else haskellPackages'
+  );
+
   variant = if doBenchmark then pkgs.haskell.lib.doBenchmark else pkgs.lib.id;

   drv = variant (haskellPackages.callPackage f {});```