-
Notifications
You must be signed in to change notification settings - Fork 11
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
add inherit
section to language basics
#18
base: main
Are you sure you want to change the base?
Conversation
4785d0c
to
8eec118
Compare
inherit
section to language basicsinherit
section to language basics
7452436
to
54e7c80
Compare
src/ch05-01-language-basics.md
Outdated
An inherit statement can be used to _pull_ values out of a parent scope | ||
into the current one. Values are whitespace separated and the statement | ||
ends with a `;`. They are valid only in `let` blocks and inside | ||
attribute sets. | ||
|
||
If an attribute set in parenthesis follows immediately after the `inherit` | ||
keyword, then values can also be pulled directly out of the given set. | ||
|
||
In a `let` block, inherit statements can also come before the values they | ||
reference, so long as they are in the same block. | ||
|
||
```nix | ||
let | ||
inherit (set.values) name version; | ||
|
||
set.values = { | ||
name = "package"; | ||
version = "0.1.0"; | ||
}; | ||
in | ||
{ inherit name version; } == set.values | ||
``` |
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 we should separate this into two separate examples, one with just inherit
pulling defined values, and then another with pulling values from an attr set.
Also, nix repl might be better here, so we don't have to leverage a let block
nix-repl> name = "foo"
nix-repl> version = "0.1.0"
nix-repl> :p { inherit name version; }
{ name = "foo"; version = "0.1.0"; }
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 was deliberately attempting to be concise, although I'm not super against splitting it in two examples either, however in this case isn't the let
block part of the demonstration since it's one of two places where an inherit
can ocur?
Also, in regards to repl style blocks, I was attempting to follow the Rust way of writing valid code blocks that could be pulled out and evaluated independantly to a boolean. It'd be great if we could somehow find a way to automate this to ensure our examples stay valid.
However, maybe this isn't as important as with Rust since Nix doesn't really change that much?
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 there's a lot going on in the blocks, let ... in block, recursive definition of set
, then inheriting the value, and then a comparison which isn't demonstrated.
The rust-lang book has a leg up in that you can just execute code. Until then we will need to be more acommodating.
The other thing is that it's not apparent how to run the code, nix eval -f example.nix
would probably be right, but that's not apparent to the user.
I've been doing
$ cat <file>
$ <nix command> <file>
For now, but it would be nice to move away from this. Just not sure what would be best.
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.
Perhaps what is needed is to develop a mdbook plugin to actually evaluate the code in browser and CI, similar to Rust. This is definitely outside of the scope of this PR though.
I went ahead and split this into two (hopefully simpler) examples.
src/ch05-01-language-basics.md
Outdated
version = "0.1.0"; | ||
}; | ||
in | ||
{ inherit name version; } == set.values |
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.
If we ran this in the repl, we could do something like:
nix-repl> { inherit name; } == { name = "foo"; }
true
06f9222
to
c9f1cb7
Compare
c9f1cb7
to
9a48620
Compare
let | ||
inherit (pkgs) zlib; | ||
|
||
nixpkgs = builtins.getFlake "nixpkgs"; | ||
pkgs = import nixpkgs { }; | ||
in | ||
pkgs.zlib == zlib |
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.
feel like we could extend this further
let | |
inherit (pkgs) zlib; | |
nixpkgs = builtins.getFlake "nixpkgs"; | |
pkgs = import nixpkgs { }; | |
in | |
pkgs.zlib == zlib | |
let | |
inherit (pkgs.zlib) version; | |
zlibVersion = pkgs.zlib.version; | |
nixpkgs = builtins.getFlake "nixpkgs"; | |
pkgs = import nixpkgs { }; | |
in | |
zlibVersion == version |
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 would also introduce the "deeply nested" syntax
Closes #15
Also add a table mentioning the collection types in Nix