Skip to content

RFC: Buck2 backend #12456

Open
Open

Description

@simonmar

(title by analogy with #9089, and this replaces an earlier RFC PR #12410)

Motivation

Cabal is a limited build system

A few of the problems I run into:

  • Support for custom build steps and code-generation is limited. The new hooks mechanism provides some rudimentary support, but it's not enough for a complex project like Glean for example, which has multiple preprocessors and code generators. We currently use a Makefile for development and include all the generated code as extra-source-files to make an sdist for uploading to Hackage. Developing this way is tedious.
  • C/C++ files' dependencies aren't tracked (C source files are not recompiled when headers change #4306). This makes it really awkward to use Cabal for development where you're modifying C/C++ sources.
  • C/C++ files aren't built in parallel (Should Cabal build foreign source files in parallel as well? #7127)

These aren't problems we should be fixing in Cabal IMO; Cabal should be the front-end to a fully-featured build system.

Cabal is slow for development

Mainly because every time you run cabal build it has to redo a bunch of work and run around and figure out what the state of everything is. I know there's cabal repl but that's a whole other story - it's even more limited as a build system (I suspect the right thing is for cabal repl to run alongside a caching build system).

The Cabal store is a poor man's build cache

The Cabal store is a special-purpose build cache that only caches complete Cabal package builds and never deletes them. The store doesn't cache anything to do with the package you're building, only its dependencies.

We should outsource this job to a general-purpose build cache, which would support caching on a smaller granularity (individual build steps) and also allow for tuning the cache size rather than growing without bound. Buck2 can do this with a minimal amount of setup.

CI on github does a lot of repeated work and it's slow. There's support for caching in github workflows but it's painful to use and error-prone. If we instead had a shared build cache (storing blobs on Amazon S3 for example) everyone would get faster CI for minimal effort.

Demo

I've made a cabal buck2 external command: cabal-buck2 which allows you to build a Cabal project using Buck2. It has two modes:

  • By default, all the external dependencies of the project are built by Cabal into the store, just like cabal build all --only-dependencies would, and cabal buck2 then generates Buck2 rules to build the packages in your project against the store libraries.
  • With the --source-deps option, the entire dependency tree (except for GHC's built-in packages) is built by Buck2, bypassing the Cabal store entirely.

It also supports using a build cache, of which there are several open-source implementations. Building with --source-deps and a build cache is just like using the Cabal store (except much faster), and doesn't need any special support in Cabal or GHC. Everything to do with the store in Cabal is redundant, essentially.

Support is pretty complete: cabal buck2 can build the whole transitive dependency tree of cabal-install itself and a bunch of other things I've tried. It supports most of Cabal's features, including hsc2hs, cmm-sources, asm-sources, and so on. The biggest roadblock currently is build-type: Custom packages; these can't be built by cabal buck2 and prevent you from using --source-deps for some packages. Fortunately this is a legacy feature though.

Design questions

This is all well and good: we can have a nice development experience locally, and we can use Buck2 in CI on github. Setting up a local build cache is straightforward and we could have a shared build cache for github CI.

However

  • It's still a bit of a pain to set things up; there are things we could do to make it smoother:
    • ghcup support for installing Buck2
    • some automation around setting up a build cache
    • Having cabal buck2 be a built-in rather than an external command
  • But I still can't use Buck2 to write custom build code in packages I upload to Hackage. For that we would need to support build-type: Buck2, and commit to supporting that.

Thoughts? How does any of this interact or conflict with other plans and directions?

Activity

  1. added
    type: RFC
    on Oct 14, 2026
  2. philderbeast commented on Oct 14, 2026

    @philderbeast
    Collaborator

    Fortunately this is a legacy feature though.

    Pier is fast but can't build custom built type packages either.

    Pier is already able to build most the packages in Stackage (specifically, 93% of the more than 2300 packages in lts-12.8). There is a list of open issues to increase Pier's coverage. (Notably, packages with Custom Setup.hs scripts are not supported.)

    I expect a lot of people would value faster build times over custom setups.

  3. philderbeast commented on Oct 14, 2026

    @philderbeast
    Collaborator

    @simonmar, this is significant enough to be a cabal proposal.

  4. zlonast commented on Oct 14, 2026

    @zlonast
    Collaborator

    Maybe we'd like it to be done over a cabal-install api? haskell/cabal-proposals#8

  5. dcoutts commented on Oct 23, 2026

    @dcoutts
    Contributor

    Support for custom build steps and code-generation is limited. The new hooks mechanism provides some rudimentary support, but it's not enough for a complex project like Glean for example, which has multiple preprocessors and code generators. We currently use a Makefile for development and include all the generated code as extra-source-files to make an sdist for uploading to Hackage. Developing this way is tedious.

    Ok, of course this could be made better. Multiple pre-processors and code generators should be possible. Would need concrete tickets.

    But as a general matter, I don't see how a backend would change anything here. Your problem here is the frontend: the inability to express the rules/dependencies that you want. That's very much a Cabal issue, not a backend issue.

    Unless I've misunderstood what you mean by a backend, and what you want to do is allow individual packages to say that their own build system is make/buck2/ninja/whatever, and then you can express the rules within that package using that specific build system.

    But then that is the opposite of our direction of travel. We are moving towards eliminating custom build types and allowing all packages to be built with a single build system (which could be ninja/buck2/make/whatever).

    The issues to do with C/C++ deps are indeed down to the fact that we've never seriously tried to make Cabal a proper build system. It has always taken the shortcut of using ghc --make, rather than doing things properly.

    Indeed this has ossified to the point where it's not possible (afaik) to implement a proper build system for Haskell/GHC (at least without violating various abstraction boundaries) because ghc -M does not provide sufficient information for a (correct) build system to manage all the required dependencies. I beleive ghc -M improvements are in the works.

    About having backends in general. This should become possible once we retire build-type: Custom, which is the goal of the new hooks scheme. I'm not sure why buck2 would be a great choice though. It's apparently insane to install (I hear they don't do releases and it's not packaged for any distro).

    About buck2, I'm also confused. Last time I asked (iirc at ZuriHac this year), it still does not have support for dynamic/monadic dependencies. That is, looking at a .c or .hs file and discovering what the dependencies are (which imho is a fundamental requirement for a build system). I understood that it relies on pre-processors to (re)scan everything and (re)generate the dependencies. So Cabal would still be involved here, and still need to know everything so that it can produce the main rules and dependencies.

    There's a plausible argument to use Cabal as a frontend to generate ninga build descriptions and use ninja in the backend.

    My own opinion is that we should move Cabal from using ghc --make to using a suitably improved ghc -M and to do "proper" builds using a build graph. That could be using shake, or ninja or something else.

  6. zlonast commented on Oct 25, 2026

    @zlonast
    Collaborator

    I’ve added a mention in haskell/cabal-proposals#8

    I’ve also provided a patch that builds upon the new API: https://github.com/zlonast/cabal-api-examples

  7. simonmar commented on Oct 25, 2026

    @simonmar
    MemberAuthor

    Buck2 indeed doesn't have releases - well, it has bi-weekly "releases" that produce installable binaries, so I believe people just pin their dependency to one of those. It's not ideal I grant you.

    Dynamic dependencies are supported within a rule, but the shape of the rule graph has to be static. It's a compromise designed to allow tools to inspect the build graph while still allowing the fine-grained dependencies within a rule's execution to vary dynamically. It's enough to support ghc -M and module-level parallelism.

    At a high level I think what I'm advocating is, in order of importance:

    1. Cabal should have good support for delegating building to other build systems. A lot of this can be shared between backends: the cabal-buck2 implementation generates an intermediate build spec that should be a suitable starting point for generating the build instructions for any backend. I'd like to see this be a first-class feature: the improvement that you get from using Buck2 is substantial, so I think it's worth the effort to streamline this path for users.
    2. Meanwhile it's OK for Cabal to be a fallback build system, particularly for the "just build it" scenarios rather than incremental development. I'm convinced by the argument that we don't want to require more dependencies, especially for distro packagers.
    3. Separately there's the issue of Cabal not being able to express general build-system functionality, like arbitrary preprocessors or code generators. Even if Cabal adds more stuff to support these kinds of things, I'm doubtful that I want to use Cabal hooks. I don't have a good solution here that doesn't require extra dependencies. If we could ignore the issues of releases and dependencies I would love to have Build-type: Buck2 for complex packages, but perhaps that's not workable as things stand.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions