-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib.getOptionalAttrs: init #269586
base: master
Are you sure you want to change the base?
lib.getOptionalAttrs: init #269586
Conversation
5e273b8
to
f7f1c5d
Compare
What are the use cases? And in any case, needs tests |
It's used inside the definition of I also find it useful when trying to implement the support for multiple VSCode extension registries (#121583).
Where could I add a test case for Nixpkgs |
https://github.com/NixOS/nixpkgs/blob/master/lib/tests/misc.nix |
f7f1c5d
to
c0c124a
Compare
Tests added. |
The ofborg bot seems broken. |
c0c124a
to
dda3c4b
Compare
This is a generalized |
I don't think we need to try to reuse names of In particular A list-based function is even less symmetrical, so I think this name fits even less. Here's some alternate ideas:
|
8ab7d67
to
8e6cf7a
Compare
8e6cf7a
to
7b8e93a
Compare
I agree that the word
Regarding these candidates, I like
Update: |
BTW, I have generalized the implementation to also take an attribute set aside from a list of strings, which meets the expectation of NixOS/nix#9050. |
I we should lean into this:
They are established names, and established names are hard to erase, so I see improving their behavior as a good thing. I don't see having multiple functions doing the same thing as a good situation to have.
This would have been good when the builtin was first added, but that could now be |
Please check out NixOS/nix#9050 (comment), where I proposed this:
Would be very consistent and not conflict with any |
I'll revert back to We could extend it later if we like to. |
7b8e93a
to
18680a0
Compare
18680a0
to
b6e8f56
Compare
Bump! |
# A list of attribute names to get out of `attrs`. | ||
names: | ||
# The set to get the named attributes from. | ||
attrs: getAttrs (intersectLists (attrNames attrs) names) attrs; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe an implementation using intersectAttrs
would be more efficient.
How about |
nixpkgs/pkgs/applications/editors/vscode/extension-registries/commons/modifiers/cookrefs.nix Line 37 in 5817f71
There you can just do this instead: { inherit (ref) description homepage"; }
Lines 457 to 458 in 87c9a09
That's a better use case. I guess the problem is really that we have two ways to represent a set of strings. On one hand there's On the other hand there's Here's an idea for an alternative: Let's have a function that allows easily constructing an attribute set from a list of strings: nullAttrs = attrs: genAttrs attrs (_: null) This way getExistingAttrs names attrs
==
intersectAttrs (nullAttrs names) attrs This also makes it clear that if you have multiple such calls, the What do you think? |
Thank you for spending time reading the hyperlinked PRs.
You're right. I cannot recall why I did it that way back then.
Thank you.
I'm haven't dug into the C++ implementation of Nix Language lists and attribute sets yet. So the point is, If the recommended way to implement is, names: intersectAttrs (nullAttrs name) it would be better to interpolate |
I'm all for that, I think all |
Actually, Unfortunately, the builtins don't offer many ways to go from lists to attrsets. Here's another possible implementation that uses single-attribute attrsets instead of getExistingAttrs = l: p:
lib.zipAttrsWith
(name: values: head values)
(map
(n: if p?${n} then { ${n} = p.${n}; } else { })
l); If we really want performance, we should probably do NixOS/nix#9050 or make |
b6e8f56
to
37b853d
Compare
Now it's called |
We can optimise that later if necessary. In turn, an interface like I'm really not convinced by the overall motivation for this function and would like to see |
Description of changes
This PR introduces
lib.getOptionalAttrs
, a handy function that returns a sub- attribute according to the input list of attribute names. Unlikelib.getAttrs
, it skips attributes that are not in the input attribute set instead of throwing the error of missing attributes. It acts as the complement ofremoveAttrs
.Changes during PR review:
getExistingAttrs
->getOptionalAttrs
.Things done
nix.conf
? (See Nix manual)sandbox = relaxed
sandbox = true
nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"
. Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/
)Priorities
Add a 👍 reaction to pull requests you find important.