如何利用Stackage的cabal.config配置Nix风格Cabal项目?
Great question! Let's break this down into two clear parts—using a Stackage cabal.config with cabal new-configure, and alternative approaches for Nix-style Cabal projects that leverage Stackage snapshot versions.
This is straightforward and ensures your project uses the exact package versions validated in a Stackage snapshot:
- First, download the
cabal.configfile for your target Stackage snapshot (either an LTS release or Nightly build). You can find this file linked directly on the snapshot's main page. - Place the file in your project's root directory, or note its full file path if you keep it elsewhere.
- Run the configure command, pointing to the config file if it's not in your project root:
If you placedcabal new-configure --config-file=/path/to/your/cabal.configcabal.configdirectly in your project root, you can skip the flag—Cabal will automatically detect and use it when you run:cabal new-configure - That's it! Cabal will now respect all version constraints from the Stackage snapshot, ensuring your dependencies are consistent with the validated package set.
Pro tip: For Cabal 3.0 and later, the
new-prefix is optional—you can usecabal configureinstead.
If you're working with Nix (to leverage its reproducibility and dependency management), there are a few robust ways to tie your project to a Stackage snapshot:
Using haskell.nix
This dedicated toolchain integrates Cabal projects with Nix and Stackage seamlessly:
- Initialize your project with
haskell.nix(follow the project's setup guide for your specific project structure). - Specify your target Stackage snapshot (e.g.,
lts-21.10) in your project's Nix configuration files. - Build or develop your project with Nix commands like
nix-build(to build the project) ornix develop(to enter a reproducible development environment). All dependencies will be pulled directly from the specified Stackage snapshot.
Using cabal2nix + Nixpkgs Stackage Sets
Nixpkgs includes pre-built package sets for every Stackage LTS and Nightly snapshot. Here's how to use them:
- Generate a Nix expression for your Cabal project with
cabal2nix:cabal2nix . > default.nix - Create a
shell.nixfile that imports the Nixpkgs revision matching your target snapshot and ties your project to its package set:let pkgs = import <nixpkgs> { config = {}; overlays = []; }; # Replace with your target snapshot stackageSet = pkgs.haskell.packages.lts-21.10; in stackageSet.callPackage ./default.nix {} - Use
nix-shellto enter a development environment with all snapshot dependencies, ornix-buildto build your project reproducibly.
Minimal nix-shell Environment
For a quick, lightweight setup, create a minimal shell.nix that gives you a GHC environment tied to your Stackage snapshot:
{ pkgs ? import <nixpkgs> {} }: pkgs.haskell.packages.lts-21.10.ghcWithPackages (ps: with ps; [ # Add your project's direct dependencies here, or leave empty for the full snapshot environment base text aeson ])
Run nix-shell to enter the environment—any cabal commands you run here will use the dependency versions from the specified Stackage snapshot.
内容的提问来源于stack exchange,提问作者sjakobi

