Is there a `docker buildx` command on NixOS?

Hi!
I was reading that in order to build docker images for different architectures, there is the “docker buildx” command that uses the moby/buildkit (that’s how I understood, I’m new to this…). I was wondering if this is available on NixOS? I tried with DOCKER_CLI_EXPERIMENTAL which didn’t work and I couldn’t find info for NixOS regarding the buildx command.

Thanks in advance for hints and tips!
Regards
Eike

I build it now as described here: GitHub - docker/buildx: Docker CLI plugin for extended build capabilities with BuildKit

export DOCKER_BUILDKIT=1
docker build --platform=local -o . git://github.com/docker/buildx
mkdir -p ~/.docker/cli-plugins
mv buildx ~/.docker/cli-plugins/docker-buildx

Yes.

It’s disabled by default though. To enable it you just need to override the attribute buildxSupport and set it to true. You also need to be on 21.05 or unstable, as the option had not yet been added as of 20.09’s release.

Example:

buildInputs = [
  # other stuff
  (docker.override(args: { buildxSupport = true; }))
  # other stuff
];

I was reading that in order to build docker images for different architectures, there is the “docker buildx” command that uses the moby/buildkit (that’s how I understood, I’m new to this…). I was wondering if this is available on NixOS?

The “nix way” to doing this is to use the pkgsCross target platforms:

$ nix build .#pkgsCross.aarch64-multiplatform.hello
$ file ./result/bin/hello
./result/bin/hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /nix/store/ichh39fkp51jjflzimrl46lnl3266c9y-glibc-aarch64-unknown-linux-gnu-2.32-46/lib/ld-linux-aarch64.so.1, for GNU/Linux 2.6.32, not stripped

Yes

$ nix-shell -p docker-buildx
this path will be fetched (8.52 MiB download, 44.11 MiB unpacked):
  /nix/store/17x68fv1nxx65j7hqndisbb86l8sli4f-docker-buildx-0.5.1
copying path '/nix/store/17x68fv1nxx65j7hqndisbb86l8sli4f-docker-buildx-0.5.1' from 'https://cache.nixos.org'...

[nix-shell:/home/jon/projects/nixpkgs]$ docker buildx --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers
...

If you’re on nixos, you should probably have virtualisation.docker.enable to have the docker daemon available.

2 Likes

Oh great, thank you so much! Added this to my systemPackages now.

Thank you! This can be used to cross-build nix derviations for a different architecture?

I’m not sure I understand :confused: - if I paste this nix build .#pkgsCross… into my terminal it doesn’t understand this (don't know what to do with argument). Is there a typo maybe; or does my brain left me somewhere (sorry)? :upside_down_face:

And for some reason I wasn’t able to lookup the docker-buildx - I’m sorry. Now I can easily find it using nix search. I don’t know why I didn’t find it in the first place…!

Oh hell, I thought I was being so careful too.

@eikek mark jonringer’s response as the solution please. I didn’t see docker-buildx because I haven’t updated my nix search cache since I updated to 21.05.

That’s because I’m using flakes. nix 1.0 command would be:

nix-build '<nixpkgs>' -A pkgsCross.aarch64-multiplatform.hello
1 Like

Thank you both for your help! I think it’s time to read up on flakes, I sadly haven’t so far. I wanted to mark both answers, because they both work for me :slight_smile: