如何覆盖`doctest`以使用我的`ghc`?

时间:2018-09-24 01:51:16

标签: haskell quickcheck doctest nix nixos

我在将doctest上的QuickCheckNixOS一起使用时遇到问题

-- code.hs
--
-- $setup
-- >>> import Test.QuickCheck
--
-- |
-- This test will pass
-- >>> 1 + 1
-- 2
--
-- This test use QuickCheck
-- prob> \xs -> reverse xs = reverse . id $ xs
--
-- The last test will fail to ensure doctest was running
-- >>> 1 == 0
-- True
--

main :: IO ()
main = pure ()

默认haskellPackages.doctest不包含QuickCheck

$ nix-shell -p haskellPackages.doctest --run 'doctest code.hs'
code.hs:4: failure in expression `import Test.QuickCheck'
expected:
 but got:
          <no location info>: error:
              Could not find module ‘Test.QuickCheck’
              It is not a module in the current program, or in any known package.

Examples: 3  Tried: 1  Errors: 0  Failures: 1

所以我尝试将替代版本设为mydoctest

# ~/.config/nixpkgs/overlays/doctest.nix

self: super:
with super.haskellPackages;
let
  myghc = ghcWithPackages (p: with p;
          [
            QuickCheck
          ]);
in
{
  mydoctest = doctest.override {
    ghc = myghc;
  };
}

它仍然找不到QuickCheck

$ nix-shell -p mydoctest --run 'doctest code.hs'
code.hs:4: failure in expression `import Test.QuickCheck'
expected:
 but got:
          <no location info>: error:
              Could not find module ‘Test.QuickCheck’
              It is not a module in the current program, or in any known package.

Examples: 3  Tried: 1  Errors: 0  Failures: 1

但是如果我通过ghc调用它就可以了

$ nix-shell -p mydoctest --run 'ghc -e ":!doctest code.hs"'
code.hs:15: failure in expression `1 == 0'
expected: True
 but got: False

Examples: 3  Tried: 3  Errors: 0  Failures: 1

myghc在这里,但未被mydoctest使用

$ nix-shell -p mydoctest --run 'doctest --version'
doctest version 0.16.0
using version 8.4.3 of the GHC API
using /nix/store/g1dj8c6h6g8rbb8cv9bgmp1h2f3bxk1l-ghc-8.4.3-with-packages/bin/ghc

$ nix-shell -p mydoctest --run 'which ghc'
/nix/store/qj3vnm903p77nxiraxqxm9b8gbz5rpia-ghc-8.4.3-with-packages/bin/ghc

如何使doctest使用覆盖ghc

2 个答案:

答案 0 :(得分:0)

Haskell派生本身不适合作为nix-shell环境。

Nixpkgs手册recommends仅使用nix-shell调用作为依赖项,仅出于ghcWithPackages的目的而创建派生。

如果您要运行的文件是haskell软件包的一部分,则可以use cabal2nix为您完成大部分工作。

我建议使用后者,因为它不会污染您整个用户的状态(配置文件或用户特定的覆盖图),更易于与他人共享,并提供足以从玩具示例扩展到有价值的包装的结构

答案 1 :(得分:0)

我在尝试设置 nix/haskell 开发环境时遇到了同样的问题,并在此 reddit post 中找到了解决方案。要让 doctest 使用特定版本的 ghc,您可以设置环境变量 NIX_GHC

$ doctest --version
doctest version 0.16.3
using version 8.10.3 of the GHC API
using /nix/store/9bm9bs9c4zpl17zdk1d0azid44x3h99b-ghc-8.10.3/bin/ghc-8.10.3

$ export NIX_GHC=/nix/store/zvx9dmfqaa8wibygkbibg4kl98pfj9qv-ghc-8.10.3-with-packages/bin/ghc
$ doctest --version
doctest version 0.16.3
using version 8.10.3 of the GHC API
using /nix/store/zvx9dmfqaa8wibygkbibg4kl98pfj9qv-ghc-8.10.3-with-packages/bin/ghc