nix-playgraound/notes/007-haskell.md
2026-04-21 10:53:35 +02:00

74 lines
2.1 KiB
Markdown

# Haskell Project
This note covers `05-haskell/`, which packages a tiny Cabal executable with Nix and provides a dev shell for editing it.
---
## 1. What the Example Teaches
The example combines three pieces that show up in real Haskell projects:
- a local Cabal package, defined by `mini-haskell.cabal`,
- a flake output that builds that package with `callCabal2nix`, and
- a dev shell that provides GHC, `cabal-install`, and Haskell Language Server.
That keeps the example focused on one idea: a flake can describe both how to build a Haskell program and how to work on it interactively.
---
## 2. The Package Build
`pkgs.haskellPackages.callCabal2nix` reads the local Cabal file and produces a Nix derivation for the executable:
```nix
project = haskellPackages.callCabal2nix "mini-haskell" ./. { };
```
The first argument is the package name as it should appear in Nix. The second is the source tree. The third is an attrset of overrides, which this example leaves empty.
That derivation becomes `packages.<system>.default`, so `nix build` produces the executable, and `nix run` executes it.
---
## 3. The Dev Shell
The dev shell uses `pkgs.mkShell` and adds the tools you need to edit and run the project:
- `ghc` for the compiler,
- `cabal-install` for local development commands, and
- `haskell-language-server` for editor support.
This keeps the shell small and obvious. For projects with many Haskell dependencies, `shellFor` can construct a shell from the package set itself, but this example stays with `mkShell` to keep the mechanics visible.
---
## 4. The Check
`checks.<system>.greeting` runs the built executable with an argument and compares its output to an expected string.
That gives `nix flake check` one concrete behavior to verify:
- the Cabal package evaluates,
- the executable builds, and
- the program runs and prints the expected result.
---
## 5. Commands to Try
```bash
cd 05-haskell
nix develop
cabal run
cabal run -- flakes
nix build
./result/bin/mini-haskell
./result/bin/mini-haskell flakes
nix run
nix run . -- flakes
nix flake check
```