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:
  /nix/store/dp45mpkl1dj4d5f44xpvqmgr67a6cf2k-docker-layer-production.drv
  /nix/store/sfzj5hxcjajr849apxchx9mcw8j1cgs0-docker-layer-production.drv
  /nix/store/c6rx57ry3x20frxxabf7qfxdp8ds0kc4-runtime-deps.drv
  /nix/store/w8yx2pi1y5b8n1d2y9myj624qpb9ivgd-docker-image-production.tar.gz.drv
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!