by chriswarbo · 4y ago · 0 comments
If you prefer imperative programming, then think of it being used like this:
const result = defaultNixpkgsSet;
for (overlay in overlays) {
// Merge the overlay's return value into the results value
result := result.merge(overlay(
result, // Give each overlay a reference to result
result.copy() // Also give a snapshot of the contents so-far
))
}
return result;
Consider an overlay something like this: function(result, snapshot) {
return {
maven = snapshot.maven.override({ jre = result.jre8; })
};
}
Merging this into the 'result' will replace the 'maven' definition. That new definition is the same as the old one ('snapshot.maven'), but it will be using 'jre8' instead of the default. Two things to note:- We're using 'result.jre8', which will include any overrides made to 'jre8' (by other overlays, even those being applied after this one!)
- We're using 'snapshot.maven', since using 'result.maven' would cause an infinite loop (since 'result.maven' depends on the output of this override!)
In that link, they're calling their arguments 'final' and 'prev' instead of 'result' and 'snapshot', but the idea's the same. Most overlays call their arguments 'self' and 'super', but I avoided them in this explanation since those names have specific meanings in other languages (they're literally just variable names in Nix).
In a way the clearer dependency semantics of flakes are are driving the reason they are increasing in adoption so much. In essence they work around the problems introduced by overly clever usage of laziness in a nix package.
I don't consider it an awkward hack; it's a very standard least-fixed-point calculation. Developers these days seem comfortable-enough with 'self' and 'super' in OOP; that fixed-point pattern is essentially the same, but without shoe-horning magic keywords into the language.
.. so the sharedOverlays list has one element, which has our in it, which will have a .lib attribute pointing to lib defined above?