Search code examples
haskellnixnixos

Could not find module in nix-shell


What could be the reason for missing module?

$ nix-shell -p haskellPackages.ghc -p haskellPackages.random

give following shell

[nix-shell:~]$ ghci 
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /data/works/dotfiles/ghci
Prelude> import System.Random

<no location info>: error:
    Could not find module ‘System.Random’
    It is not a module in the current program, or in any known package.

And what is the nix way to install and use Haskell packages?

I thought nixos.haskellPackages.<package> will automatically registered for ghc but it seem like it is not the case.

Reinstall the random inside the shell don't fix it.

[nix-shell:~]$ nix-env -iA nixos.haskellPackages.random
installing ‘random-1.1’

[nix-shell:~]$ ghc-pkg list | grep -i random

Solution

  • I'm not aware of reasons why this doesn't work. But you could try this command:

    nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [random])"
    

    Works for me. And now ghci sees System.Random package.

    UPDATE:

    This post is really helpful for beginners in their nix + haskell adventure:

    https://web.archive.org/web/20170910171927/http://alpmestan.com/posts/2017-09-06-quick-haskell-hacking-with-nix.html