hix icon indicating copy to clipboard operation
hix copied to clipboard

How to add Cabal build-tools?

Open bristermitten opened this issue 3 months ago • 5 comments

I'm having some trouble adding alex as a build-tool, I've tried doing haskellTools = ghc : [ghc.alex]; but I still get

      > Error: [Cabal-4123]
      > The program 'alex' is required but it could not be found

because it's not added as a build-tool. I've also tried adding it to component.build-tool-depends to no avail:

     build-tool-depends = [
              "alex"
            ];

This error only happens when running with nix run, interestingly. cabal run does seem to work fine

bristermitten avatar Sep 01 '25 14:09 bristermitten

I think the easiest way to make a tool available to a derivation should be

{
  packages.<name>.buildInputs = pkgs: [pkgs.alex];
}

However, this will not provide the package built with the same ghc as your package or environment uses, so if that's important, you'd have to do something like:

{
  packages.<name>.override = {buildInputs, self, ...}: buildInputs [self.alex];
}

This is definitely a bit too cumbersome, I should introduce a proper option for build-tool-depends.

FYI, this doesn't work with nix run despite working with cabal run because the latter looks for the tool itself, and – if you're editing your cabal config manually – because the derivation generates a fresh cabal file from the flake config, ignoring what's there (unless that feature is disabled with manualCabal = true;)

tek avatar Sep 01 '25 16:09 tek

Thanks for your response! The buildInputs seems to have worked :)

The weird thing is that I'm not editing my cabal config manually. I wrote my entire config in Nix, then ran nix run .#gen-cabal, and the generated cabal file works with cabal run, but nix run doesn't.

bristermitten avatar Sep 01 '25 16:09 bristermitten

Thanks for your response! The buildInputs seems to have worked :)

Glad to hear it ☺️

The weird thing is that I'm not editing my cabal config manually. I wrote my entire config in Nix, then ran nix run .#gen-cabal, and the generated cabal file works with cabal run, but nix run doesn't.

I see...well, I assume Cabal is exposing alex in $PATH then, or maybe it's available in the global $PATH. The derivation is way more hermetic in any case. In particular, if you've set haskellTools and entered a nix develop shell, the tool is visible to cabal run.

tek avatar Sep 01 '25 16:09 tek

I don't believe I had alex on the global $PATH, so yeah, Cabal is handling it.

To be clear, nix run, even inside a devshell, requires the buildInputs to be set or it fails with the error I mentioned initially. nix run .#gen-cabal and then cabal run works fine with just the component.build-tools set (and nothing else). The haskellTools being set or not doesn't seem to make a difference.

bristermitten avatar Sep 01 '25 17:09 bristermitten

right, then it's gotta be Cabal

tek avatar Sep 01 '25 17:09 tek