1# Building Specific Parts of NixOS {#sec-building-parts}
2
3With the command `nix-build`, you can build specific parts of your NixOS
4configuration. This is done as follows:
5
6```ShellSession
7$ cd /path/to/nixpkgs/nixos
8$ nix-build -A config.option
9```
10
11where `option` is a NixOS option with type "derivation" (i.e. something
12that can be built). Attributes of interest include:
13
14`system.build.toplevel`
15
16: The top-level option that builds the entire NixOS system. Everything
17 else in your configuration is indirectly pulled in by this option.
18 This is what `nixos-rebuild` builds and what `/run/current-system`
19 points to afterwards.
20
21 A shortcut to build this is:
22
23 ```ShellSession
24 $ nix-build -A system
25 ```
26
27`system.build.manual.manualHTML`
28
29: The NixOS manual.
30
31`system.build.etc`
32
33: A tree of symlinks that form the static parts of `/etc`.
34
35`system.build.initialRamdisk` , `system.build.kernel`
36
37: The initial ramdisk and kernel of the system. This allows a quick
38 way to test whether the kernel and the initial ramdisk boot
39 correctly, by using QEMU's `-kernel` and `-initrd` options:
40
41 ```ShellSession
42 $ nix-build -A config.system.build.initialRamdisk -o initrd
43 $ nix-build -A config.system.build.kernel -o kernel
44 $ qemu-system-x86_64 -kernel ./kernel/bzImage -initrd ./initrd/initrd -hda /dev/null
45 ```
46
47`system.build.nixos-rebuild` , `system.build.nixos-install` , `system.build.nixos-generate-config`
48
49: These build the corresponding NixOS commands.
50
51`systemd.units.unit-name.unit`
52
53: This builds the unit with the specified name. Note that since unit
54 names contain dots (e.g. `httpd.service`), you need to put them
55 between quotes, like this:
56
57 ```ShellSession
58 $ nix-build -A 'config.systemd.units."httpd.service".unit'
59 ```
60
61 You can also test individual units, without rebuilding the whole
62 system, by putting them in `/run/systemd/system`:
63
64 ```ShellSession
65 $ cp $(nix-build -A 'config.systemd.units."httpd.service".unit')/httpd.service \
66 /run/systemd/system/tmp-httpd.service
67 # systemctl daemon-reload
68 # systemctl start tmp-httpd.service
69 ```
70
71 Note that the unit must not have the same name as any unit in
72 `/etc/systemd/system` since those take precedence over
73 `/run/systemd/system`. That's why the unit is installed as
74 `tmp-httpd.service` here.