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