Dependencies
| Add a dependency | |
| Update the index | |
| Version bounds | |
| Freeze and lock |
Add a dependency
Hackage hosts the packages. Adding one means one line in build-depends, and Cabal resolves the rest:
build-depends:
base >=4.18 && <5
, text >=2.0 && <3
, containers >=0.6 && <0.8
, aeson >=2.2 && <3
text gives packed strings, containers gives maps and sets, and aeson reads and writes JSON. Those four plus base cover most command line tools.
Update the index
Cabal keeps a local copy of the package index. When a version you need is missing, refresh it:
cabal update
Downloading the latest package list from hackage.haskell.org Package list of hackage.haskell.org is up to date.
Then build. The solver prints the plan it chose, and the first build compiles the dependencies, which can take several minutes:
cabal build
Resolving dependencies... Build profile: -w ghc-9.10.1 -O1 In order, the following will be built: - text-2.1.1 (lib) - todo-0.1.0.0 (exe:todo)
Version bounds
A bound says which versions your code was tested with. Too tight and the project blocks every update; too loose and a breaking release compiles by accident:
-- reasonable: one major version window
text >=2.0 && <3
-- too loose: any version, including the next rewrite
text
The convention is a lower bound on the version you actually used and an exclusive upper bound on the next major release. The cabal check command flags missing bounds before you upload a package.
Freeze and lock
A build that worked today should work next year. Freezing records the exact versions in a plan file, and a project file pins the compiler and index state:
cabal freeze
Wrote freeze file: /home/ada/todo/cabal.project.freeze
Commit cabal.project.freeze for an
application and leave it out for a library. Applications want
reproducible builds, libraries want to be testable against new
dependency versions.
Next:
Testing with HUnit
Article author: Arthur Isaev