问题描述
如果我使用cabal sandbox init
创建一个阴谋沙箱,则可以使用cabal repl
或cabal exec ghc(i)
来处理这些软件包,而无需创建项目:
If I create a cabal sandbox with cabal sandbox init
, I can use cabal repl
or cabal exec ghc(i)
to work with those packages without creating a 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 I change the path to something else, even to a subdirectory, I cannot access the packages anymore:
$ mkdir -p /tmp/example/sub && cd /tmp/example/sub
$ cabal exec ghci
Prelude> :m Test.QuickCheck
<no location info>:
Could not find module ‘Test.QuickCheck’
It is not a module in the current program, or in any known package.
是否可以使用沙箱中的内容而不复制其内容?
Is there any way to use the contents from the sandbox, without copying its content?
推荐答案
问题是cabal
仅会尊重当前工作目录中的沙箱.但是,有几个选项可以为Cabal指定沙箱位置,也可以为GHC指定包装数据库.
The problem is that cabal
will only respect sandboxes in the current working directory. However, there are several options where you can specify a sandbox location for cabal or the package databse for GHC.
您可以使用cabal
的--sandbox-config-file
选项来指定沙箱配置,例如
You can use cabal
's --sandbox-config-file
option to specify a sandbox configuration, e.g.
$ cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config exec ghci
Prelude> :m Test.QuickCheck
Prelude Test.QuickCheck>
这还使您能够从其他位置更改沙箱,如果您只想将随机内容安装到临时位置,这将非常方便:
This also enables you to change the sandbox from other places, which comes in handy if you just want to install random stuff into a 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 this gets cumbersome after a while, you should probably add an alias
$ alias sandboxed-cabal="cabal --sandbox-config-file=/tmp/example/cabal.sandbox.config"
$ sandboxed-cabal repl
Prelude>
使用ghc -package-db
或者,当您将GHC与 -package-db
:
Using ghc -package-db
Alternatively, you can directly specify the package database when you use GHC with -package-db
:
$ ghci -package-db /tmp/example/.cabal-sandbox/<ARCH>-packages.conf.d
Prelude> :m Test.QuickCheck
Prelude Test.QuickCheck>
<ARCH>
取决于您的系统和使用的GHC,例如在64位Linux和GHC 7.10.3上,它是x86_64-linux-ghc-7.10.3-packages.conf.d
.然后,您可以使用该数据库中的所有软件包:
The <ARCH>
depends on your system and the used GHC, e.g. on a 64bit Linux and GHC 7.10.3 it's x86_64-linux-ghc-7.10.3-packages.conf.d
. You can then use all packages in that database:
$ ghci -package-db /tmp/example/.cabal-sandbox/<ARCH>-packages.conf.d
Prelude> :m Control.Lens
Prelude Control.Lens>
同样,别名应该派上用场.
Again, an alias should come in handy.
最后但并非最不重要的一点是,您可以调整环境变量.但是,如果环境变量 GHC_PACKAGE_PATH
存在,它将覆盖GHC的常规软件包数据库,因此您需要检查ghc-pkg list
并将其也添加
Last, but not least, you can adjust an environment variable. However, if the environment variable GHC_PACKAGE_PATH
exists, it will overwrite GHC's usual package databases, so you either need to check ghc-pkg list
and 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
或使用 -global-package-db
和 -user-package-db
以重新启用它们:
or use -global-package-db
and -user-package-db
to 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
这篇关于如何将GHC与不在当前工作目录中的阴谋集团沙箱一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!