1<chapter xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xmlns:xi="http://www.w3.org/2001/XInclude"
4 version="5.0"
5 xml:id="sec-building-parts">
6 <title>Building Specific Parts of NixOS</title>
7 <para>
8 With the command <command>nix-build</command>, you can build specific parts
9 of your NixOS configuration. This is done as follows:
10<screen>
11$ cd <replaceable>/path/to/nixpkgs/nixos</replaceable>
12$ nix-build -A config.<replaceable>option</replaceable></screen>
13 where <replaceable>option</replaceable> is a NixOS option with type
14 “derivation” (i.e. something that can be built). Attributes of interest
15 include:
16 <variablelist>
17 <varlistentry>
18 <term>
19 <varname>system.build.toplevel</varname>
20 </term>
21 <listitem>
22 <para>
23 The top-level option that builds the entire NixOS system. Everything else
24 in your configuration is indirectly pulled in by this option. This is
25 what <command>nixos-rebuild</command> builds and what
26 <filename>/run/current-system</filename> points to afterwards.
27 </para>
28 <para>
29 A shortcut to build this is:
30<screen>
31$ nix-build -A system</screen>
32 </para>
33 </listitem>
34 </varlistentry>
35 <varlistentry>
36 <term>
37 <varname>system.build.manual.manual</varname>
38 </term>
39 <listitem>
40 <para>
41 The NixOS manual.
42 </para>
43 </listitem>
44 </varlistentry>
45 <varlistentry>
46 <term>
47 <varname>system.build.etc</varname>
48 </term>
49 <listitem>
50 <para>
51 A tree of symlinks that form the static parts of
52 <filename>/etc</filename>.
53 </para>
54 </listitem>
55 </varlistentry>
56 <varlistentry>
57 <term>
58 <varname>system.build.initialRamdisk</varname>
59 </term>
60 <term>
61 <varname>system.build.kernel</varname>
62 </term>
63 <listitem>
64 <para>
65 The initial ramdisk and kernel of the system. This allows a quick way to
66 test whether the kernel and the initial ramdisk boot correctly, by using
67 QEMU’s <option>-kernel</option> and <option>-initrd</option> options:
68<screen>
69$ nix-build -A config.system.build.initialRamdisk -o initrd
70$ nix-build -A config.system.build.kernel -o kernel
71$ qemu-system-x86_64 -kernel ./kernel/bzImage -initrd ./initrd/initrd -hda /dev/null
72</screen>
73 </para>
74 </listitem>
75 </varlistentry>
76 <varlistentry>
77 <term>
78 <varname>system.build.nixos-rebuild</varname>
79 </term>
80 <term>
81 <varname>system.build.nixos-install</varname>
82 </term>
83 <term>
84 <varname>system.build.nixos-generate-config</varname>
85 </term>
86 <listitem>
87 <para>
88 These build the corresponding NixOS commands.
89 </para>
90 </listitem>
91 </varlistentry>
92 <varlistentry>
93 <term>
94 <varname>systemd.units.<replaceable>unit-name</replaceable>.unit</varname>
95 </term>
96 <listitem>
97 <para>
98 This builds the unit with the specified name. Note that since unit names
99 contain dots (e.g. <literal>httpd.service</literal>), you need to put
100 them between quotes, like this:
101<screen>
102$ nix-build -A 'config.systemd.units."httpd.service".unit'
103</screen>
104 You can also test individual units, without rebuilding the whole system,
105 by putting them in <filename>/run/systemd/system</filename>:
106<screen>
107$ cp $(nix-build -A 'config.systemd.units."httpd.service".unit')/httpd.service \
108 /run/systemd/system/tmp-httpd.service
109# systemctl daemon-reload
110# systemctl start tmp-httpd.service
111</screen>
112 Note that the unit must not have the same name as any unit in
113 <filename>/etc/systemd/system</filename> since those take precedence over
114 <filename>/run/systemd/system</filename>. That’s why the unit is
115 installed as <filename>tmp-httpd.service</filename> here.
116 </para>
117 </listitem>
118 </varlistentry>
119 </variablelist>
120 </para>
121</chapter>