Rene Saarsoo
Rene Saarsoo

Reputation: 13917

How to install package for Haskell with stack and cabal?

I'm attempting to install one small Haskell package.

I set up a project using stack:

stack new my-project simple

This worked out fine. I'm able to build and run the code.

But then I stumbled into needing to use some other library. Searching the internet, I found that I should use cabal to do that. First installed it:

brew install cabal-install

Then updated it:

cabal update

Then tried to install a library with it:

cabal install --lib split

This resulted in:

Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.10.1.0 supports
'ghc' version < 9.8): /usr/local/bin/ghc is version 9.8.1
Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.10.1.0 supports
'ghc' version < 9.8): /usr/local/bin/ghc is version 9.8.1
Resolving dependencies...
Build profile: -w ghc-9.8.1 -O1
In order, the following will be built (use -v for more details):
 - split-0.2.4 (lib) (requires download & build)
Downloading  split-0.2.4
Downloaded   split-0.2.4
Starting     split-0.2.4 (lib)
Building     split-0.2.4 (lib)

Failed to build split-0.2.4.
Build log ( /Users/nene/.cache/cabal/logs/ghc-9.8.1/splt-0.2.4-d1ef7190.log ):
Configuring library for split-0.2.4..
Preprocessing library for split-0.2.4..
Building library for split-0.2.4..
[1 of 2] Compiling Data.List.Split.Internals ( src/Data/List/Split/Internals.hs, dist/build/Data/List/Split/Internals.o, dist/build/Data/List/Split/Internals.dyn_o )
[2 of 2] Compiling Data.List.Split  ( src/Data/List/Split.hs, dist/build/Data/List/Split.o, dist/build/Data/List/Split.dyn_o )
ld: warning: directory not found for option '-L/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/lib'
ld: unknown option: -no_fixup_chains
clang: error: linker command failed with exit code 1 (use -v to see invocation)
`clang' failed in phase `Linker'. (Exit code: 1)
Error: cabal: Failed to build split-0.2.4. See the build log above for
details.

There seems to be a conflict with cabal version (3.10.1.0) and GHC version (9.8.1). Not sure how to solve it.

Additionally I have a my-project.cabal file that stack created. Inside it I see:

cabal-version:       2.2

If my mental model is correct, then stack uses its own separate GHC installation and (possibly) also its own Cabal installation. But I have no idea how to access these.

I tried creating package.yaml file (as suggested by Silvio Mayolo) with the following contents:

dependencies:
- split

But this seems to have no effect. The build simply complains that it's unable to find Data.List.Split module (which the split library should provide).

PS: For now I have simply implemented the little function I wanted to import by myself. But it would be nice to figure out how to install some Haskell libraries.

Upvotes: 2

Views: 969

Answers (2)

Cigarette Smoking Man
Cigarette Smoking Man

Reputation: 635

If you just want to try out something quickly then cabal run my-script.hs, with my-script.hs being

{- cabal:
build-depends: base, split
-}
import Data.List.Split
main = print $ splitOn "x" "axbxc"

is the easiest (and single file) solution.

Some more notes:

  • use only cabal if you can, it's the core build and packaging tool for haskell
  • stack and stackage are (1) a project management tool (where you have more cabal libraries than one), and (2) a curated package list. "curated" as in "all these packages build together without errors", it's subset of the entire hackage, the place cabal goes to find all the packages. Stack depends on cabal, and adds its extra functionality on top.
  • package.yaml is a source for the yaml-based .cabal file generator hpack. (If you run hpack in the same folder where package.yaml is, it will create a my-package.cabal from it.) Stack also depends on hpack and runs it by default if it finds package.yaml files within your project
  • cabal has over the years gotten better at both project management and in .cabal file syntax (there is also the cabal.project file which enables multi-package projects), so one might get by with just using that

Upvotes: 1

ollimandoliini
ollimandoliini

Reputation: 550

To add a dependency to a project created with the simple template, you need to add it to the build-depends field in the .cabal file created by the template - in your case my-project.cabal.

After that, your cabal file should look like this:

cabal-version:       2.2

name:                my-project
version:             0.1.0.0
-- synopsis:
-- description:
homepage:            https://github.com/githubuser/my-project#readme
license:             BSD-3-Clause
license-file:        LICENSE
author:              Author name here
maintainer:          [email protected]
copyright:           2023 Author name here
category:            Web
build-type:          Simple
extra-source-files:  README.md
                     CHANGELOG.md

executable my-project
  hs-source-dirs:      src
  main-is:             Main.hs
  default-language:    Haskell2010
  build-depends:       base >= 4.7 && < 5, split
  ghc-options:         -Wall
                       -Wcompat
                       -Widentities
                       -Wincomplete-record-updates
                       -Wincomplete-uni-patterns
                       -Wmissing-export-lists
                       -Wmissing-home-modules
                       -Wpartial-fields
                       -Wredundant-constraints

Upvotes: 1

Related Questions