about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-11-25T12·21+0100
committerVincent Ambo <mail@tazj.in>2020-11-25T12·40+0100
commit3236b7ff284383a66df114277c0dccc09dce3e73 (patch)
tree0434565e226c10035bcbfd2b34373aed520989b9
parent56a1f03a109da9e33d5c2018c5fe3e3080cd93e4 (diff)
Add small section about `or` expressions
-rw-r--r--nix/nix-1p/README.md28
1 files changed, 27 insertions, 1 deletions
diff --git a/nix/nix-1p/README.md b/nix/nix-1p/README.md
index 7f6770a871eb..0399bc41cd2d 100644
--- a/nix/nix-1p/README.md
+++ b/nix/nix-1p/README.md
@@ -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)
@@ -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.