haskell - How can I use GHC with a cabal sandbox that's not in the current working directory? -


if create cabal sandbox cabal sandbox init, can use cabal repl or cabal exec ghc(i) work packages without creating project:

$ mkdir /tmp/example && cd /tmp/example $ cabal sandbox init $ cabal install quickcheck $ cabal exec ghci prelude> :m test.quickcheck prelude test.quickcheck> 

however, if change path else, subdirectory, cannot access packages anymore:

$ mkdir -p /tmp/example/sub && cd /tmp/example/sub $ cabal exec ghci prelude> :m test.quickcheck <no location info>:     not find module ‘test.quickcheck’     not module in current program, or in known package. 

is there way use contents sandbox, without copying content?

the problem cabal respect sandboxes in current working directory. however, there several options can specify sandbox location cabal or package databse ghc.

using cabal features

you can use cabal's --sandbox-config-file option specify sandbox configuration, e.g.

$ cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config exec ghci prelude> :m test.quickcheck prelude test.quickcheck>    

this enables change sandbox other places, comes in handy if want install random stuff temporary place:

$ cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config install lens $ cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config repl prelude> :m control.lens prelude control.lens> :m test.quickcheck prelude control.lens test.quickcheck> 

since gets cumbersome after while, should add alias

$ alias sandboxed-cabal="cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config" $ sandboxed-cabal repl prelude> 

using ghc -package-db

alternatively, can directly specify package database when use ghc -package-db:

$ ghci -package-db /tmp/example/.cabal-sandbox/<arch>-packages.conf.d prelude> :m test.quickcheck prelude test.quickcheck> 

the <arch> depends on system , used ghc, e.g. on 64bit linux , ghc 7.10.3 it's x86_64-linux-ghc-7.10.3-packages.conf.d. can use packages in database:

$ ghci -package-db /tmp/example/.cabal-sandbox/<arch>-packages.conf.d prelude> :m control.lens prelude control.lens>  

again, alias should come in handy.

using ghc_package_path

last, not least, can adjust environment variable. however, if environment variable ghc_package_path exists, overwrite ghc's usual package databases, either need check ghc-pkg list , add them too

$ ghc_package_path=/opt/ghc/7.10.3/lib/ghc-7.10.3/package.conf.d/:/tmp/example/.cabal-sandbox/x86_64-linux-ghc-7.10.3-packages.conf.d ghci 

or use -global-package-db , -user-package-db reenable them:

$ ghc_package_path=/tmp/example/.cabal-sandbox/x86_64-linux-ghc-7.10.3-packages.conf.d ghci -global-package-db -user-package-db 

Comments