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-getting-sources">
6
7<title>Getting the Sources</title>
8
9<para>By default, NixOS’s <command>nixos-rebuild</command> command
10uses the NixOS and Nixpkgs sources provided by the
11<literal>nixos-unstable</literal> channel (kept in
12<filename>/nix/var/nix/profiles/per-user/root/channels/nixos</filename>).
13To modify NixOS, however, you should check out the latest sources from
14Git. This is done using the following command:
15
16<screen>
17$ nixos-checkout <replaceable>/my/sources</replaceable>
18</screen>
19
20or
21
22<screen>
23$ mkdir -p <replaceable>/my/sources</replaceable>
24$ cd <replaceable>/my/sources</replaceable>
25$ nix-env -i git
26$ git clone git://github.com/NixOS/nixpkgs.git
27$ cd nixpkgs
28$ git remote add channels git://github.com/NixOS/nixpkgs-channels.git
29$ git remote update channels
30</screen>
31
32This will check out the latest NixOS sources to
33<filename><replaceable>/my/sources</replaceable>/nixpkgs/nixos</filename>
34and the Nixpkgs sources to
35<filename><replaceable>/my/sources</replaceable>/nixpkgs</filename>.
36(The NixOS source tree lives in a subdirectory of the Nixpkgs
37repository.) The remote <literal>channels</literal> refers to a
38read-only repository that tracks the Nixpkgs/NixOS channels (see <xref
39linkend="sec-upgrading"/> for more information about channels). Thus,
40the Git branch <literal>channels/nixos-14.12</literal> will contain
41the latest built and tested version available in the
42<literal>nixos-14.12</literal> channel.</para>
43
44<para>It’s often inconvenient to develop directly on the master
45branch, since if somebody has just committed (say) a change to GCC,
46then the binary cache may not have caught up yet and you’ll have to
47rebuild everything from source. So you may want to create a local
48branch based on your current NixOS version:
49
50<screen>
51$ nixos-version
5214.04.273.ea1952b (Baboon)
53
54$ git checkout -b local ea1952b
55</screen>
56
57Or, to base your local branch on the latest version available in a
58NixOS channel:
59
60<screen>
61$ git remote update channels
62$ git checkout -b local channels/nixos-14.12
63</screen>
64
65(Replace <literal>nixos-14.12</literal> with the name of the channel
66you want to use.) You can use <command>git merge</command> or
67<command>git rebase</command> to keep your local branch in sync with
68the channel, e.g.
69
70<screen>
71$ git remote update channels
72$ git merge channels/nixos-14.12
73</screen>
74
75You can use <command>git cherry-pick</command> to copy commits from
76your local branch to the upstream branch.</para>
77
78<para>If you want to rebuild your system using your (modified)
79sources, you need to tell <command>nixos-rebuild</command> about them
80using the <option>-I</option> flag:
81
82<screen>
83$ nixos-rebuild switch -I nixpkgs=<replaceable>/my/sources</replaceable>/nixpkgs
84</screen>
85
86</para>
87
88<para>If you want <command>nix-env</command> to use the expressions in
89<replaceable>/my/sources</replaceable>, use <command>nix-env -f
90<replaceable>/my/sources</replaceable>/nixpkgs</command>, or change
91the default by adding a symlink in
92<filename>~/.nix-defexpr</filename>:
93
94<screen>
95$ ln -s <replaceable>/my/sources</replaceable>/nixpkgs ~/.nix-defexpr/nixpkgs
96</screen>
97
98You may want to delete the symlink
99<filename>~/.nix-defexpr/channels_root</filename> to prevent root’s
100NixOS channel from clashing with your own tree.</para>
101
102<!-- FIXME: not sure what this means.
103<para>You should not pass the base directory
104<filename><replaceable>/my/sources</replaceable></filename>
105to <command>nix-env</command>, as it will break after interpreting expressions
106in <filename>nixos/</filename> as packages.</para>
107-->
108
109</chapter>