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

时间:2015-01-01 06:42:24

标签: haskell nixos hoogle

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

我已经修改了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与this answer中的相同。

3 个答案:

答案 0 :(得分:10)

这是我的Nix 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)]; }; 中的

环境助手功能

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

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

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

定义一些环境

调用此函数可定义两个环境:一个用于在更改时运行Hoogle构建器,另一个用于:

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

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

~/.profile

Shell助手

haskell-env-hoogle中定义一些bash函数以便为方便起见加载这些环境:

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

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

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

Hoogle

在你的shell中调用hoogle。这将构建您的所有包+文档,并将您加载到范围为hoogle server --local -p 8080 &> /tmp/hoogle.log & disown 的环境中。此时我通常输入:

haskell-hoogle-url

启动一个hoogle服务器的后台。最终我想为此设置一个systemd服务,以便我可以通过nixos-rebuild重新生成文档并自动启动服务器。

的Emacs

对于emacs,我已将http://localhost:8080/?hoogle=%s设置为, h h,以便我可以在光标下获取关键字的本地hoogle文档。我使用spacemacs,因此我只需输入<div id="typeahead-box"> <div class="ic-tokens"> <div class="ic-token">Hover over</div> </div> <div> <div id="tooltip">Should show</div> </div> </div> 即可获得此功能。

您可以在此处查看我的完整nixpkgs配置:QSettings Class

希望有所帮助。

答案 1 :(得分:8)

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

William Casarin的回答似乎是假设你将拥有一个单一的&#34; haskell开发环境&#34;你使用,而不是使用nix-shell为不同的项目设置不同的开发环境。

我刚才想出的方法是编写我的shell.nix以覆盖ghc.withPackagesghcWithPackagesghc.withHoogle,这样当nix-shell时使用GHC创建一个环境,该环境知道所有必要的软件包,它还会创建一个知道相同软件包的hoogle数据库。

这是我的shell.nix 1

{ 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很新,但我相信这应该是非常多的&#34;项目独立&#34 ;;当我更改它时,我可以使用cabal2nix . > default.nix从我的cabal文件生成一个nix包,而不必触及shell.nix。

我还没有在实际开发中实际使用它,只是我用来试图找出如何让nogle-shell工作的虚拟项目。

1 这是cabal2nix --shell吐出的内容,删除了项目特定的内容并替换为f = import ./default.nix,而不是再次嵌入nixified cabal包。 / p>

答案 2 :(得分:0)

使用@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 {});```