From 9fa67dd485ba033c0adfdfe1ef9265a066a2caff Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 3 Mar 2025 16:02:24 +0100 Subject: haskell/cabal-lib: Include `cabal repl -b` as an alternative --- haskell/cabal-lib.html | 16 ++++++++++------ haskell/cabal-lib.md | 20 ++++++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/haskell/cabal-lib.html b/haskell/cabal-lib.html index 4008c82..281d56b 100644 --- a/haskell/cabal-lib.html +++ b/haskell/cabal-lib.html @@ -1,5 +1,5 @@

About cabal install --lib

-

TL;DR: Don't use it, add the library to your package-name.cabal or package.yaml instead, or use a cabal script. After you learn more about the downsides, you can reconsider. See the "What to do instead" section below.

+

TL;DR: Don't use it, add the library to your package-name.cabal or package.yaml instead, or use a cabal script, or use cabal repl -b. After you learn more about the downsides, you can reconsider. See the "What to do instead" section below.


Suppose you are new to Haskell, or at least new to the current (2023) Haskell tooling, and would like to install a program written in Haskell. For example, say you would like to install a Haskell formatter, say fourmolu, and find that installing Haskell packages uses a tool called cabal. @@ -100,7 +100,14 @@ This is in ~/.ghc/architecture-OS-ghcversion/en

As mentioned, the compiled packages are still around (in ~/.cabal/store/ghc-version/), but removing those is tricky -- do not try it, cabal likes to maintain its own consistent set of packages in the "store". Removing the entire store folder for a particular GHC version is safe, however -- even though this does of course mean that you may need to recompile a lot of things later. :)

What to do instead

-

Create a project! +

If you just wanted a ghci session with some libraries in scope, use e.g. cabal repl -b parsec -b time. +(-b A is short for --build-depends=A.) +This will build those packages, if necessary, and then start a ghci session with those packages in scope.

+

If you want something slightly more permanent than a single ghci session, you can make a cabal script. +This allows you to effectively make a self-contained project inside a single Haskell file. +You specify the dependencies in a special comment block at the top of the file. +See the documentation for more details.

+

If you want more than that: create a project! The intended mode of operation of the modern Haskell tooling, that is cabal or stack, is to always work inside of a project. Often, "project" basically means "package", but you can have projects with multiple packages in them (using a cabal.project file, see the docs).

Creating a package is easily done using cabal init --simple inside a fresh directory. @@ -114,7 +121,4 @@ A package (a single thing in the package repository, should you decide to upload cabal will automatically ensure that a consistent set of versions is compiled and made available, if at all possible. You can also add version bounds to your dependencies if you want to apply some proper software engineering principles.

(If you want to use stack instead of cabal, try their getting started guide.)

-

An even lighter-weight alternative

-

An alternative to creating a project is to make a cabal script: this allows you to effectively make a self-contained project inside a single Haskell file. -You specify the dependencies in a special comment block at the top of the file. -See the documentation for more details.

+

Edited 2025-03-03 to include cabal repl as an alternative.

diff --git a/haskell/cabal-lib.md b/haskell/cabal-lib.md index 6b6c7ca..dc3b79e 100644 --- a/haskell/cabal-lib.md +++ b/haskell/cabal-lib.md @@ -1,6 +1,6 @@ ## About `cabal install --lib` -**TL;DR: Don't use it, add the library to your *package-name*.cabal or `package.yaml` instead, or use a [cabal script](https://cabal.readthedocs.io/en/3.10/getting-started.html#run-a-single-file-haskell-script). After you learn more about the downsides, you can reconsider. See the "What to do instead" section below.** +**TL;DR: Don't use it, add the library to your *package-name*.cabal or `package.yaml` instead, or use a [cabal script](https://cabal.readthedocs.io/en/3.10/getting-started.html#run-a-single-file-haskell-script), or use `cabal repl -b`. After you learn more about the downsides, you can reconsider. See the "What to do instead" section below.** --- @@ -150,7 +150,16 @@ Removing the entire store folder for a particular GHC version is safe, however - ## What to do instead -Create a project! +If you just wanted a `ghci` session with some libraries in scope, use e.g. `cabal repl -b parsec -b time`. +(`-b A` is short for `--build-depends=A`.) +This will build those packages, if necessary, and then start a `ghci` session with those packages in scope. + +If you want something slightly more permanent than a single `ghci` session, you can make a _cabal script_. +This allows you to effectively make a self-contained project inside a single Haskell file. +You specify the dependencies in a special comment block at the top of the file. +See [the documentation](https://cabal.readthedocs.io/en/3.10/getting-started.html#run-a-single-file-haskell-script) for more details. + +If you want more than that: create a project! The intended mode of operation of the modern Haskell tooling, that is `cabal` or `stack`, is to always work inside of a _project_. Often, "project" basically means "package", but you can have projects with multiple packages in them (using a `cabal.project` file, see [the docs](https://cabal.readthedocs.io/en/3.10/cabal-project.html)). @@ -169,9 +178,4 @@ You can also add [version bounds](https://cabal.readthedocs.io/en/3.10/cabal-pac (If you want to use `stack` instead of `cabal`, try [their getting started guide](https://docs.haskellstack.org/en/stable/GUIDE/).) - -### An even lighter-weight alternative - -An alternative to creating a project is to make a _cabal script_: this allows you to effectively make a self-contained project inside a single Haskell file. -You specify the dependencies in a special comment block at the top of the file. -See [the documentation](https://cabal.readthedocs.io/en/3.10/getting-started.html#run-a-single-file-haskell-script) for more details. +_Edited 2025-03-03 to include `cabal repl` as an alternative._ -- cgit v1.2.3-70-g09d2