You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This Feature should work on recent versions of Debian/Ubuntu, RedHat Enterprise Linux, Fedora, RockyLinux, and Alpine Linux.
4
+
5
+
## Location of Flakes
6
+
7
+
Currently `flakeUri` works best with a remote URI (e.g., `github:nixos/nixpkgs/nixpkgs-unstable#hello`) as local files need to be in the image.
8
+
9
+
> Proposed support for lifecycle hooks in Features ([#60](https://github.com/devcontainers/spec/issues/60)) would allow for expressions files or Flakes to exist in the source tree to be automatically installed on initial container startup, but today you will have to manually add the appropriate install command to `postCreateCommand` to your `devcontainer.json` instead.
10
+
11
+
## Multi-user vs. single-user installs
12
+
13
+
This Dev Container Feature supports two installation models for Nix: multi-user and single user. Multi-user is the default, but each has pros and cons.
14
+
15
+
| Installation Model | Pros | Cons |
16
+
| --- | --- | --- |
17
+
|*Multi-User*| Nix can be used with any user including root.<br /><br />Also still works if the UID or GID of any user is updated. | Only works with Nix 2.11 and up due to a Nix installer limitation.<br /><br />Container must run either: run as root (but `remoteUser` in devcontainer.json can be non-root), or includes `sudo` with the `remoteUser` being configured to use it. <br /><br />Note that automated start of the `nix-daemon` requires passwordless `sudo` if the container itself (e.g., `containerUser`) is not running as root. Manual startup using `sudo` can require a password, however (more next). |
18
+
|*Single-User*| Does not require the container to run as root or `sudo` to be included in the image. | Only works with the user specified in the `userName` property or an auto-detected user. If this user's UID/GID is updated, that user will no longer be able to work with Nix. This is primarily a consideration when running on Linux where the UID/GID is sync'd to the local user. |
19
+
20
+
### Manually starting the Nix daemon
21
+
22
+
If you have `sudo` in your base image, but have a password set so automatic startup is not possible, you can manually start the Nix daemon by running the following command in a terminal:
23
+
24
+
```bash
25
+
sudo /usr/local/share/nix-entrypoint.sh
26
+
```
27
+
28
+
This same command can be used to restart the daemon if it has stopped for some reason. Logs are available at `/tmp/nix-daemon.log`.
# Do a multi or single-user setup based on feature config
70
+
if [ "${MULTIUSER}"="true" ];then
71
+
echo"(*) Performing multi-user install..."
72
+
sh "${tmpdir}/install-nix" --daemon
73
+
else
74
+
home_dir="$(eval echo~${USERNAME})"
75
+
if [ !-e"${home_dir}" ];then
76
+
echo"(!) Home directory ${home_dir} does not exist for ${USERNAME}. Nix install will fail."
77
+
exit 1
78
+
fi
79
+
echo"(*) Performing single-user install..."
80
+
echo -e "\n**NOTE: Nix will only work for user ${USERNAME} on Linux if the host machine user's UID is $(id -u ${USERNAME}). You will need to chown /nix otherwise.**\n"
81
+
# Install per https://nixos.org/manual/nix/stable/installation/installing-binary.html#single-user-installation
82
+
mkdir -p /nix
83
+
chown ${USERNAME} /nix ${tmpdir}
84
+
su ${USERNAME} -c "sh \"${tmpdir}/install-nix\" --no-daemon --no-modify-profile"
85
+
# nix installer does not update ~/.bashrc, and USER may or may not be defined, so update rc/profile files directly to handle that
86
+
snippet='
87
+
if [ "${PATH#*$HOME/.nix-profile/bin}" = "${PATH}" ]; then if [ -z "$USER" ]; then USER=$(whoami); fi; . $HOME/.nix-profile/etc/profile.d/nix.sh; fi
sudo -n sh -c '. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh; /nix/var/nix/profiles/default/bin/nix-daemon > /tmp/nix-daemon.log 2>&1'&
13
+
if [ "$?"="0" ];then
14
+
start_ok=true
15
+
fi
16
+
fi
17
+
if [ "${start_ok}"="false" ];then
18
+
echo -e 'Failed to start nix-daemon as root. Set multiUser to false in your feature configuraiton if you would\nprefer to run the container as a non-root. You may also start the daemon manually if you have sudo\ninstalled and configured for your user by running "sudo -c nix-daemon &"'
0 commit comments