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

add inherit section to language basics #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nrdxp
Copy link
Contributor

@nrdxp nrdxp commented Aug 22, 2022

Closes #15

Also add a table mentioning the collection types in Nix

@nrdxp nrdxp force-pushed the inherit-statements branch from 4785d0c to 8eec118 Compare August 22, 2022 02:46
@nrdxp nrdxp changed the title and inherit section to language basics add inherit section to language basics Aug 22, 2022
@nrdxp nrdxp force-pushed the inherit-statements branch 3 times, most recently from 7452436 to 54e7c80 Compare August 22, 2022 02:58
Comment on lines 124 to 146
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
```
Copy link
Collaborator

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"; }

Copy link
Contributor Author

@nrdxp nrdxp Aug 22, 2022

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?

Copy link
Collaborator

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.

Copy link
Contributor Author

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 Show resolved Hide resolved
version = "0.1.0";
};
in
{ inherit name version; } == set.values
Copy link
Collaborator

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

@nrdxp nrdxp force-pushed the inherit-statements branch 2 times, most recently from 06f9222 to c9f1cb7 Compare August 28, 2022 20:37
@nrdxp nrdxp force-pushed the inherit-statements branch from c9f1cb7 to 9a48620 Compare August 28, 2022 20:43
Comment on lines +139 to +145
let
inherit (pkgs) zlib;

nixpkgs = builtins.getFlake "nixpkgs";
pkgs = import nixpkgs { };
in
pkgs.zlib == zlib
Copy link
Collaborator

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

Suggested change
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

Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mention inherit
2 participants