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 small section about or expressions #5

Merged
merged 1 commit into from Nov 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Expand Up @@ -23,7 +23,8 @@ important is missing.
- [`if ... then ... else ...`](#if--then--else-)
- [`inherit` keyword](#inherit-keyword)
- [`with` statements](#with-statements)
- [`import` / `NIX_PATH` / `<entry>`](#import--nixpath--entry)
- [`import` / `NIX_PATH` / `<entry>`](#import--nix_path--entry)
- [`or` expressions](#or-expressions)
- [Standard libraries](#standard-libraries)
- [`builtins`](#builtins)
- [`pkgs.lib`](#pkgslib)
Expand Down Expand Up @@ -331,6 +332,31 @@ let pkgs = import <nixpkgs> {};
in pkgs.something
```

## `or` expressions

Nix has a keyword called `or` which can be used to access a value from an
attribute set while providing a fallback to a default value.

The syntax is simple:

```nix
# Access an existing attribute
let set = { a = 42; };
in set.a or 23
```

Since the attribute `a` exists, this will return `42`.


```
# ... or fall back to a default if there is no such key
let set = { };
in set.a or 23
```

Since the attribute `a` does not exist, this will fall back to returning the
default value `23`.

# Standard libraries

Yes, libraries, plural.
Expand Down