4

I have problem using doctest with QuickCheck on NixOS

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

The default haskellPackages.doctest doesn't include 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

so I try to make an overrride version as mydoctest

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

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

It still can't find 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

but it work if I call it via 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 is there but didn't use by 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

How can I make doctest to use my override ghc ?

wizzup
  • 2,361
  • 20
  • 34

2 Answers2

1

Haskell derivations are not suitable as nix-shell environments by themselves.

The Nixpkgs manual recommends creating a derivation for the purpose of a nix-shell only, using a ghcWithPackages invocation as a dependency.

If the file you're trying to run is part of a haskell package, you can use cabal2nix to do most of the work for you.

I recommend the latter, because it does not pollute your user-wide state (whether profile or user-specific overlay), it is easier to share with others and provides structure that is sufficient to scale from toy example to a valuable package.

Robert Hensing
  • 6,708
  • 18
  • 23
0

I had the same problem trying to get my nix/haskell dev environment set up and found a solution in this reddit post. To get doctest to use a specific version of ghc you can set the environment variable 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
Theo Sherry
  • 113
  • 1
  • 3
  • 10