Nix local overlay store in docker container

I have been trying to implement nix local overlay store in a docker container, i have done nix single-user installation in dockerfile, it looks like this

FROM ubuntu:latest
RUN apt-get update &&
apt-get install -y sudo xz-utils curl vim &&
rm -rf /var/lib/apt/lists/*
RUN groupadd -g 1024 valnix &&
useradd -u 1024 -m -g valnix -s /bin/bash valnix &&
echo ‘valnix ALL=(ALL) NOPASSWD: ALL’ >> /etc/sudoers

RUN curl -L https://nixos.org/nix/install -o install_nix.sh
RUN chmod +x install_nix.sh

USER valnix

RUN sh install_nix.sh --no-daemon
ENV PATH=“{$PATH}:/home/valnix/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/bin”

when i run the container nix installation works,
docker run --privileged -v /nix/store:/nix/store-host -it image_name (/nix/store has several packages installed like python3)

For the local overlay store, i do the following steps inside the container

  1. mkdir -p /nix/store-host/upper /nix/store-host/workdir
  2. sudo mkdir -p /etc/nix && sudo vi /etc/nix/nix.conf
  3. In nix.conf
    experimental-features = nix-command flakes local-overlay-store read-only-local-store
    store = local-overlay?lower-store=/nix/store&upper-layer=/nix/store-host/upper&check-mount=false
  4. sudo mount -t overlay overlay -o lowerdir=/nix/store,upperdir=/nix/store-host/upper,workdir=/nix/store-host/workdir “/nix/store”

When i do nix-shell or nix-env, for example (nix-shell -p python3) it will start installing from cache.nixos.org ( copying path ‘/nix/store/pgb120fb7srbh418v4i2a70aq1w9dawd-python3-3.12.5’ from ‘https://cache.nixos.org’ to ‘local-overlay://’… )

FYI, python3 was available in my host machine /nix/store

Expected is, when i do nix-shell -p python3 it shouldn’t be installed from “https://cache.nixos.org

Please help me on this, let me know if i am doing something wrong

1 Like

Maybe try doing what would look like a multi user installation, mounting the whole of /nix including the daemon in the docker? It seems that this would inter-operate better if there are several processes using the nix store.

See this by someone who achieved that:
https://www.reddit.com/r/NixOS/comments/10q9db1/shared_nix_store_between_docker_containers/