By default, NixOS’s <command>nixos-rebuild</command> command uses the NixOS and Nixpkgs sources provided by the <literal>nixos</literal> channel (kept in <filename>/nix/var/nix/profiles/per-user/root/channels/nixos</filename>). To modify NixOS, however, you should check out the latest sources from Git. This is as follows:
This will check out the latest Nixpkgs sources to <filename>./nixpkgs</filename> the NixOS sources to <filename>./nixpkgs/nixos</filename>. (The NixOS source tree lives in a subdirectory of the Nixpkgs repository.) The remote <literal>channels</literal> refers to a read-only repository that tracks the Nixpkgs/NixOS channels (see <xreflinkend="sec-upgrading"/> for more information about channels). Thus, the Git branch <literal>channels/nixos-17.03</literal> will contain the latest built and tested version available in the <literal>nixos-17.03</literal> channel.
It’s often inconvenient to develop directly on the master branch, since if somebody has just committed (say) a change to GCC, then the binary cache may not have caught up yet and you’ll have to rebuild everything from source. So you may want to create a local branch based on your current NixOS version:
(Replace <literal>nixos-17.03</literal> with the name of the channel you want to use.) You can use <command>git merge</command> or <command>git rebase</command> to keep your local branch in sync with the channel, e.g.
If you want to rebuild your system using your (modified) sources, you need to tell <command>nixos-rebuild</command> about them using the <option>-I</option> flag:
If you want <command>nix-env</command> to use the expressions in <replaceable>/my/sources</replaceable>, use <command>nix-env -f <replaceable>/my/sources</replaceable>/nixpkgs</command>, or change the default by adding a symlink in <filename>~/.nix-defexpr</filename>:
You may want to delete the symlink <filename>~/.nix-defexpr/channels_root</filename> to prevent root’s NixOS channel from clashing with your own tree (this may break the command-not-found utility though). If you want to go back to the default state, you may just remove the <filename>~/.nix-defexpr</filename> directory completely, log out and log in again and it should have been recreated with a link to the root channels.