Cannot build Docker image

I tried to use a Docker image using Nix. My first attempt was inside nixos/nix image on top of Docker for Mac. First I ran into this error:

building '/nix/store/y9qc5ib6lj1wvmyvx40pjs38bkmf18jy-vm-run-stage2.drv'...
building '/nix/store/xd8xjrl16bni0n5vin09vl2cfgf09bvq-vm-run.drv'...
error: a 'x86_64-linux' with features {kvm} is required to build '/nix/store/7ysysln9wkprbsqkcls1vhc7gx1az96x-docker-layer-production.drv', but I am a 'x86_64-linux' with features {benchmark,
 big-parallel, nixos-test}

Following a very similar problem on GH, I added system-features = kvm in nix.conf. It brought the build a little bit further, but then I hit very similar one:

$ nix-build docker.nix
these derivations will be built:
building '/nix/store/dp45mpkl1dj4d5f44xpvqmgr67a6cf2k-docker-layer-production.drv'...
Formatting '/nix/store/xii055jm54g7q1lw3ldk28kg1g5c4grg-docker-layer-production/disk-image.qcow2', fmt=qcow2 size=1073741824 cluster_size=65536 lazy_refcounts=off refcount_bits=16
qemu-system-x86_64: CPU model 'host' requires KVM

I thought it has something to do with Darwin underneath, so I tried to do it on AWS NixOS image, but ran into exactly same error.

Are there any workarounds? Also, am I right that it is impossible to use dockerTools.buildImage using macOS and Nix?

1 Like

Normally kvm is set when /dev/kvm is present.

dockerTools can start qemu (with KVM) as part of the build. AFAIK this happens when you use exportImage or the runAsRoot option.

Is the kvm module loaded on the VM where you are trying to build the Docker image? (Does it support KVM/nested virtualization?)

1 Like

Hi @danieldk,

First of all, thanks - I indeed had runAsRoot and I managed to build my image once I removed it (though I’m bit puzzled why runAsRoot required KVM).

I’m also not super familiar with virtualization tech (know nothing about it), but it doesn’t seem that either of my machines had it. I checked it out and it seems there are plenty of docs on how to enable it on both Docker and EC2, but don’t think I’ll need it short term.

Thanks again!

Hi - just to be sure:
Is the solution just to ditch the runAsRoot option completely ?

I ran into a similar issue, when i tried to build a docker image on my VPS.

Okay, sorry, I overread the system-features = kvm part.
But anyway that leads me to another question:
The VPS machine does not have /dev/kvm present - does that mean kvm is not working ?
If so, why is the docker image building properly after overriding the systemFeatures in nix.conf ?

1 Like

I am/was having the same issue. Nix is installed in RHEL7 which is a virtual machine (VMWare) I have no control over. There is no /dev/kvm. I started getting this error when I added runAsRoot (to add a non-root user id to the container) to the docker image declaration. I added the system-features = kvm workaround as advised above and the error is now gone. But I am getting a different error now. As the process is adding the first component to the container it runs of of space. This didn’t happen before I added the runAsRoot part and the whole process is taking a lot longer now.

Executing pre-mount steps...
Adding contents...
Adding /nix/store/EDITED...
rsync: write failed on "/tmp/disk/layer/SOME_FILE": No space left on device (28)
rsync error: error in file IO (code 11) at receiver.c(374) [receiver=3.1.3]
[  481.925325] reboot: Power down

The system has plenty of space. What may be causing the error? A slightly related question: can I add the user to the container without runAsRoot?


I am having the same issue too. I’m trying to run buildImage into the docker container (image nixos/nix:2.11.1) in the GitLab CI pipeline.

error: a 'x86_64-linux' with features {kvm} is required to build '/nix/store/c36j7rn8nx5mycb5awqxwsq97q3iyyh2-docker-layer-etl-traxxall-shell-ci.drv', but I am a 'x86_64-linux' with features {benchmark, big-parallel, nixos-test, uid-range}

If I understand correctly, the only way to solve this issue is to enable /dev/kvm in the gitlab-runner. Am I right?