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
Nix language tutorial #267
Nix language tutorial #267
Conversation
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: |
a4b1f42
to
f7513aa
Compare
See #286 for my WIP |
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/2022-07-28-nix-documentation-team-meeting-6/20627/1 |
143334f
to
90fdd2a
Compare
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/tweag-nix-dev-update-34/20901/1 |
c8effaa
to
b98bdc3
Compare
86a394d
to
a68cf03
Compare
@domenkozar where can we see the preview? |
For some reason it doesn't work, probably because the PR is older than the pages installation. We could fix it by opening a new PR, what do you think? |
Shall we instead merge and fix forward? Your editors will have an easier time working on language that way. |
:::{note} | ||
If you are familiar with JSON, imagine the Nix language as *JSON with functions*. | ||
|
||
Nix language data types *without functions* work just like their counterparts in JSON and look very similar. |
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.
This is not true. It's fine to say think about Nix as JSON plus functions, but this is only an analogy. Specifically:
- Strings in C++ Nix are byte sequences and not Unicode-encoded in some shape or form as in JSON.
- Integers in Nix are 64 bit integers and not represented as floating point numbers as they are in JSON.
- Floats overall behave weirdly in Nix to the point that you should warn about them, i.e. they get truncated when serializing them in many situations.
- Due to the nature of Nix's strings, not every legal Nix attribute name is a legal JSON object key.
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 think the wording "imagine the Nix language as" is somehow similar to saying "think about Nix as", isn't it?
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.
Yes, but the claim in the second sentence is stronger than that and wrong, as I've pointed out. “Nix language data types without functions” does not work just like their counterparts in JSON.
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.
Well, we're not saying they work exactly the same. For practical intents and purposes, the differences that you name seem exotic, and if nothing else are completely out of scope of this beginner tutorial. Learners are not even supposed to be able to write any code here, so there is no need for them to know at this point that some attribute names will not properly map to JSON keys.
What do you propose as a change? Say that dead Nix data works "much like" JSON? I wouldn't degrade it to something like "similar to JSON", because there is nothing obvious even from using the Nix language for a few years that would clearly point out a dissimilarity. The problem I have with exposing these edge cases in the first place is that it would mandate explaining them, and that would just be a distraction for everyone - already because it diverts readers' attention to inessential details, but also because giving it the tiny amount of room it deserves is in fact most of the work. How would you put in one brief sentence what makes these two examples semantically different?
I'd be really curious to know what kind of use cases made you discover these thorny edges.
PS: That kind of information belongs to the reference manual, where we have way too little of it. But please let's not dump it on unsuspecting beginners.
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 feel that it is at least implied. With tutorials as the first contact point, one should be careful to avoid creating wrong impressions in readers – even by accident. FWIW my point is not that these differences must be explained, but rather that we should be careful to word it in a way that we don't imply they don't exist.
I'm also not sure these edges are that far out.
The integer thing is a relatively well known JSON pitfall – Nix is not thorny here as it behaves like you'd expect.
> echo 34343499993845656749 | jq
34343499993845658000
The float situation can be discovered quite easily. E.g.:
nix-repl> 1.000004
1
nix-repl> builtins.toJSON 1.000004
"1"
String indexing, I'd say is a very basic topic that should be covered in a tutorial, since the internal representation is apparent in this, it is relevant. Indexing into non-ASCII strings will unexpectedly e.g. builtins.substring 0 1 "καλημερα"
will return in some garbage being returned. E.g. “Nix strings are similar to C strings” is a statement like the one about JSON data, but more accurate.
I finally managed to sit with someone through the tutorial. I wanted someone with a very specific experience; someone with extensive software engineering experience, mild or no devops experience, and zero exposure to Nix. It took a while to make it happen. On the bright side, they only got stuck on 3 points and had very few questions otherwise, which speaks to the quality of the tutorial. Here's where they got stuck:
|
Do we though? I've never actually heard anyone say that, and usually people talk about interpolation instead. The term really only seems to show up in documentation, in the implementations of Nix, rnix, and Tvix (probably also hnix) they're always referred to as "string interpolation" and "Interpolation parts". Maybe we should get rid of the "antiquotation" term completely. |
@mkaito thank you for this extremely valuable insight. A PR with the changes would be awesome! @tazjin Agreed, we should rename "antiquotation" to "string interpolation" in the Nix manual. Sometimes it's the small changes that make a big difference. What are "interpolation parts" though? Is it the same thing? |
@fricklerhandwerk Interpolation parts are the individual parts of a string with interpolations, e.g. in |
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/where-is-a-vm-documented/22258/10 |
as proposed by @mkaito[1] and @tazjin[2] [1]: NixOS/nix.dev#267 (comment) [2]: NixOS/nix.dev#267 (comment) to keep this change minimal, not included here but should follow: - restructure the section on the string data type for better overview - define string interpolation and related terms in one place and link to it from all occurrences - use non-violent language: replace "string coercion" with "string representation" (requires some rewording for grammatical consistency)
as proposed by @mkaito[1] and @tazjin[2] [1]: NixOS/nix.dev#267 (comment) [2]: NixOS/nix.dev#267 (comment) to keep this change minimal, not included here but should follow: - restructure the section on the string data type for better overview - define string interpolation and related terms in one place and link to it from all occurrences - use non-violent language: replace "string coercion" with "string representation" (requires some rewording for grammatical consistency)
as proposed by @mkaito[1] and @tazjin[2] and discussed with @edolstra and Nix maintainers [1]: NixOS/nix.dev#267 (comment) [2]: NixOS/nix.dev#267 (comment)
as proposed by @mkaito[1] and @tazjin[2] and discussed with @edolstra and Nix maintainers [1]: NixOS/nix.dev#267 (comment) [2]: NixOS/nix.dev#267 (comment)
as proposed by @mkaito[1] and @tazjin[2] and discussed with @edolstra and Nix maintainers [1]: NixOS/nix.dev#267 (comment) [2]: NixOS/nix.dev#267 (comment) Co-authored-by: John Ericson <git@JohnEricson.me> Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
as proposed by @mkaito[1] and @tazjin[2] and discussed with @edolstra and Nix maintainers [1]: NixOS/nix.dev#267 (comment) [2]: NixOS/nix.dev#267 (comment) Co-authored-by: John Ericson <git@JohnEricson.me> Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
as proposed by @mkaito[1] and @tazjin[2] and discussed with @edolstra and Nix maintainers [1]: NixOS/nix.dev#267 (comment) [2]: NixOS/nix.dev#267 (comment) Co-authored-by: John Ericson <git@JohnEricson.me> Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
as proposed by @mkaito[1] and @tazjin[2] and discussed with @edolstra and Nix maintainers [1]: NixOS/nix.dev#267 (comment) [2]: NixOS/nix.dev#267 (comment) Co-authored-by: John Ericson <git@JohnEricson.me> Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
based on @tazjin's tazjin/nix-1p and @zimbatm's NixCon 2019 talk Reading The Nix Language
As discussed in #288
To render a preview: