haskellcabalhaskell-stack

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.


Solution

  • 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:          example@example.com
    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