Document how to use crane inside of an overlay
Is your feature request related to a problem? Please describe.
I have a project written in Rust for which I provide an overlay to make it easy to use via, e.g., Home Manager. I'm not confident that I wrote the overlay correctly; currently I use crane.mkLib final, but I can't tell if I should actually be doing crane.mkLib prev instead, or if actually neither are correct and I should do something more subtle.
Describe the solution you'd like
It would be great if the crane docs could describe whether, when defining a Nixpkgs overlay that provides a package built with crane, one should use crane.mkLib final, or crane.mkLib prev, or do something else entirely.
Describe alternatives you've considered I can't think of any alternatives.
craneLib is a library, so it is not something you'd add to nixpks overlay. AFAICT you are adding a software built with crane to an overlay which is done exactly like adding any other software to an overlay.
@dpc OK, so... sorry, I'm still somewhat new to Nix. Which should I use, crane.mkLib final or crane.mkLib prev? Or do you mean I should be doing neither of those, in which case, what should I be doing instead?
@dpc OK, so... sorry, I'm still somewhat new to Nix. Which should I use,
crane.mkLib finalorcrane.mkLib prev? Or do you mean I should be doing neither of those, in which case, what should I be doing instead?
Yeah, we've all been there, don't worry. :D
Probably final, unless you have some fundamental issues causing circular dependencies. final are the "final" packages (typically named pkgs) after all overlays are composed. So you want to use the final state as much as you can.
@dpc gotcha, thanks; I think I understand that in general. Could you help me understand this specific case in the context of the general advice given in the Nixpkgs manual? (emphasis mine)
The first argument (
final,self) corresponds to the final package set. You should use this set for the dependencies of all packages specified in your overlay. For example, all the dependencies ofrrin the example above come fromfinal, as well as the overridden dependencies used in theboostoverride.The second argument (
prev,super) corresponds to the result of the evaluation of the previous stages of Nixpkgs. It does not contain any of the packages added by the current overlay, nor any of the following overlays. This set should be used either to refer to packages you wish to override, or to access functions defined in Nixpkgs. For example, the original recipe ofboostin the above example, comes fromprev, as well as thecallPackagefunction.
Given that crane.mkLib uses lib from Nixpkgs, this made me think that prev should actually be used instead of final in this case:
https://github.com/ipetkov/crane/blob/d9e753122e51cee64eb8d2dddfe11148f339f5a2/lib/default.nix#L2
Is there some particular reason why that advice doesn't apply here?
Honestly, I'm not 100% sure which one to use here. The problem with final/prev in overlays is that you might accidentally trigger a circular recursion, even between different overlays, as they could be overriding stuff in final that the other side uses for evaluating the override itself. If you're bringing in an extral crate like crane to build some package, I think the chances of that are minimal if not possible at all, so I think final will be fine. If you use prev it will work just as well, just crane will use set of packages that are pre this and other overlays itself.
I second everything that has been said earlier. There's nothing crane specific about using overlays, any general documentation around them (and any advice when to use final vs prev) would similarly apply here.
Though happy to review any PRs with additional documentation improvements around this!
@ipetkov thanks, makes sense; so just to clarify, are you saying that similar to @dpc, your stance is also that it's not 100% clear which to use between final and prev? Do you ever write an overlay like the one I linked above, and if so, do you use final or do you use prev? Or if you don't ever write such an overlay, is there a reason why not? Is there a different thing that I should be doing instead?
your stance is also that it's not 100% clear which to use between
finalandprev?
No. My stance is that is is clear which one to use, but you have to take into account:
- what your package depends on
- where you expect your package to be consumed downstream (i.e. by callers importing your overlay)
- whether there are any (evaluation) cycles between the above
If there are no cycles, use final for maximum flexibility/least surprise for further downsteam overlays. Otherwise if there are cycles (you get infinite recursion errors) or you aren't sure if there may be, use prev.
IME for the majority of overlays which define their own packages (i.e. not in nixpkgs, and not replacing anything in nixpkgs) the distinction doesn't really matter so pick whichever works for your use case!