Teaser: pluggable cabal backends and a Nix backend

As is the trend,@Ericson2314 and I have been doing some LLM-assisted prototyping of Haskell devX improvements. We changed Cabal (cabal-install) into a frontend for build systems, making the original behavior just one of many backend options. If all goes well, Cabal’s commands will just keep working as normal with any backend.

@simonmar’s Buck2 build system could become one of these (instead of an external command), and we’ve been implementing a Nix-backend as well. Incremental Nix builds for GHC had already been implemented in sandstone, so it was relatively easy to create this backend.

Here’s a little demo showing the behavior of the “classic” backend vs. the Nix (cabal-sandstone) backend:

demo

The code for the backend is available for anyone caring to look: GitHub - parenthetical/cabal-sandstone: A Nix backend for Cabal · GitHub . The repository contains Git submodules linking to our Cabal fork (and many other forks :sweat_smile:).

Although the cabal-sandstone backend shares many advantages with the cabal-buck2 command, it loses out on absolute performance.

However, in return you are freed from worrying about how to get GHC and foreign library dependencies. Getting all your build tools and libraries just happens magically, as long as they are correctly declared in your project and your project’s dependencies’ cabal files.

You also get a centralized persistent cache for (intermediate) build artifacts through the Nix store (I always end up with gigabytes of dist-newstyle spread over my worktrees.)

For users who have tried building Haskell with Nix before, here are some differences from prior attempts to be aware of:

  • No slow eval times. cabal-sandstone in fact doesn’t use the Nix language at all, and certainly no “import from derivation”. All build plan are expressed with low-level “nix store layer” primitives, most notably dynamic derivations.
  • No coarse caching. Nix solutions traditionally built entire packages in one step, because they viewed Cabal as a complex black box. But, since we’re sliding and dicing Cabal as much as we need to, we don’t have those problems. Also, thanks to the the power of dynamic derivations we do not need to know exact dependencies in advance and can dynamically figure out both package-package dependencies and module-module dependencies. We even support cross-package module-module dependencies!

We know our myriad Cabal changes are not going to go upstream in one giant PR. We have a plan to upstream the work “bottom-up”, first addressing some foundational longstanding architectural issues in Cabal, and then decoupling Cabal’s “backend” using that foundation. See some of @ericson2314’s open/draft Cabal PRs for a sneak preview of what that first step entails.

Happy to answer any questions :slightly_smiling_face:

17 Likes

I’m interested in understanding the architecture here, and figuring out how we could converge. I poked around a little but it wasn’t obvious to me how I should understand it - what exactly is the interface to the backend?

I’ve been thinking about the interface to the backend as an expanded plan.json, as you might have seen from all the Cabal PRs and RFC: Buck2 backend · Issue #12456 · haskell/cabal · GitHub . Does your approach differ in any fundamental way or are you just passing the same information as Haskell datatypes to the backend? Is the API documented somewhere?

Is the UI always just cabal build? In the Buck2 world we like to run cabal once and then use buck2 as the UI. I suppose we could make it possible to do either.

2 Likes

This sounds like an excellent candidate for a cabal-proposal. I think everyone would benefit from a specification for the change you are trying to accomplish, especially when the diffs are understandably so large

2 Likes

First of all, we’ve chosen to publish this “open notes”, in that cabal-sandstone/PLAN.org at main · parenthetical/cabal-sandstone · GitHub and its full history is here. If you point your LLM at that and ask whatever questions you feel like, you can probably figure out a lot. Even figure out things that we don’t know ;).

@aidylns worked on the pluggable backends itself, and so would be better able to answer that than me. But I can answer some related questions.

I do think we want to be able to work from an exported stable plan format eventually, but my view is that current plan.json isn’t really the right starting point. It is too much an artifact of the design of the current[1] solver. In Per component plan, elaboration in two steps by Ericson2314 · Pull Request #12679 · haskell/cabal · GitHub (stacked atop other PRs), I tried to rework what in Cabal is known as “elaboration” (solver output → concrete build steps) so that it proceeds in two steps:

  1. Turn package-level plan (what cabal-install-solver spits out today) into a component-level plan (except when custom setups[2] etc. prohibit this).

  2. Finish elaboration which figures out concrete CLI flags, concrete file paths including dist-newstyle, etc. etc.

Part 2 is blatantly backend-specific. Part 1, however, is nicely backend-agnostic while still discarding irrelevant distinctions. For example, post solving, there is no need to care which component comes from which package. That distinction only matters for solving. In terms of how we invoke GHC to actually build things we just don’t care:

  • every library, whether it’s top-level or a “sublibrary”, is built the same way
  • test suites and benchmarks are built just like regular executables. (The distinction is just how Cabal wants to run them.) All are “leaf” builds that nothing depends on, and don’t end up in any package database.

So it is the “core language” that sits between steps 1 and 2 that I think is what backends actually want.

Ironically while I think this is a great fit for cabal-buck2 right away, currently cabal-sandstone still ends up using the result of step 2 because we want to crib the concrete flags Cabal chose too (rather than reimplement that logic ourselves). I am next going to try to expose the “lowering to CLI flags” functionality used in step 2 separately, as functions that a backend can use or not use as it pleases. That will be better than today when such a backend has to opt into the entire phase 2 which is blatantly “this local machine” and “this dist-newstyle directory” specific, and thus not suitable for backends that do distributed builds and don’t store their build products in dist-newstyle.

No, but most of what I said above is written — somewhere — in that PLAN.org I mentioned. The PRs that Per component plan, elaboration in two steps by Ericson2314 · Pull Request #12679 · haskell/cabal · GitHub builds on I have actually reviewed somewhat, but that one itself is merely the result of me describing the problem and solution as per above, and the LLM doing…something. I would say the concrete implementation is not fit for other human consumption until I went and reviewed it first :).

So @aidylns has been dogfooding this as a cabal <anything> replacement. He just sets the backend environment variable, using the right cabal, and everything “just works” as if he is using stock cabal, without having to think about it.
It is true that only building is farmed off to Nix, strictly speaking, but that doesn’t mean that is the only interaction.

For example, consider cabal repl. The backend (Nix, Buck2, whatever) will be instructed to just build the dependencies. That’s fine; that’s just pruning the build plan a bit so the backend doesn’t “overbuild”. However, then cabal wants to run ghc --interactive with the right arguments. So the backend needs to be instructed to make sure certain build products are locally available, and also get their locations back to cabal for it to invoke. (I think currently we might just symlink some things in dist-newstyle and then run the normal logic for starting the repl. This works, but is rather hamfisted, and not really in the spirit of relegating dist-newstyle and all knowledge about it to just the built-in backend.)

All that said, while I think the above is a great workflow for developers, and users wholly uninterested in the details of Nix, Buck2, etc. :), I totally agree that for more “production build” use-cases, it is better to extract a build plan once, and then make sure nothing is needed from the “original” cabal process ever again. For this workflow check out the last section of the read-me, GitHub - parenthetical/cabal-sandstone: A Nix backend for Cabal · GitHub. Not only is the cabal process ephemeral, and doesn’t need to stick around for the duration of the build, but the invocation of cabal itself, and the creation of all plans and anything else that is needed, is fully driven by Nix. Think of this as Nix/Buck2 both working strictly from a plan.json (once the above tech debt is sorted out), and running the commands that create the plan.json in the first place.

(For context to others — I imagine @simonmar is already thinking along these lines — in whole-system production builds, Cabal is not going to be the main “entry point”. For example, when building a NixOS configuration — even if the purpose of the image is just to run a server written in Haskell — the Haskell program and its dependencies are going to be just one executable among many. (The build system doesn’t know which executables are the “main event” and which are a “side show”, e.g. just there for system administrators SSHing to the production system when something goes wrong.) It’s very convenient that all cabal interactions can just be another node in the build graph in this scenario so we aren’t forced to commit generated files like plan.json if we don’t want to, because the actual top-level build step is going to be some mundane configuration assembly shell script, and not anything Haskell-related.)

The bottom line is this tool supports both “I don’t care what Nix is, just make it go brrrr” and “I love Nix, hide the Cabal from me” workflows :).


  1. The solver today currently just thinks about packages and versions, with global coherence. Interestingly enough there are issues about component-level solving (Per-component dependency solving · Issue #4087 · haskell/cabal · GitHub) and private dependencies support (Private dependencies · Issue #4035 · haskell/cabal · GitHub). Component-level solving would allow the solver to understand dependencies on a finer-grained level, which will avoid some unnecessary coherence conditions we impose today. Private dependencies force the solver to also consider more hierarchical structure than original “flat” name → version assignments of yore (though currently it is already more sophisticated than that, to be clear).

    My two-phase elaboration and new core language anticipate these future extensions, but don’t force them. Today’s solver continues working, and any new solver can just skip phase 1 and output component-level plans directly. ↩︎

  2. Custom setups are extremely pathological, because they currently force the entire package to be built in a black-box setup-script-controlled arbitrary way, with no finer-grained caching possible. As many people have expressed in other threads already, we should be moving away from them. The Hooks method will soon not have many of these deficiencies, but being able to drop down and write “inline Starlark” (for Buck2) or “inline derivations” (for Nix), analogous to “inline assembly” where one just splices in some code in the backend’s native language directly, is also a good option. ↩︎

1 Like

I am happy to write things (looks up at my marathon reply above), but a proposal feels a bit too “waterfall” for where we are. We are not in a “the backend authors know exactly what they need from Cabal, now the Cabal devs just need to do it!” place :). At most, I’d feel comfortable writing a meta-level proposal: “this is an important direction for the ecosystem, and we should commit resources to it”.

Instead, I think the process is sitting down with the Cabal developers, e.g. in the periodic Cabal development video call, and gradually aligning mental models while working on intermediate steps. I still need to understand all of the diff myself, per the above, but I know it is built atop smaller “tech debt” PRs that I do understand, and believe are good (and would be good even without pluggable backends). Those make good warm-up tasks for aligning everyone’s mental models: small, self-contained, and understandable purely in terms of existing code and use cases. Landing them shrinks the diff right away, while @aidylns and I concurrently rework the rest into reviewable PRs to be discussed in future meetings.

I think the above is a pretty good process that starts shrinking the diffs immediately. But the final state isn’t knowable in advance (by design! it’s supposed to emerge from those meetings organically), so there isn’t much a technical proposal could pin down with confidence today.

3 Likes