Getting to play with Nix on Windows using Docker

Daniel Díaz Carrete
2 min readJun 14, 2019

Prompted by this post, I decided to learn the rudiments of the Nix package manager. The problem is that I’m on a Windows machine, and Nix is mainly for Linux and macOS .

So I turned to Docker, more precisely to the nixos/nix image:

PS> docker pull nixos/nix
PS> docker run --rm -ti nixos/nix
9cf6b3be9ed6:/# nix-env --install --file '<nixpkgs>' -A ghc

This works well as a first step, but the problem of course is that all installed packages evaporate when I kill the Docker container.

So let’s create two names volumes (one for persisting /root, the other for persisting the /nix directory which contains the Nix store and other important stuff) and mount them every time we launch the container:

PS> docker volume create nix
PS> docker volume create nix_root
PS> docker run --rm -ti --mount=type=volume,source=nix,target=/nix --mount=type=volume,source=nix_root,target=/root nixos/nix

But wait. Those folders inside the container have already lots of stuff in them. What happens when we mount the volumes over them? Aren’t we going to make the container unusable?

As it happens, no. It’s a (not very well documented, last time I checked) behaviour of Docker volumes that when they are initially empty and they are mounted over a folder that already has files, those files are copied from the container to the volumes. The contents of the volumes will overshadow the original folder contents from then on.

Having to invoke that complex docker run command every time is a chore, so it’s better to put all in a docker-compose.yml file:

version: '3.7'
services:
nixos:
image: "nixos/nix:latest"
volumes:
- nix:/nix
- nix_root:/root
volumes:
nix:
nix_root:

And run it interactively like this:

docker-compose run --rm nixos

Remember also to clean up afterwards:

docker-compose down

Note that docker-compose will give prefixes to the names of created volumes, which is surprising at first but seems to be normal behaviour.

--

--