You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nix integration is not supported in new-build. #4646
Reacted by Elliot Cameron, Vladislav Zavialov, Spiros Boosalis, Travis Whitaker, Sebastian Graf, Zachary Churchill, Julian Antonielli, Michael Peyton Jones, Attila Gulyas, Emily and 1 more
I intentionally did not implement Nix integration for the new-build command because it was yet somewhat unstable. Nix integration will be of limited utility with new-build, although it will be useful to manage foreign dependencies. Unfortunately, I do not expect I will have time to implement this feature soon.
@ttuegel What are the challenges involved in implementing this? Do you think it's a reasonable goal for someone like me who has never contributed to Cabal before?
In a cabal2nix issue, I briefly outlined a common workflow with Nix+Cabal that I use for single-package projects quite often. The goal (to me) would be to have Nix provide you with the GHC version and all your haskell dependencies, while allowing Cabal to do the incremental development work. The main reason is that Nix builds are much more suited for production than they are for development, while Nix shells are fantastic for development. So cabal new-build --enable-nix would enter a nix-shell to find the GHC it needs, which has a package database with all the necessary packages already present, and it would use that GHC to give you incremental compilation across packages in your project.
Reacted by Spiros Boosalis, Tim Sears, Nick Hamilton, Travis Whitaker, Kavin Ruengprateepsang, ivan-timokhin, Sebastian Graf, Matthieu Coudron, Filippo Ghibellini, Ryan and 1 more
So having all of a project's non-local dependencies installed via nix means translating the project plan into nix exprs and using nix to execute that. It's hardly any more work to also be able to make nix exprs for building the local deps too, which would be super-useful for CI.
BTW, I don't think single-package nix exprs really make sense for us, a project corresponds to a particular set of packages with particular configuration for them all. Or to put it another way, cabal new-build's nix integration / cabal2nix should really be whole-project not single package.
Not quite. The purpose of cabal2nix is to generate a Nix expression that just trusts whatever your nixpkgs.haskellPackages provides as its package set. This workflow does not use Cabal's dependency planning system whatsoever. Nixpkgs acts a bit like Stackage in this regard, and Cabal just becomes a great command line interface for incremental development over that.
Reacted by Spiros Boosalis, Nick Hamilton and Peter Becich
Ok, whereas for cabal new-build, it'd ignore nixpkgs.haskellPackages and do exactly what your cabal.project file specifies (which itself can of course use stackage or any other collection) including the solver/planner.
No I don't think so. The point of using Nix with Haskell is to use Nix to define all your dependencies in a package set. Cabal should be doing no planning / solving, as that work is meant to be done by the nix expressions.
I would expect it to work exactly how it does currently with cabal build. Currently, if my nix-shell provides a GHC with all my Haskell dependencies, I can do cabal configure from within that shell to configure cabal to use the dependencies from that environment, and I can use cabal build to do a local build depending on that nix environment.
Reacted by Tim Sears, Travis Whitaker, Gastón Tonietti, Filippo Ghibellini and Mats Rauhala
Ok, so that's a completely different requirement and it's incompatible with cabal new-build and project files. We cannot use new-build to get nix to install all the deps when nix uses its own choice of configuration of those packages because then there is no guarantee that the configuration matches up with what we need for this project.
The project file (+ solving & elaboration) determines the configuration of all the Haskell dependencies all the way down. It does not just pick versions but all details of the configuration (which it then hashes nix-style). We cannot arbitrarily substitute differently configured deps and expect it to work. The fact that the old build's nix integration code did this is no evidence that such a thing is possible. That approach was not deterministic and was easy to break for exactly these kinds of reasons.
There are three levels of integration I can imagine:
Just use nix to build non-Haskell deps, like ghc and C libs etc, just as we might try to do with RPM or DEB based systems. Ie just treat it like any ordinary dumb system package manager.
Generate nix expr(s) for the cabal project based on the exact config that the cabal new-build code would choose natively. This would work essentially by translating the new-build install plan into a nix expr. This would be a separate mode and would have no other CLI integration. It would be useful for doing CI for cabal projects via nix.
Do 2. above but also include CLI integration so that under the hood the new-* commands use nix to execute the translated plan, rather than executing the plan directly as they do now. This would also allow entering a nix shell with all deps in the right configuration, but it'd also allow just running cabal commands directly and it'd use nix to build non-local deps.
To be totally clear, the point here isn't to use new-build for its Nix style build infrastructure. It's almost exclusively just needed as a means to build multi-package projects against a Nix environment, rather than any environment solved by Cabal. This is consistent with the workflow that is possible today with cabal build, which also uses Cabal exclusively for local incremental compilation and configuration, without any solving.
I also don't believe it should be incompatible. I can actually emulate the desired effect just by manually entering a nix-shell and running the Cabal commands in there myself. new-build is more than happy to use whatever GHC you throw at it, including the packages that come with it. And it will correctly fail when one of those packages is outside the constraints in the cabal files. The point of this issue is merely that --enable-nix should enter that Nix shell itself.
Reacted by Tim Sears, Travis Whitaker and Michael Peyton Jones
If people don't object I'll try to summarize the discussion so far as somebody who has used all three of cabal/stack/Nix extensively:
Nix's Haskell support works similar to Stack, meaning:
Nixpkgs maintains a curated set of packages (analogous to a Stack resolver)
You can override a package within that package set to use a different version from Hackage (analogous to the extra-deps section of a stack.yaml file)
You can also add or override packages from GitHub or use a local source checkout of the package (similar to the packages section of a stack.yaml file)
Users typically prefer not to use Cabal's solver but can if they really want to (similar to Stack)
Nix's Haskell support differs from Stack in the following way:
Stack forces cabal to use its pinned dependencies using a giant cabal.config file (like this one)
Nix forces cabal to use its pinned dependencies by generating a wrapped GHC whose global package database contains those dependencies
Running all cabal-install commands inside of a nix-shell works with V1 commands and appears to work with V2 commands, too
I say "appears" because I haven't used V2 commands extensively in my own Nix+Haskell development, and I've only done some light testing
The V1 commands support a nix: True~/.cabal/config flag which allowed the same commands to work outside of a nix-shell
It sounds like a small thing, but it's extremely convenient for Nix users
It also reduces user error since Cabal's Nix integration will regenerate the nix-shell any time you run cabal configure (either explicitly or implicitly as part of another cabal command that detected a configuration change)
The request here is to support the same nix: True configuration option for the V2 commands
Feel free to correct me if I got anything wrong
Reacted by Elliot Cameron, David Costa, Michael Peyton Jones, Johannes Maier, Andreas Källberg, Michael Hueschen, Tim Spence, BBBSnowball, Clemens Schmid, Cody Goodman and 1 more
One further issue is that cabal v2 commands will always fetch and install packages that aren't in the db into the store, so you can't ensure you're only using the "closed universe" of packages made available into the custom pkgdb that nix builds. There are workarounds that can/should be integrated into nix scripts. In particular, setting a custom cabal_config env variable (as per here: #5322) can be used to prevent cabal from installing additional packages from hackage.
When I enter a nix-shell and then use cabal new-* (v2 commands), build tools required by my .cabal file seem to be built by cabal even if they are available from the nix-shell environment. In particular, any patches applied by Nix when building those build tools have no effect. Using v1 commands does not seem to have the same problem.
@gbaz, new in cabal-install 3.0, reject-unconstrained-dependencies allows you to restrict the solver to a closed set. It'd be a little bit cumbersome to repurpose for this, though - I guess you'd want to generate a cabal.project (or perhaps a freeze file?) with every package in the pkgdb that Nix creates.
One goal in that regard is that Nix and Cabal should be able to hash files in the same way. I think the best thing to do is teach them both git tree hashes. Once we reach the point where hackage builds, patched hackage builds, and local builds are all cached with comparable keys, things will start to move smoothly.
If we are doing a local build, and a Nix expression exists in the same directory as the .cabal file,
then run cabal inside a nix-shell of that expression.
Until this is solved in Cabal proper, you can use a workaround: nix-cabal.
In the shell, you just use nix-cabal foo instead of cabal foo, although you'll suffer the overhead of entering the nix-shell every time.
Another solution is lorri. It runs a background daemon that keeps nix-shell warm, and uses direnv to automatically enter nix-shell whenever you are in the project directory. It is somewhat finicky, though.
I think facts has demonstrated nix integration in v2 build is at best a very low priority enhancement.
This issue has a very interesting discussion on the topic but i would close it if you all dont mind. If anyone still thinks the nix integration should be done please raise a hand.
Last I checked (years ago) Cabal's (and Stack's) Nix integration wasn't really what I wanted as a Nix user anyways, so I don't mind if they whither away and are replaced with something more proper later.
Personally I've defined cabal = nix-shell --run "cabal $@" and this seems to be doing what I need, mostly. I still think it would be nicer to have this, so one can simply add nix: True to cabal/config.
Activity
ElvishJerricco commentedon Jul 31, 2017
It seems
--config-filealso does not work innew-build. Is there common global command line option logic not being used?23Skidoo commentedon Jul 31, 2017
/cc @ttuegel
ttuegel commentedon Jul 31, 2017
I intentionally did not implement Nix integration for the
new-buildcommand because it was yet somewhat unstable. Nix integration will be of limited utility withnew-build, although it will be useful to manage foreign dependencies. Unfortunately, I do not expect I will have time to implement this feature soon.hvr commentedon Jul 31, 2017
Re
--config-file, see #4649ElvishJerricco commentedon Aug 3, 2017
@ttuegel What are the challenges involved in implementing this? Do you think it's a reasonable goal for someone like me who has never contributed to Cabal before?
dcoutts commentedon Aug 3, 2017
What is the goal exactly for nix integration with new-build?
To translate into nix exprs? To build C deps using nix? To do the whole build using the cabal CLI but use nix under the hood to do the builds?
ElvishJerricco commentedon Aug 3, 2017
In a
cabal2nixissue, I briefly outlined a common workflow with Nix+Cabal that I use for single-package projects quite often. The goal (to me) would be to have Nix provide you with the GHC version and all your haskell dependencies, while allowing Cabal to do the incremental development work. The main reason is that Nix builds are much more suited for production than they are for development, while Nix shells are fantastic for development. Socabal new-build --enable-nixwould enter anix-shellto find the GHC it needs, which has a package database with all the necessary packages already present, and it would use that GHC to give you incremental compilation across packages in your project.dcoutts commentedon Aug 3, 2017
So having all of a project's non-local dependencies installed via nix means translating the project plan into nix exprs and using nix to execute that. It's hardly any more work to also be able to make nix exprs for building the local deps too, which would be super-useful for CI.
BTW, I don't think single-package nix exprs really make sense for us, a project corresponds to a particular set of packages with particular configuration for them all. Or to put it another way, cabal new-build's nix integration / cabal2nix should really be whole-project not single package.
ElvishJerricco commentedon Aug 3, 2017
Not quite. The purpose of
cabal2nixis to generate a Nix expression that just trusts whatever yournixpkgs.haskellPackagesprovides as its package set. This workflow does not use Cabal's dependency planning system whatsoever. Nixpkgs acts a bit like Stackage in this regard, and Cabal just becomes a great command line interface for incremental development over that.dcoutts commentedon Aug 3, 2017
Ok, whereas for cabal new-build, it'd ignore
nixpkgs.haskellPackagesand do exactly what your cabal.project file specifies (which itself can of course use stackage or any other collection) including the solver/planner.ElvishJerricco commentedon Aug 3, 2017
No I don't think so. The point of using Nix with Haskell is to use Nix to define all your dependencies in a package set. Cabal should be doing no planning / solving, as that work is meant to be done by the nix expressions.
I would expect it to work exactly how it does currently with
cabal build. Currently, if mynix-shellprovides a GHC with all my Haskell dependencies, I can docabal configurefrom within that shell to configure cabal to use the dependencies from that environment, and I can usecabal buildto do a local build depending on that nix environment.dcoutts commentedon Aug 3, 2017
Ok, so that's a completely different requirement and it's incompatible with cabal new-build and project files. We cannot use new-build to get nix to install all the deps when nix uses its own choice of configuration of those packages because then there is no guarantee that the configuration matches up with what we need for this project.
The project file (+ solving & elaboration) determines the configuration of all the Haskell dependencies all the way down. It does not just pick versions but all details of the configuration (which it then hashes nix-style). We cannot arbitrarily substitute differently configured deps and expect it to work. The fact that the old build's nix integration code did this is no evidence that such a thing is possible. That approach was not deterministic and was easy to break for exactly these kinds of reasons.
dcoutts commentedon Aug 3, 2017
There are three levels of integration I can imagine:
Just use nix to build non-Haskell deps, like ghc and C libs etc, just as we might try to do with RPM or DEB based systems. Ie just treat it like any ordinary dumb system package manager.
Generate nix expr(s) for the cabal project based on the exact config that the cabal new-build code would choose natively. This would work essentially by translating the new-build install plan into a nix expr. This would be a separate mode and would have no other CLI integration. It would be useful for doing CI for cabal projects via nix.
Do 2. above but also include CLI integration so that under the hood the new-* commands use nix to execute the translated plan, rather than executing the plan directly as they do now. This would also allow entering a nix shell with all deps in the right configuration, but it'd also allow just running cabal commands directly and it'd use nix to build non-local deps.
ElvishJerricco commentedon Aug 3, 2017
To be totally clear, the point here isn't to use
new-buildfor its Nix style build infrastructure. It's almost exclusively just needed as a means to build multi-package projects against a Nix environment, rather than any environment solved by Cabal. This is consistent with the workflow that is possible today withcabal build, which also uses Cabal exclusively for local incremental compilation and configuration, without any solving.I also don't believe it should be incompatible. I can actually emulate the desired effect just by manually entering a nix-shell and running the Cabal commands in there myself.
new-buildis more than happy to use whatever GHC you throw at it, including the packages that come with it. And it will correctly fail when one of those packages is outside the constraints in the cabal files. The point of this issue is merely that--enable-nixshould enter that Nix shell itself.36 remaining items
Gabriella439 commentedon Sep 5, 2019
If people don't object I'll try to summarize the discussion so far as somebody who has used all three of
cabal/stack/Nix extensively:extra-depssection of astack.yamlfile)packagessection of astack.yamlfile)cabalto use its pinned dependencies using a giantcabal.configfile (like this one)cabalto use its pinned dependencies by generating a wrapped GHC whose global package database contains those dependenciescabal-installcommands inside of anix-shellworks with V1 commands and appears to work with V2 commands, toonix: True~/.cabal/configflag which allowed the same commands to work outside of anix-shellnix-shellany time you runcabal configure(either explicitly or implicitly as part of anothercabalcommand that detected a configuration change)nix: Trueconfiguration option for the V2 commandsFeel free to correct me if I got anything wrong
gbaz commentedon Sep 5, 2019
One further issue is that cabal v2 commands will always fetch and install packages that aren't in the db into the store, so you can't ensure you're only using the "closed universe" of packages made available into the custom pkgdb that nix builds. There are workarounds that can/should be integrated into nix scripts. In particular, setting a custom cabal_config env variable (as per here: #5322) can be used to prevent cabal from installing additional packages from hackage.
j6carey commentedon Sep 5, 2019
Following on the comments of @Gabriel439 and @gbaz :
When I enter a
nix-shelland then usecabal new-*(v2 commands), build tools required by my.cabalfile seem to be built bycabaleven if they are available from thenix-shellenvironment. In particular, any patches applied by Nix when building those build tools have no effect. Using v1 commands does not seem to have the same problem.quasicomputational commentedon Sep 5, 2019
@gbaz, new in cabal-install 3.0,
reject-unconstrained-dependenciesallows you to restrict the solver to a closed set. It'd be a little bit cumbersome to repurpose for this, though - I guess you'd want to generate acabal.project(or perhaps a freeze file?) with every package in the pkgdb that Nix creates.Ericson2314 commentedon Sep 6, 2019
IMO https://github.com/input-output-hk/haskell.nix is the future, and a much better friend of Cabal and stack in particular, and any future work on nix-cabal integration should focus on that.
One goal in that regard is that Nix and Cabal should be able to hash files in the same way. I think the best thing to do is teach them both git tree hashes. Once we reach the point where hackage builds, patched hackage builds, and local builds are all cached with comparable keys, things will start to move smoothly.
neongreen commentedon Dec 3, 2019
Until this is solved in Cabal proper, you can use a workaround: nix-cabal.
Installation:
Then whenever I want a project to use nix-cabal, I create a
.dir-locals.elfile in the project root and Emacs picks nix-cabal up:In the shell, you just use
nix-cabal fooinstead ofcabal foo, although you'll suffer the overhead of entering thenix-shellevery time.Another solution is lorri. It runs a background daemon that keeps nix-shell warm, and uses direnv to automatically enter nix-shell whenever you are in the project directory. It is somewhat finicky, though.
jneira commentedon Mar 23, 2022
I think facts has demonstrated nix integration in v2 build is at best a very low priority enhancement.
This issue has a very interesting discussion on the topic but i would close it if you all dont mind. If anyone still thinks the nix integration should be done please raise a hand.
Ericson2314 commentedon Mar 23, 2022
Last I checked (years ago) Cabal's (and Stack's) Nix integration wasn't really what I wanted as a Nix user anyways, so I don't mind if they whither away and are replaced with something more proper later.
rnhmjoj commentedon Mar 23, 2022
Personally I've defined cabal =
nix-shell --run "cabal $@"and this seems to be doing what I need, mostly. I still think it would be nicer to have this, so one can simply addnix: Truetocabal/config.Ericson2314 commentedon Mar 24, 2022
Mmm while that is better than nothing, it is so far from adequate Nix support it feels like false advertising to call that
nix: Trueto me.