Haskell: ghc package environment - haskell

Hi in my package toaster, i cabal v2-install lib:toaster --package-env ~/myenv/
In the ~/myenv/ dir, there is .ghc.environment.x86_64-linux-9.2.2
I've also set the environment variable to point to the myenv dir.
Unfortunately, cabal install doesn't update the environment.
Am i doing the wrong way?
when I runghc Hello.hs, it says that toaster is hidden.
Found the answer:
cabal install --lib lib:toaster --overwrite-policy=always --env ~/dev/haskell/pkg_env/

By default, cabal install only installs executables, storing libraries built along the way only in its cache. If you want the library to be put somewhere, you must add --lib to your cabal install line.

Related

What is the difference between install, v1-install, v2-install and new-install?

The commands install, v1-install, v2-install and new-install are simply described to "Install packages" when running man cabal. Are they different from one another? Which is the preferred option?
For modern versions of cabal-install, install, new-install and v2-install are the same. v1-install and the other v1- commands are obsolete and should not be used anymore.
Notice that, when developing a cabal package, the install command is largely unnecessary. Running cabal build and cabal repl will automatically install the required dependencies (listed in the build-depends: section of the cabal file).
cabal install is still useful in cases like the following:
Installing an executable from a package. Like the warp executable from the package wai-app-static. In those cases, the options --overwrite-policy, --install-method and --installdir are useful.
Creating a local package environment in some folder, so that "bare" invocations of ghc and ghci see some desired set of libraries. Like this. In those cases, the options --lib and --package-env are useful.

Can't install "System.Random" by cabal

I try to install "System.Random" by cabal through Powershell & Git Bash.
getting this result.
PS C:\Users\xxx> cabal install random
Resolving dependencies...
Up to date
Warning: You asked to install executables, but there are no executables in
target: random. Perhaps you want to use --lib to install libraries instead.
And then I try to input like this
cabal install --lib random
Resolving dependencies...
and
cabal install random --lib
both output Resolving dependencies... Up to date without warning.
but ghci Could not find module `System.Random'
input cabal install random still having the same result with warning.
It is a bad idea to install packages globally, so cabal install doesn't do that. The package is built and placed into the Cabal package database, but GHC won't find it unless you specifically tell Cabal to point GHC at it:
cabal repl -b random # -b is short for --build-depends
# Note that cabal install isn't really necessary: the above command would have installed random if it wasn't there already
I think your Cabal/GHC installation might be outdated, however. When you do cabal install --lib random, recent versions of Cabal should write out an "environment file" at %APPDATA%\ghc\arch-os-ghcversion\environments\default, which GHC should then automatically read (GHCi should say something like Loaded package environment from ...), and it should then find the installed package. If you are using the latest version of everything,
cabal install --lib random
ghci
should work.

Haskell Cabal v2 and Sandbox

We are told that this is now legacy mode of cabal, to manage a user-defined sandbox:
cabal init sandbox
cabal install <some stuff>
Which is later loaded at your discretion using
cabal exec bash
Question: How is an equivalent operation performed using the new implementation of Cabal?
The documentation is (as it currently stands) very cryptic with zero usage example. That would be helpful to facilitate migration.
Currently contemplating Cabal 2.4.0.0 with GHC 8.6.5.
There are no sandboxes. You can install packages and they end up in the user's cabal store using v2-install and v2-install --lib. Let's use cabal to install a few packages:
cabal v2-udpate
cabal v2-install --lib generic-trie containers
Now we can load modules from these packages in GHCi:
ghci
> import Data.GenericTrie
> import Data.Map
Why does this work? Because the cabal v2-install --lib modifies the default environment GHCi looks at to load packages. This file is found at .ghc/x86_64-linux-8.6.5/environments/default.
You can delete the environment file any time to start mostly fresh. This is handy if cabal is telling you some set of packages have conflicting dependencies with prior packages. Think of it as a global store but as just pointers to the nix-style builds, so it's cheap to blow away and redo.

What is the common workflow of using cabal for personal project?

Assume I want to creat my own local library called MyLib, my workflow is:
$ cabal init
# # edit the ".cabal" file, set the "exposed-modules" as "MyLib"
# # edit "MyLib.hs" located in "src"
$ cabal sandbox init
$ cabal install
So my question is
is my workflow an acceptable one, or could it be better?
now I have a compiled library in .cabal-sandbox/lib, how could I use it or import MyLib from my another haskell project? It seems that packages downloaded from hackage through cabal install are stored in ~/.cabal, while my own locally installed package is not there.
what is the difference between cabal build and cabal install, it seems that after running cabal build, I could already run my lib through cabal repl, so what extra jobs does cabal install do?
I've never used cabal sandbox so I can't speak much to it.
cabal build compiles your source code into your dist directory.
cabal install takes your compiled source code and sends it to your ~/.cabal directory, and registers it in your ~/.ghc directory. Now you can import it into other code just as you would any other library you've installed with cabal.
In my personal projects, I use cabal configure, cabal build, cabal repl, and cabal install. And configure is kind of optional.

How do you install packages/libraries without Cabal or Cabal-Install?

I'm trying to set up Haskell from scratch, on Ubuntu 11.04, without using the outdated Debian repository or Haskell-Platform.
I've installed GHC-7.0.4 from source with no problem, and now need to install Cabal (which appears to already be included in GHC in /usr/local/lib/ghc-7.0.4/Cabal-1.10.2.0) and Cabal Install.
The latter specifies several dependencies (parsec and network), each of which has several dependencies of their own (mtl, text, etc).
What's the command to install these packages, that I downloaded from hackage in tar.gz form?
Unpack, then runhaskell doesn't work.
I see Setup.lhs, but it's not clear what that's for or how to use it.
Most of the Haskell documentation I've found assumes you've installed from a repo or Haskell-Package and doesn't really explain this well.
cabal-install has a shell script that does this. If you download it from hackage and install it, you can start bootstrap.sh to install cabal-install. You can then use it to install other packages.
There are two different packages: Cabal and cabal-install. Cabal is a library, and cabal-install is an executable named cabal.
To install a package, cabal-install is an optional convenience wrapper around Cabal, but Cabal is required.
According to http://hackage.haskell.org/trac/ghc/wiki/Commentary/Libraries , Cabal is a 'zero-boot' package, so when you build GHC, Cabal and its dependencies are built for you automatically.
You can use ghc-pkg executable to check which packages are already installed:
# ghc-pkg list
Check if Cabal is in the list after you build GHC. If yes, you can install more packages without cabal-install using this documentation:
http://haskell.org/haskellwiki/Cabal/How_to_install_a_Cabal_package
I suggest you to install cabal-install first, and then install everything else using cabal-install executable. A usual commandine for global installation is this:
# runhaskell Setup configure
# runhaskell Setup build
# sudo runhaskell Setup install
Unpack a package tarball and run the commands in the folder with Setup.hs or Setup.lhs files. Note that a per-user non-root installation is also supported - Use runhaskell Setup configure --user
When you install cabal executable and its dependencies this way, use cabal install {package-name} to install more packages.
Note that Haskell Platform exists mostly because of the pain of installing cabal-install by yourself.

Resources