Skip to content
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

[RFC 0126] Standardize special properties #126

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions rfcs/0126-standardize-special-properties.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
feature: standardize-special-properties
start-date: 2022-05-26
author: Anselm Schüler
co-authors: None
shepherd-team: None
shepherd-leader: None
related-issues: None
---

# Summary
[summary]: #summary

Rename attrset attribute names treated specially by Nix to start with a double underscore.
Add a builtin function `guardSpecial` that returns `null` for double underscore prefixed strings.

# Motivation
[motivation]: #motivation

Nix treats many attributes specially with unique behaviours. Some of these are prefixed with double underscores to disambiguate them from everything else. But some aren’t: `recurseForDerivations` isn’t, `type`, when set to `"derivation"`, changes visual output.

It would also be a good idea to avoid accidentally using reserved special attribute names. Therefore I suggest `guardSpecial` that can be used in dynamic attribute names to avoid this.

# Detailed design
[design]: #detailed-design

Properties treated specially by the Nix tools and Nix language are renamed to start with a double underscore. Instances where this is not necessary are `__functor` and `__toString`. `recurseForDerivations` is renamed `__recurseForDerivations` and `type` is renamed `__type`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recurseForDerivations is everywhere, not just in Nixpkgs.
Do we have any evidence of this causing a problem in the real world?
I suggest adapting the definition of builtins.guardSpecial instead.

Same goes for type.
Note that we also have _type, which is the type tag used by module system related constructors. Again, changing this is more trouble than it's worth. Just add _type to guardSpecial's deny list.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t that have to be a completely different RFC?

Note that I’m open to closing this for good, it’s a much bigger change than I had anticipated when I wrote it up.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to expand on the "any evidence of this causing a problem" bit: changing attribute names would be incredibly hard in the very dynamic language nix is. we can find all instances of e.g. drvPath in code, but which of these are used to construct/modify derivations and which aren't? meta is to some extent a special name, changing that would touch almost the entire nixpkgs tree (and introduce mismatches between derivations and modules). changing old names feels like asking for a disaster, and silently dropping assignments can also easily turn into a footgun. 😕

which is not to say that guarding against accidental use of special names is bad! such a guard should (usually) fail very loudly though to catch mistakes early, behaving much like assert ! elem name [ «specials» ] && match "__.*" name == null; name. prefixing new special names also sounds very reasonable.


A new builtin function is introduced, `builtins.guardSpecial`.
If called on a string, it returns the string if it does not start with a double underscore, and `null` otherwise. This can be used by semantically charged non-generic functions that update attrs dynamically to avoid unwanted interactions.

Nixpkgs & Nix need to be updated to reflect this. Therefore, it may be advisable to have a grace period during which unprefixed special properties are still supported, but warn.

# Examples and Interactions
[examples-and-interactions]: #examples-and-interactions

```nix
rec {
set-val-to-null = k: attrs: attrs // {
${guardSpecial k} = null;
};
x = set-val-to-null "__functor" { __functor = _: _: { }; };
y = set-val-to-null "y" { __functor = _: _: { }; };
}
```
evaluates to
```nix
{
set-val-to-null = «lambda»;
x = { __functor = «lambda»; };
y = { y = null; __functor = «lambda»; };
}
```

# Drawbacks
[drawbacks]: #drawbacks

- This change is backwards-incompatible
- This requires judging if certain properties count as special attributes

# Alternatives
[alternatives]: #alternatives

- `guardSpecial` could be introduced on its own, thereby complicating its implementation
- These could be left alone

# Unresolved questions
[unresolved]: #unresolved-questions

I honestly don’t have a full list of these special names, I only listed the ones I know about.

It’s also unclear what exactly counts. I think attrsets passed to functions as de-facto named arguments shouldn’t count (e.g. for `derivation`). You could even argue the outputs of `listToAttrs` should be prefixed!

The name of `guardSpecial` might need some work.

# Future work
[future]: #future-work

This eases introduction of any future special names.

It can also be assumed the implementation of `guardSpecial` won’t need to be updated if this standard naming convention is adhered to.