= Variables

// TODO what characters are legal in a variable name/identifier?

== Assignment

You can declare variables in Nix and assign values to them.

[source]
.Example
....
nix-repl> a = 7

nix-repl> b = 3

nix-repl> a - b
4
....

[IMPORTANT]
====
The spaces before and after operators aren't always required.
However, you can get unexpected results when you omit them, as shown in the following example.
Nix allows hyphens (`-`) in variable names,
so `a-b` is interpreted as the name of a variable rather than a subtraction operation.

[source]
.Example
....
nix-repl> a-b
error: undefined variable 'a-b'

       at «string»:1:1:

            1| a-b
             | ^
....
====

== Immutability

In Nix, values are _immutable_;
once you assign a value to a variable, you cannot change it.
You can, however, create a new variable with the same name, but in a different scope.
Don't worry if you don't completely understand the previous sentence;
we will see some examples in <<functions>>, <<_let_expressions>>, and <<_with_expressions>>.

[IMPORTANT]
====
In the Nix REPL, it may seem like the values of variables can be changed,
in _apparent_ contradiction to the previous paragraph.
In truth, the REPL works some behind-the-scenes "magic",
effectively creating a new nested scope with each assignment.
This makes it much easier to experiment in the REPL.

[source]
.Example
....
nix-repl> x = 1

nix-repl> x
1

nix-repl> x = x + 1 # creates a new variable called "x"; the original is no longer in scope

nix-repl> x
2
....
====

