# NixOS on unstable - How to fix a broken nixos-rebuild

From time to time it can happen that nixos-rebuild fails due to a package build error.

Here is how to fix this.

So your nixos-rebuild tells you something like this:

building Nix...
building the system configuration...
these derivations will be built:
/nix/store/<hash>-libvterm-v2015-02-23-src.drv
/nix/store/<hash>-neovim-libvterm-2015-02-23.drv
/nix/store/<hash>-neovim-0.1.0.drv
/nix/store/<hash>-neovim-0.1.0-configured.drv
/nix/store/<hash>-system-path.drv
/nix/store/<hash>-dbus-conf.drv
/nix/store/<hash>-unit-dbus.service.drv
/nix/store/<hash>-unit-polkit.service.drv
/nix/store/<hash>-system-units.drv
/nix/store/<hash>-etc.drv
/nix/store/<hash>-nixos-system-yuu-16.03pre71289.7ae05ed.drv
building path(s) ‘/nix/store/<hash>-libvterm-v2015-02-23-src’
builder for ‘/nix/store/<hash>-libvterm-v2015-02-23-src.drv’ failed with exit code 1
cannot build derivation ‘/nix/store/<hash>-neovim-libvterm-2015-02-23.drv’: 1 dependencies couldn't be built
cannot build derivation ‘/nix/store/<hash>-neovim-0.1.0.drv’: 1 dependencies couldn't be built
cannot build derivation ‘/nix/store/<hash>-neovim-0.1.0-configured.drv’: 1 dependencies couldn't be built
cannot build derivation ‘/nix/store/<hash>-system-path.drv’: 1 dependencies couldn't be built
cannot build derivation ‘/nix/store/<hash>-dbus-conf.drv’: 1 dependencies couldn't be built
cannot build derivation ‘/nix/store/<hash>-unit-polkit.service.drv’: 1 dependencies couldn't be built
cannot build derivation ‘/nix/store/<hash>-unit-dbus.service.drv’: 1 dependencies couldn't be built
cannot build derivation ‘/nix/store/<hash>-system-units.drv’: 2 dependencies couldn't be built
cannot build derivation ‘/nix/store/<hash>-etc.drv’: 2 dependencies couldn't be built
cannot build derivation ‘/nix/store/<hash>-nixos-system-yuu-16.03pre71289.7ae05ed.drv’: 2 dependencies couldn't be built
error: build of ‘/nix/store/<hash>-nixos-system-yuu-16.03pre71289.7ae05ed.drv’ failed


And you don’t know what to do? Well, there’s a solution to that, and the solution is rather simple, actually.

# Step 1: Find the broken package

If the break happens due to an error in your configuration.nix, you should fix this error. Then you don’t need to go on reading here.

If the build fails due to an error in the nixpkgs, as above, you should continue reading.

So we have to find the broken package. From the backtrace nixos-rebuild shows us, we can find out that the break happens due to an error in the libvterm package. This is clearly an error in the nixpkgs and we can fix this error only with the nixpkgs available, so…

# Step 2: Get nixpkgs

If you havn’t already cloned the nixpkgs repository, you need to do this first. You also need to fetch the unstable channel commit, which is provided by the nixpkgs-channels repository:

git clone https://github.com/nixos/nixpkgs && cd nixpkgs


# Step 3: Check out the commit of your system

Now you should have a commit which shows the current unstable channel. You can check this like so:

nixos-version # 16.03.git.ab0a7e9 (Emu)
git show nixos-unstable # commit ab0a7e9


If the commit is the same as the hash which is shown from calling nixos-version you have found the channel commit your system is build from. You can now check this commit out and create a new branch in the nixpkgs from it.

git checkout ab0a7e9
git checkout -b nixos-unstable-fixes


# Step 4: Search for fixes.

Sometimes there are already fixes available in the master branch of nixpkgs, but are not in unstable yet. You can simply do something like this:

1. Find out where the broken package lives
2. Get the log from the current unstable channel commit to master
3. Apply these fixes in your -fixes branch
4. Rebuild the system with the local nixpkgs

If there are no fixes available, you have to fix the build yourself and I cannot help you with that, because the build problems are often very package-specific.

We already know that libvterm is the problem here.

grep -rnil libvterm pkgs/
# or 'ag -l libvterm pkgs', which is much faster


This gives us four possible locations:

pkgs/top-level/all-packages.nix
pkgs/applications/window-managers/vwm/default.nix
pkgs/applications/editors/neovim/default.nix
pkgs/development/libraries/libvterm/default.nix


We also know (from the backtrace) that it has something to do with neovim. So we check the changes of this one first:

git log --oneline nixos-unstable..master -- pkgs/applications/editors/neovim/default.nix


Well, we found a fix. Let’s apply it!

git checkout nixos-unstable-fixes # just to be sure
git cherry-pick 2617919


Now we can rebuild our system with this patch applied:

sudo nixos-rebuild switch -I nixpkgs=/home/user/nixpkgs/


… and our system is updated. Now you can try to update packages the same way using the diff from nixos-unstable to master and apply the appropriate patches onto your -fixes branch. After a channel update you can simply rebase the -fixes branch onto the new channel commit. If rebase fails due to conflicts (which shouldn’t happen that often) you can just hard-reset your -fixes branch onto the unstable channel commit and re-apply patches as needed.