-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
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
Examples in NixOS options manual do not escape attribute names #195562
Comments
Good catch! Definitely not a WONTFIX. This also affects https://search.nixos.org. The places to look at are
lib.strings.escapeNixIdentifier .
|
agreed. should probably be done in |
+1000. If I understand correctly, you're suggesting that non- |
that would be one way to do it. might be nicer to not fix non-literal examples that much though and only escape the attribute names where necessary, essentially justifying the current assumption that attr names of examples are safe to emit as-is. not making everything literal immediately would need less processing in nix and give greater flexibility for renderers. (in fact we may want to at some point push down |
I am not really in favour of introducing a new ad-hoc format ("JSON with possibly Nix-escaped keys") for something publicly exported like cc @roberth |
The examples are, in general, Nix expressions, a data type that does not have a structured representation in JSON.¹ Hence, the only representation exported by the module system should have its defaults represented by Nix strings containing expressions, which are subsequently escaped because that's how JSON represents its strings. Any consumers of this JSON will treat the JSON escapes according to spec, so that those consumers work with strings containing Nix expressions, as intended.
Having established how we'll represent our data types, the rest is "boring". nix-repl> lib.generators.toPretty {} { "a.b" = true; "/home/user" = "foo"; }
"{\n \"/home/user\" = \"foo\";\n \"a.b\" = true;\n}" ¹: We could describe the AST structure in JSON, but that's besides the point. |
Sounds about right. And since we also allow defaults/examples (I don't see any reason to treat defaults differently from examples in this matter) to be documentation strings instead of Nix expressions, we need |
defaults have semantics, examples do not. there's
if the expression involves lambdas or non-constant subexpressions (like accesses to |
Likewise, having multiple longer code paths and a more complicated interface is unnecessary. |
Problem
Attribute sets in the manual's examples do not properly escape keys when are, for example, paths. For the "Example" for an option, we see
{ /home/user = "foo"; }
which is invalid Nix code instead of{ "/home/user" = "foo"; }
which is valid Nix code. This happens when the Nix options getbuiltins.toJSON
run on them during the manual generation. In the wild, look at/mnt/btr_pool
in the example of this option. Maybe the general opinion on this is, "obviously attrset keys can't be paths", but it tripped me up for a few minutes as a newcomer, and I think it could be a nice quality of life improvement.Checklist
Proposal
Changing
toJSON
in Nix seems inappropriate, so maybe I could open a PR with a Nix function which escapes the problem attributes, but that might have performance issues. I am open to suggestions (or just marking it as "won't fix").The text was updated successfully, but these errors were encountered: