Repository navigation
Description
Problem
I often see maintainers and committers alike suggesting to use the let in syntax following the practices suggested by https://nix.dev/guides/best-practices.html. However, this is hardly uniform some would suggest using rec, and it's hard to get a solid grasp on what to use in each situation. This was only exemplified when finalAttrs started popping up more commonly as of recent.
Proposal
From my understanding https://nix.dev is an official source so perhaps it would be best to get everyone on board and aware of this best practice? Or maybe to get a more concrete set of best practices to use in each given situation. Or at the very least make members aware that the let in syntax is said to be the best practice, as stated in https://nix.dev/guides/best-practices.html.
Checklist
- checked latest Nixpkgs manual (source) and latest NixOS manual (source)
- checked open documentation issues for possible duplicates
- checked open documentation pull requests for possible solutions
Add a 👍 reaction to issues you find important.
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity
Atemu commentedon May 30, 2024
At the very least I'd like reviewers to stop nitpicking between these.
Unless there is explicit community-wide consensus (i.e. RFC), wide-spread historical precedent (i.e. mkDerivation attr order) or significant concrete problems with the code (i.e. correctness, readability), people should be free to use whichever code patterns or organisation they like.
And by significant, I really do mean significant. Code being organised against your personal preference or patterns you dislike being used are minor differences in taste, not a significant readability problems.
Nitpicking such "issues" is not in any way productive and utterly confusing/unwelcoming to newcomers. They are faced with dozens such implicit rules that aren't explicitly written down anywhere and, even worse, differ wildly depending on which person reviews the code. I don't doubt we've lost more than a dozen contributors whose first PR was faced with 12 nitpicks and 0 actually substantial review comments and then left to bitrot for months.
I've been guilty of this aswell and we all just need to stop doing it.
pluiedev commentedon May 30, 2024
I was (and still am, I guess) one of those nitpickers and I think what really needs to happen here is to standardize the
finalAttrspattern so that it's not only applicable tomkDerivations. Nowadays I don't even nitpickrecs appearing in other builders and change them to let-ins, but when I didn't change that in one of my own PRs I got nitpicked for that 😅It almost feels pedantic, in a way, with the rule being more of a dogma, and the rationale behind it not being at all clear to new contributors and the applicability of it still very limited. We should really try to explain things more, come to a common standard, and make that standard applicable to everything.
Perhaps a table could serve as a brief explanation as to why one pattern may be preferred over the other?
recoverrideslet inoverridesletportion, and used in theinportionfinalAttrsoverridesfinalAttrsor inherited fromfinalAttrsmkDerivationand a few other builders (e.g.buildRustPackage,buildGoModule, etc.)Also, the existence of
finalAttrsis buried in a tiny chapter in the huge Nixpkgs manual, which nobody would even know of were it not for the recent surge of its use. I think there should also be more coverage of this pattern elsewhere in the scattered Nix docs, so that new contributors could be made aware of it before they start contributing. The explanation of the differences betweenrecandfinalAttrsis also terribly unclear and not backed up with concrete examples, like so:getchoo commentedon May 30, 2024
i think #310373 is important to bring up in this conversation.
at first glance,
finalAttrsdoes seem like a "silver bullet" to overrides propagating across the expression. however, continuing from the example above:while the
revofsrcis overridden throughfinalAttrs,hashisn't. as explained in the issue, this and other uses offinalAttrscan easily introduce a lot of footguns to users when overriding; and even in the best case, the use offinalAttrsoverrechere doesn't really improve anything for the end userthis is probably what i want most out of this issue, though. a lot of times the differences between these patterns are exactly that: nitpicks. i'm sure it's frustrated many here time and time again - and if it doesn't actually benefit our user in many cases, why are we doing it? why are we giving new contributors a hard time over preferred patterns to reach the same goal?
i would personally encourage package maintainers to use their personal preference unless it's actually affecting how the expression is used or defined. for other readability/consistency concerns, we will hopefully have
nixfmtsoon tree-wide soon 😄pluiedev commentedon May 30, 2024
The problem with
versionspecifically, I think, is that it's either easy to forget to update the hash if the rev depends onfinalAttrs.version, or it's easy to forget to update the source altogether. Might be better to have a built-in lint/warning/assert against that inmkDerivationisabelroses commentedon May 30, 2024
It may also be worth reading over #293452 and #119942, for more insight.
Atemu commentedon May 30, 2024
@getchoo made the same points I was going to make.
To that I want to add that whenever this comes up, one should think about which parts of the package they want to expose as an external API and what parts you don't.
finalAttrsresponds specifically tooverrideAttrs, notoverrides in general. Whether you want to use the pattern depends on whether you want the reference attributes to be be generically overrideable; part of the package's API. It does not always make sense to do this however. As @getchoo pointed out, exposing the version as part of the API using finalAttrs like this has issues.As a more robust alternative, I've recently discovered that you can simply make such parameters part of the regular
overrideinterface. I think this is even better as it makes them explicit part of the package's external API and allows other parts of the external API to respond to version changes (i.e.withFoo ? lib.versionAtLeast version "6.1"). I did this in the ffmpeg package and I think it's better for it: https://github.com/NixOS/nixpkgs/pull/295691/files#diff-547e4d5699df1c405afdc0c2837df95959ed6de64f384ca5720ee37367eebcceThis design allows you to take any ffmpeg variant (i.e. ffmpeg_7-full) and transform it into any other (i.e. ffmpeg_4-headless) using only
override, without anyoverrideAttrs.Regardless of opinions on these manners though, a table like @pluiedev's would be great to have in the contributor's manual. We generally need more such short summaries and general guidelines to be written down in accessible places.
I also want to mention that, in my book, it is perfectly valid to use
rec,let ... inandfinalAttrsin combination as they each have distinct purposes.nixos-discourse commentedon Jul 4, 2024
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/more-official-guidelines-in-regards-to-styling-choices/48407/3
Mic92 commentedon Jul 7, 2024
What's missing in this table is the performance overhead of finalAttrs. Also I probably should write proper benchmark for this, a performance decrease have been observed by several people because of the fixed input computation.
eclairevoyant commentedon Jul 7, 2024
btw,
finalAttrsalso applies to the nim and php builders, perhaps some others as well that I don't know offhand.pluiedev commentedon Jul 9, 2024
It's kinda part of the problem, isn't it? It's very inconsistent as to which builders have the fixed-point capability...
eclairevoyant commentedon Jul 9, 2024
Someone has to care enough to add it to the other builders, basically. Or redo the entire fixed-point concept from scratch.
goyalyashpal commentedon May 31, 2025
recis less verbose than the latter and seems somewhat easy to understand, but maybe due to anchor bias.(finalAttrs: { ... })thing.Also,
finalAttrs.{pname,version}introducing overriding traps #310373 infetchTarballeven without overrides.mentioned at Documentation: guide for using
let invsrecvsfinalAttrs#315337 (comment) by @ getchoonow, which one should i use when creating a yarn / node package?
rec { ... }:nixpkgs/pkgs/by-name/ku/kuro/package.nix
Lines 16 to 18 in 942c4a2
(finalAttrs: { ... })nixpkgs/doc/languages-frameworks/javascript.section.md
Lines 592 to 594 in d313d94
5 remaining items
oxalica commentedon Jul 2, 2025
I don't really like the word "migrate" which seems one is superior than another deprecated one, while they have different semantics and are not actually interchangeable in their own scopes.
Sorry for duplicating about detail reasons below. I didn't read the first comment correctly at the first glance.
recandletare variable substitution to only decrease (Nix) program complexity.finalAttrsis for responsive variables that should change onoverride. We should not usefinalAttrsfor any "duplicated literal" that are NOT responsive. Otherwise overriding them will make the the derivation semantically incorrect, thus prevent people from building them successfully after override. See #310373An obvious example is
github:awesomeprogram/awesomeprogramshould not be changed togithub:patched/patchedorgithub:awesomeprogram/patchedwhen overriding its name. This does not improve anything but adds a footgun and worsen eval time.pbsds commentedon Jul 2, 2025
It's not spelled out anywhere explicitly, and it at least took me a bit before it dawned on me. And I was not alone, seeing as every now and then I get as push-back during review to the tune of "we're not using finalAttrs here anyway so it is not needed". (Rarely do I consider those nits blocking anyway, so I usually just go ahead and merge)
I very much agree with the sentiment. "migrate" was worded from the imagined drive-by contributors point of view. That wording communicates what I imaged their state of mind and motivation to be.
nixos-discourse commentedon Oct 23, 2025
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/what-does-rec-mean/54880/3
ShamrockLee commentedon Nov 8, 2025
After PR #234651 introduces
lib.extendMkDerivation, we are pushing build helpers' adoption to the fixed-point arguments (finalAttrs: { ... }). There's a chapter of Nixpkgs about extending a build helper withlib.extendMkDerivation, and I agree with @pluiedev that the documentation is kinda scattered and less discoverable.eveeifyeve commentedon Jan 8, 2026
Is there a made mind yet about to use
finalAttrsover rec? Because I read some stuff about people are still not sure to A. use finalAttrs instead of rec and B. understanding there isoverrideAttrsvsoverrideonfinalAttrs.limwa commentedon Mar 5, 2026
Looking at some recently merged pull requests, I'd say that the way to go is to use
finalAttrsinstead ofrec.Here are some of the PRs I saw, all merged 2 weeks ago:
The 3rd pull request was opened specifically because Lix now emits a warning when dynamic attrs are used in rec attrsets, like so:
I'm not sure how common this pattern is in nixpkgs...
MattSturgeon commentedon Mar 5, 2026
We should be cautious of encouraging blind use of one specific pattern, as each pattern has subtly semantic differences. Ideally, people should be selecting the right tool for the job; deciding whether to inherit the "current" value from the "local" fixpoint (
letandrec) or react to the "final" override value (finalAttrsfixpoint).When supported,
finalAttrsshould be the go to, because reacting to overridden values is usually more useful and intuitive.In cases where a "local" fixpoint is more appropriate, using
let inorlib.fixshould be preferred overrec. Other than extremely trivial cases, perhaps. Whilerecis often most concise, it is often more confusing and it has similar footguns towith.ansible: use finalAttrs instead of rec
ansible: use finalAttrs instead of rec
ansible: use finalAttrs instead of rec
python3Packages.pybase91: init at 0.2.2
python3Packages.pybase91: init at 0.2.2
libinput: modernize, clean-up