在Nix上的Haskell开发环境中使用Hoogle

15
我正在尝试在类似O'Charles' wiki描述的Haskell开发环境中使用Hoogle:
我已经修改了shell.nix,以便使用hoogleLocal,但似乎没有为我安装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 包含到 buildInputs 中,hoogle 二进制文件就会被安装,但它无法找到数据库。下面是当我尝试使用它时发生的情况。
$ 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与此答案中的相同。
3个回答

14

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

~/.nixpkgs/config.nix里:

环境助手函数

首先,定义一个名为haskellEnvFun的函数来构建Haskell环境:

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="$1"
  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

希望这有所帮助。


14

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

William Casarin的回答似乎假定您将拥有一个单独的“Haskell开发环境”,而不是使用nix-shell为不同的项目使用不同的开发环境。

我刚刚想到的替代方法是编写我的shell.nix以覆盖ghc.withPackagesghcWithPackagesghc.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比较陌生,但我相信这应该是“与项目无关”的;当我更改cabal文件时,我可以使用cabal2nix . > default.nix从中生成一个nix包,而不必触及shell.nix。

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


1这个的骨架是由cabal2nix --shell输出的,已经移除了特定于项目的部分,用f = import ./default.nix替换了再次嵌入nixified cabal软件包的部分。


是的!我曾经这样做。当编写大量Haskell代码时,我发现将所有内容放在一个shell中以进行快速开发更容易。但是如果您只有一个正在处理的项目,则此方法非常有效。 - William Casarin

2

参考@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 {});```

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接