Cabal - add build dependency with Cabal instead of manually mangling with the file
Asked Answered
P

2

8

Do I need to manually edit the *.cabal file's build-depends section to add package as a project dependency? Or perhaps there is a more convenient way that is not as error prone as manually mangling with build files is.

Thinking about functionality that pretty much any package manager I used has, namely

apt install

npm i

nuget install

Install Package

and so on. Does such functionality exist in Cabal?

Psychrometer answered 14/7, 2021 at 10:53 Comment(5)
Well, apt install etc. aren't really comparable. It used to be possible to cabal install a package to the “global project” and then it would be available in any Haskell file without an associated cabal project... but eventually people agreed that this was unreliable, and that explicitly adding dependencies to the .cabal file is actually the better way. That said, I agree that it would be nice to have a command-line utility for the actual editing of the file, including lookup of a suitable version range.Dree
That functionality is not supported in cabal-install itself. There's a helper executable called cabal-edit that seems to do what you want. hackage.haskell.org/package/cabal-edit You can install it with the command cabal install cabal-edit --overwrite-policy=always --install-method=copy --installdir=some_destination_folder.Mozzetta
When you type apt install whatever-fancy-program and apt tells you it's also going to install gtk, how do you suppose it knows that gtk is a dependency? Answer: somebody manually mangled a file to record that information. (And if this is not the functionality that you refer to when you say "such functionality", then can you be more specific about what you want? You may think it's obvious, but to me it's not -- I'm making a lot of guesses here.)Montes
@DanielWagner You are answering a question that I did not ask. Too big gun. @dandiaz gave me a good pointer that is still something but nowhere near to succinctness of npm i See, what I am looking for is just a way to use package manager to add build dependencies in the cabal file. Just that. Currently I need to do that manually which is surprising and I guess more experienced Haskell programmers do have a better way to manage build dependencies other that typing them manually. At least I hope this is the case.Psychrometer
I am pretty sure most experienced Haskell programmers just write their dependencies in their cabal files. Usually the time spent adding dependencies is negligible compared to the total time spent on a project. I am not very familiar with npm but maybe it is also a difference of philosophy because packages on npm are smaller compared to packages of other languages? Then time spent on modifying metadata might start becoming significant.Orchidectomy
P
4

There is no better way at the moment. The answer @danidiaz gave is essentially correct -- cabal-edit will automatically update cabal files for you. The plan is to import similar functionality into cabal directly. This was remains blocked on an exactprinter that can parse and emit cabal files precisely -- and work on that exactprinter is now underway.

Prearrange answered 12/8, 2021 at 4:29 Comment(0)
P
1

There is a subcommand cabal gen-bounds which is a bit more convenient but not such enough.

Just declare your dependencies without version tag in .cabal file. And then run cabal gen-bounds and manually copy the output into the .cabal.

Perlie answered 20/3, 2023 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.