1# Changing the Configuration {#sec-changing-config}
2
3The file `/etc/nixos/configuration.nix` contains the current
4configuration of your machine. Whenever you've [changed
5something](#ch-configuration) in that file, you should do
6
7```ShellSession
8# nixos-rebuild switch
9```
10
11to build the new configuration, make it the default configuration for
12booting, and try to realise the configuration in the running system
13(e.g., by restarting system services).
14
15::: {.warning}
16This command doesn't start/stop [user services](#opt-systemd.user.services)
17automatically. `nixos-rebuild` only runs a `daemon-reload` for each user with running
18user services.
19:::
20
21::: {.warning}
22These commands must be executed as root, so you should either run them
23from a root shell or by prefixing them with `sudo -i`.
24:::
25
26You can also do
27
28```ShellSession
29# nixos-rebuild test
30```
31
32to build the configuration and switch the running system to it, but
33without making it the boot default. So if (say) the configuration locks
34up your machine, you can just reboot to get back to a working
35configuration.
36
37There is also
38
39```ShellSession
40# nixos-rebuild boot
41```
42
43to build the configuration and make it the boot default, but not switch
44to it now (so it will only take effect after the next reboot).
45
46You can make your configuration show up in a different submenu of the
47GRUB 2 boot screen by giving it a different *profile name*, e.g.
48
49```ShellSession
50# nixos-rebuild switch -p test
51```
52
53which causes the new configuration (and previous ones created using
54`-p test`) to show up in the GRUB submenu "NixOS - Profile 'test'".
55This can be useful to separate test configurations from "stable"
56configurations.
57
58Finally, you can do
59
60```ShellSession
61$ nixos-rebuild build
62```
63
64to build the configuration but nothing more. This is useful to see
65whether everything compiles cleanly.
66
67If you have a machine that supports hardware virtualisation, you can
68also test the new configuration in a sandbox by building and running a
69QEMU *virtual machine* that contains the desired configuration. Just do
70
71```ShellSession
72$ nixos-rebuild build-vm
73$ ./result/bin/run-*-vm
74```
75
76The VM does not have any data from your host system, so your existing
77user accounts and home directories will not be available unless you have
78set `mutableUsers = false`. Another way is to temporarily add the
79following to your configuration:
80
81```nix
82users.users.your-user.initialHashedPassword = "test";
83```
84
85*Important:* delete the \$hostname.qcow2 file if you have started the
86virtual machine at least once without the right users, otherwise the
87changes will not get picked up. You can forward ports on the host to the
88guest. For instance, the following will forward host port 2222 to guest
89port 22 (SSH):
90
91```ShellSession
92$ QEMU_NET_OPTS="hostfwd=tcp::2222-:22" ./result/bin/run-*-vm
93```
94
95allowing you to log in via SSH (assuming you have set the appropriate
96passwords or SSH authorized keys):
97
98```ShellSession
99$ ssh -p 2222 localhost
100```