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
Open
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
44 changes: 38 additions & 6 deletions src/ch05-01-language-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

These values are mostly similar to JSON:

| Type | Description | Example
|---|---|---|
| integer | Whole number | 1 |
| float | Floating point number | 1.054 |
| string | UTF-8 string | "hello!" |
| path | File or url | ./default.nix |
| Type | Description | Example
|---------------|--------------------------|----------------------|
| integer | Whole number | `1` |
| float | Floating point number | `1.054` |
| string | UTF-8 string | `"hello!"` |
| path | File or url | `./default.nix` |
| list | Multi-type list | `[ "hello" 1 ]` |
| attribute set | Key-value structure | `{ key = "value"; }` |

*NOTE*: Paths are special. They will be resolved relative to the file.
They must start with a "." or "/", similar to how they would be expressed in a shell.
Expand Down Expand Up @@ -112,6 +114,36 @@ let expressions work similarly to how they work in Haskell.
sha256 = "...";
};
```

## Inherit statements

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.

```nix
let
name = "package";
in
{ inherit name; } == { name = "package"; }
```

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 (pkgs) zlib;

nixpkgs = builtins.getFlake "nixpkgs";
pkgs = import nixpkgs { };
in
pkgs.zlib == zlib
Comment on lines +139 to +145
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

```

## With expressions

Expand Down