aboutsummaryrefslogtreecommitdiff
path: root/src/nix/repl.md
blob: f8ad49199a428787ac6b8a9e108757d1b2bd41c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
R""(

**Note:** this command's interface is based heavily around [*installables*](./nix.md#installables), which you may want to read about first (`nix --help`).

# Examples

* Display all special commands within the REPL:

  ```console
  # nix repl
  nix-repl> :?
  ```

* Evaluate some simple Nix expressions:

  ```console
  # nix repl

  nix-repl> 1 + 2
  3

  nix-repl> map (x: x * 2) [1 2 3]
  [ 2 4 6 ]
  ```

* Interact with Nixpkgs in the REPL:

  ```console
  # nix repl --file example.nix
  Loading Installable ''...
  Added 3 variables.

  # nix repl --expr '{a={b=3;c=4;};}'
  Loading Installable ''...
  Added 1 variables.

  # nix repl --expr '{a={b=3;c=4;};}' a
  Loading Installable ''...
  Added 1 variables.

  # nix repl --extra-experimental-features 'flakes repl-flake' nixpkgs
  Loading Installable 'flake:nixpkgs#'...
  Added 5 variables.

  nix-repl> legacyPackages.x86_64-linux.emacs.name
  "emacs-27.1"

  nix-repl> legacyPackages.x86_64-linux.emacs.name
  "emacs-27.1"

  nix-repl> :q

  # nix repl --expr 'import <nixpkgs>{}'

  Loading Installable ''...
  Added 12439 variables.

  nix-repl> emacs.name
  "emacs-27.1"

  nix-repl> emacs.drvPath
  "/nix/store/lp0sjrhgg03y2n0l10n70rg0k7hhyz0l-emacs-27.1.drv"

  nix-repl> drv = runCommand "hello" { buildInputs = [ hello ]; } "hello; hello > $out"

  nix-repl> :b drv
  this derivation produced the following outputs:
    out -> /nix/store/0njwbgwmkwls0w5dv9mpc1pq5fj39q0l-hello

  nix-repl> builtins.readFile drv
  "Hello, world!\n"

  nix-repl> :log drv
  Hello, world!
  ```

# Description

This command provides an interactive environment for evaluating Nix
expressions. (REPL stands for 'read–eval–print loop'.)

On startup, it loads the Nix expressions named *files* and adds them
into the lexical scope. You can load addition files using the `:l
<filename>` command, or reload all files using `:r`.

)""