3

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
wizzup
  • 2,361
  • 20
  • 34
  • The nix way of using development packages is indeed to go through `nix-shell` like you did. There might be specific issues with Haskell: I would advise you to read the [Haskell section of the nixpkgs manual](https://nixos.org/nixpkgs/manual/#users-guide-to-the-haskell-infrastructure) to learn more. – Zimm i48 May 19 '17 at 09:15

1 Answers1

5

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

Shersh
  • 9,019
  • 3
  • 33
  • 61
  • 2
    This method only work for `ghc` but not for `ghc-mod`; `$ nix-shell -p "haskellPackages.ghcWithPackages (self: with self; [random ghc-mod])"`; I am so confuse about nixos haskell system. – wizzup May 18 '17 at 12:42
  • 1
    I wrote an answer to another question where I explain why it didn't work: https://stackoverflow.com/a/45023011/450128 (the question itself isn't strictly a duplicate) – Ben Sep 15 '17 at 07:57
  • Shersh thank you for the helpful answer, your 'nix-shell' line there worked for me. Also, fyi that link to the post appears to be broken. – mherzl Jun 12 '22 at 16:20
  • 1
    @mherzl I'm glad you liked the answer :) I've updated the link to the one from WebArchive. – Shersh Jun 12 '22 at 19:13