How can I install a Haskell library to be accessible via GHCi with Nixos?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 00:08:09

问题


I've managed to install ghc with nix-env -i ghc.

I'd like to install a Haskell library now, how should this be done? For example the turtle (https://hackage.haskell.org/package/turtle) library.

I've run nix-env -f "<nixpkgs>" -iA haskellPackages.turtle, however running ghci and import Turtle fails:

Prelude> import Turtle

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

Output of ghc-pkg list:

/nix/store/fvf278s3lqsjv488ahhdi8jx6i0qzsr9-ghc-8.0.2/lib/ghc-8.0.2/package.conf.d      
Cabal-1.24.2.0                          
array-0.5.1.1                           
base-4.9.1.0                            
binary-0.8.3.0                          
bytestring-0.10.8.1                     
containers-0.5.7.1                      
deepseq-1.4.2.0                         
directory-1.3.0.0                       
filepath-1.4.1.1                        
ghc-8.0.2                               
ghc-boot-8.0.2                          
ghc-boot-th-8.0.2                       
ghc-prim-0.5.0.0                        
ghci-8.0.2                              
haskeline-0.7.3.0                       
hoopl-3.10.2.1                          
hpc-0.6.0.3                             
integer-gmp-1.0.0.1                     
pretty-1.1.3.3                          
process-1.4.3.0                         
rts-1.0                                 
template-haskell-2.11.1.0               
terminfo-0.4.0.2                        
time-1.6.0.1                            
transformers-0.5.2.0                    
unix-2.7.2.1                            
xhtml-3000.2.1

回答1:


This works differently on NixOS because of purity. NixOS' GHC will only look at its own immutable installation directory and the packages that have been installed by the user with cabal install.

What you can do is install into your user profile a GHC wrapper that supplies a nice set of packages when you run ghci.

Create a file my-ghc.nix:

(import <nixpkgs> {}).haskellPackages.ghcWithPackages (hpkgs: with hpkgs; [
    lens
    aeson
    turtle
])

Uninstall your previous attempt, to avoid name collisions:

nix-env -e ghc turtle

Install the wrapped GHC:

nix-env -if my-ghc.nix

You may edit the file in the future and re-run that command.

When I am working on a project, I prefer to use cabal2nix and nix-shell. (For an introduction, see Oliver Charles' blog post)




回答2:


As an alternative Robert's answer, one can use a nix-shell environment by creating a shell.nix file with contents of:

{ pkgs ? import <nixpkgs> {} }:
let myGhc = pkgs.haskellPackages.ghcWithPackages (hpkgs: with hpkgs; [
      turtle
    ]);
in
pkgs.mkShell {
  buildInputs = [ myGhc ];
}

And entering this environment with nix-shell.



来源:https://stackoverflow.com/questions/47377748/how-can-i-install-a-haskell-library-to-be-accessible-via-ghci-with-nixos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!