Nix: Installing Nix in Docker on non-NixOS Linux fails: $USER is not set

Created on 15 Jul 2016  Â·  18Comments  Â·  Source: NixOS/nix

Issue description

Installation of Nix 1.11.2 on non-NixOS Linuxes inside Docker fails with:

/tmp/nix-binary-tarball-unpack.XXXXajokPC/unpack/nix-1.11.2-x86_64-linux/install: $USER is not set
The command '/bin/sh -c curl https://nixos.org/nix/install | sh' returned a non-zero code: 1

According to this login sets the $USER environment variable.

The most obvious workaround is to manually set the $USER var in your Dockerfile using: ENV USER tester.

Using id -u -n instead of the environment variable seems like a good idea at first glance. I'm willing to take a crack at it, if it seems reasonable by the maintainers.

Steps to reproduce

  1. Use fresh install
  2. Install dependencies bzip2 gnupg perl curl bash sudo
  3. Add user to sudo-group and sudoers
  4. Run curl https://nixos.org/nix/install | sh

    Technical details

I have tested this on Debian Jessie and Alpine 3.3 using Docker 1.10.3 on NixOS 16.03. See files below for a minimal example:

builld.sh

#!/usr/bin/env sh

set -e

echo "Building Alpine container"
docker build -f ./Dockerfile_alpine -t alpine-nix .

echo "Building Debian container"
docker build -f ./Dockerfile_debian -t debian-nix .

Dockerfile_alpine

FROM alpine:3.3

RUN apk add --update bzip2 gnupg perl curl bash sudo && \
    rm -rf /var/cache/apk/*

ENV LANG en_US.utf8
RUN adduser -h /home/tester -D -s /bin/bash tester
RUN addgroup sudo
RUN addgroup tester sudo
RUN echo " tester      ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers

WORKDIR /home/tester
USER tester

RUN curl https://nixos.org/nix/install | sh

Dockerfile_debian

FROM debian:jessie

RUN apt-get update --fix-missing && apt-get install \
    curl perl sudo locales bzip2 -y

RUN apt-get clean && apt-get purge && apt-get autoremove --purge -y && \
    rm -rf /var/lib/apt/lists/*

RUN useradd -m -d /home/tester -s /bin/bash tester
RUN usermod -a -G sudo tester
RUN echo " tester      ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers

RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8

WORKDIR /home/tester
USER tester

RUN curl https://nixos.org/nix/install | sh
installer

Most helpful comment

I wonder why you don't use https://hub.docker.com/r/nixos/nix/

All 18 comments

I wonder why you don't use https://hub.docker.com/r/nixos/nix/

@domenkozar Thanks for the tip! Will use that next time I'm testing a package build on non-NixOS Linux.

The reason why I opened this issue was because someone else might want to test Nix on their distro using the official install script and encounter this problem. Which could make them dismiss nix.

Maybe the scenario when $USER is not set is very unlikely, but I thought I'd open an issue to start a discussion.

any one is fixing this? I'm looking for a working nix env inside docker without using nixos.

based on the age of this, and that it continues to be a problem today, I'm going to assume that the official answer is that nix, the package manager not the os, shouldn't be run on other linux distros in docker.

I wouldn't assume that. I think it's more that with limited manpower in the community and extensive "batteries included" options, most of the people who might otherwise fix this haven't been affected by it. I definitely don't think anyone thinks this is the way it _should_ work.

..if that was the case, PRs that addressed the problem wouldn't have gone ignored until the PR maintainers gave up and left.

The single user installer is only intended for non root users, in which case I would expect USER to be configured even in a container. I'm not sure if this issue is a bad thing. It's possible to user the installer for a root/multi-user setup but that requires some extra configuration like nixbld users anyway. There are multiple images available that provide a working nix install:

FYI. the nixos/nix image is actually based on alpine.

Also, due to an injury I’ve been unable to dedicate appropriate amounts of time to the installer. I’ll be back and able to work on it again soon.

On Jul 24, 2018, at 2:44 PM, Daiderd Jordan notifications@github.com wrote:

The single user installer is only intended for non root users, in which case I would expect USER to be configured even in a container. I'm not sure if this issue is a bad thing. It's possible to user the installer for a root/multi-user setup but that requires some extra configuration like nixbld users anyway. There are multiple images available that provide a working nix install:

https://hub.docker.com/r/nixos/nix
https://hub.docker.com/r/nixorg/nix
https://github.com/LnL7/nix-docker
FYI. the nixos/nix image is actually based on alpine.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

I encountered this while trying to install nix within nvidia/cuda:9.2-cudnn7-devel-ubuntu16.04 (a preconfigured Ubuntu image for nvidia-docker with GPU support). I believe this is a good example of why one might want to install nix under a non-NixOS distro in Docker.

Adding similar evidence to this GitHub issue:

The https://gitpod.io produces the following image https://hub.docker.com/r/gitpod/workspace-full/ which can be overlayed to add support for new programming languages in the web based IDE. For example I'm interested in taking gitpod/workspace-full, shipping nix in there and then shipping the usual haskell stuff into https://github.com/theia-ide/haskell-theia-extension

@ghuntley here for the same reason. Hacked the following monster, but not sure this is optimal way to do it

RUN addgroup --system nixbld && \
  adduser --home /home/nix --disabled-password --gecos "" --shell /bin/bash nix && \
  adduser nix nixbld && \
  mkdir -m 0755 /nix && chown nix /nix && \
  mkdir -p /etc/nix && echo 'sandbox = false' > /etc/nix/nix.conf

CMD /bin/bash -l
USER nix
ENV USER nix
WORKDIR /home/nix

RUN touch .bash_profile && \
  curl https://nixos.org/nix/install | sh

RUN . /home/nix/.nix-profile/etc/profile.d/nix.sh && \
  nix-env -iA hies -f https://github.com/domenkozar/hie-nix/tarball/master

Lovely. Thank-you @stereobooster

FYI @stereobooster see https://github.com/Infinisil/all-hies which has replaced hie-nix

sadly both of @rzetterberg dockerfiles now require $USER as well

Until a satisfactory resolution can be found to this, I'd recommend that https://nixos.org/nix/manual/#chap-installation be changed from

Chapter 3. Supported Platforms
Nix is currently supported on the following platforms:

  • Linux (i686, x86_64, aarch64).
  • macOS (x86_64).

to say that Docker versions of those environments are not supported, something like:

Chapter 3. Supported Platforms
Nix is currently supported on the following platforms:

  • Linux (i686, x86_64, aarch64).
  • macOS (x86_64).

Nix cannot currently be used in Docker environments without some special tweaking, please see https://github.com/NixOS/nix/issues/971 for the details.

Might help draw attention to the issue from someone who has the time & knowhow to fix it too.

Open it @kenahoo. I agree.

PR created.

I'm a little bit surprised this isn't an issue in more places for Docker. Are these unset as well:

  • HOME
  • LOGNAME

?

Was this page helpful?
0 / 5 - 0 ratings