Error with building a Dockerfile for Holochain in a Nix Shell: error: opening lock file '/home/docker/.nix-profile.lock': Permission denied

I have this Dockerfile:

FROM manjarolinux/base:latest

# make sure image is up-to-date
# linux docker does not ship with much; update repositories
# and install curl, tar, sudo if needed
# output has warnings:
# warning: dependency cycle detected:
# warning: bashrc-manjaro will be installed before its bash dependency
RUN pacman -Syuu --noconfirm --needed curl tar sudo

# nix does not work under root
# add a docker user that can sudo
RUN useradd docker
RUN groupadd -r sudo
RUN gpasswd -a docker sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# nix expects the nixbld group
RUN groupadd -r nixbld
RUN gpasswd -a docker nixbld

# keep this matching nix-shell! (This may not be needed.)
# https://discourse.nixos.org/t/warning-nix-search-path-entry-nix-var-nix-profiles-per-user-root-channels-does-not-exist-ignoring/5170/4
ENV NIX_PATH nixpkgs=channel:nixos-19.09

# sandbox may not play nice with manjaro (at least in docker)
RUN mkdir -p /etc/nix && echo 'sandbox = false' > /etc/nix/nix.conf

RUN cat /etc/passwd

# prevents the output of the line "sudo: setrlimit(RLIMIT_CORE): Operation not permitted" to sudo commands
# From https://ask.fedoraproject.org/t/sudo-setrlimit-rlimit-core-operation-not-permitted/4223
RUN touch /etc/sudo.conf
RUN echo 'Set disable_coredump false' > /etc/sudo.conf

# use the docker user
USER docker
ENV USER docker
WORKDIR /home/docker

# https://nixos.wiki/wiki/Nix_Installation_Guide#Single-user_install
RUN sudo install -d -m755 -o $(id -u) -g $(id -g) /nix

# warning: Nix search path entry '/home/docker/.nix-defexpr/channels' does not exist, ignoring
# https://discourse.nixos.org/t/warning-nix-search-path-entry-nix-var-nix-profiles-per-user-root-channels-does-not-exist-ignoring/5170/4
# error: opening lock file '/home/docker/.nix-profile.lock': Permission denied
# /tmp/nix-binary-tarball-unpack.EIdivHyL1l/unpack/nix-2.3.1-x86_64-linux/install: unable to install Nix into your default profile
RUN curl https://nixos.org/nix/install | sh

# warm nix and avoid warnings about missing channels
# https://github.com/NixOS/nixpkgs/issues/40165
RUN . /home/docker/.nix-profile/etc/profile.d/nix.sh; \
nix-channel --update; \
nix-shell https://holochain.love --run echo

When I run sudo docker build -t holochain/holonix:manjaro . while cd'd in the folder of this file, I get this output with an error:

$ sudo docker build -t holochain/holonix:manjaro .
Sending build context to Docker daemon  3.584kB
Step 1/17 : FROM jonathonf/manjaro:latest
 ---> 89c8a82b8804
Step 2/17 : RUN pacman -Syuu --noconfirm --needed curl tar sudo
 ---> Using cache
 ---> 8b9367f8e555
Step 3/17 : RUN useradd docker
 ---> Using cache
 ---> 501b1b584152
Step 4/17 : RUN groupadd -r sudo
 ---> Using cache
 ---> 1da1e5ae7a09
Step 5/17 : RUN gpasswd -a docker sudo
 ---> Using cache
 ---> c3b9449d3227
Step 6/17 : RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
 ---> Using cache
 ---> e1c93d217490
Step 7/17 : RUN groupadd -r nixbld
 ---> Using cache
 ---> 5b017c4c77ef
Step 8/17 : RUN gpasswd -a docker nixbld
 ---> Using cache
 ---> 96898346b75b
Step 9/17 : ENV NIX_PATH nixpkgs=channel:nixos-19.09
 ---> Using cache
 ---> 7d8caac8163e
Step 10/17 : RUN mkdir -p /etc/nix && echo 'sandbox = false' > /etc/nix/nix.conf
 ---> Using cache
 ---> 242607961a8e
Step 11/17 : RUN cat /etc/passwd
 ---> Using cache
 ---> 33dae95384e8
Step 12/17 : USER docker
 ---> Using cache
 ---> e7bf6e1366a1
Step 13/17 : ENV USER docker
 ---> Using cache
 ---> 378f2b3698b2
Step 14/17 : WORKDIR /home/docker
 ---> Using cache
 ---> 1ab9315c9295
Step 15/17 : RUN sudo install -d -m755 -o $(id -u) -g $(id -g) /nix
 ---> Using cache
 ---> 61d367cdd697
Step 16/17 : RUN curl https://nixos.org/nix/install | sh
 ---> Running in 150eacf5e2d3
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2399  100  2399    0     0   2111      0  0:00:01  0:00:01 --:--:--  2111
downloading Nix 2.3.1 binary tarball for x86_64-linux from 'https://nixos.org/releases/nix/nix-2.3.1/nix-2.3.1-x86_64-linux.tar.xz' to '/tmp/nix-binary-tarball-unpack.4UvtUVMpy3'...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15.2M  100 15.2M    0     0  1949k      0  0:00:07  0:00:07 --:--:-- 2826k
performing a single-user installation of Nix...
copying Nix to /nix/store................................
installing 'nix-2.3.1'
warning: Nix search path entry '/home/docker/.nix-defexpr/channels' does not exist, ignoring
building '/nix/store/cb6ss4jv8baxkq2pi6fnviqp9zyna73l-user-environment.drv'...
created 6 symlinks in user environment
error: opening lock file '/home/docker/.nix-profile.lock': Permission denied
/tmp/nix-binary-tarball-unpack.4UvtUVMpy3/unpack/nix-2.3.1-x86_64-linux/install: unable to install Nix into your default profile
The command '/bin/sh -c curl https://nixos.org/nix/install | sh' returned a non-zero code: 1

Currently I’m trying to figure out how to fix the error, and any assistance is appreciated.

Cross-posted at https://forum.manjaro.org/t/draft-dockerfile-for-manjaro-to-build-holonix-holochain-dev-env-in-a-nix-shell/116892/3

I think you’re coming at this from the wrong (although canonical) prospective. Nix already has a lot of great tools for creating minimal docker images using nixpkgs.

If you want a more typical Dockerfile “FROM blah:foo” approach, then check out LnL’s premade nix docker images

nix is already a powerful tool for packaging and distributing software, no need to fight with it.

The context of the Dockerfile is using it in CircleCI for Holochain, in order to try to reproduce warnings occuring in the below issue, as well as being useful for testing how Nix builds in different operating systems.

I’ll look into your links, however.

https://github.com/holochain/holochain-rust/issues/1789

@jamesray1 what does RUN ls -la /home/docker output if you put it just before the nix installation?

My guess is that it either doesn’t exist or is not owned by the docker user.