Skip to content

Commit 302feca

Browse files
max06Flo Müllersamruddhikhandale
authored
feat(nix): Enable persistent volume for shared nix-store (devcontainers#1127)
* feat(nix): Enable persistent volume for shared nix-store * fix: Wrong mount * fix: Switch to persistent volumes per devcontainer, fixes renamed nix package used in test * Update src/nix/devcontainer-feature.json Co-authored-by: Samruddhi Khandale <[email protected]> * fix: Bump version --------- Co-authored-by: Flo Müller <[email protected]> Co-authored-by: Samruddhi Khandale <[email protected]>
1 parent 5077a2c commit 302feca

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
lines changed

src/nix/devcontainer-feature.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{
22
"id": "nix",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"name": "Nix Package Manager",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/nix",
66
"description": "Installs the Nix package manager and optionally a set of packages.",
77
"options": {
88
"version": {
99
"type": "string",
10-
"proposals": ["latest", "2.11"],
10+
"proposals": [
11+
"latest",
12+
"2.11"
13+
],
1114
"default": "latest",
1215
"description": "Version of Nix to install."
1316
},
@@ -43,5 +46,12 @@
4346
"containerEnv": {
4447
"PATH": "/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:${PATH}"
4548
},
49+
"mounts": [
50+
{
51+
"source": "nix-store-${devcontainerId}",
52+
"target": "/nix",
53+
"type": "volume"
54+
}
55+
],
4656
"entrypoint": "/usr/local/share/nix-entrypoint.sh"
4757
}

src/nix/install.sh

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,67 +23,63 @@ fi
2323

2424
detect_user USERNAME
2525

26-
if [ -e "/nix" ]; then
27-
echo "(!) Nix is already installed! Skipping installation."
28-
else
29-
if [ "${USERNAME}" = "root" ] && [ "${MULTIUSER}" != "true" ]; then
30-
echo "(!) A single user install is not allowed for root. Add a non-root user to your image or set multiUser to true in your feature configuration."
31-
exit 1
32-
fi
26+
if [ "${USERNAME}" = "root" ] && [ "${MULTIUSER}" != "true" ]; then
27+
echo "(!) A single user install is not allowed for root. Add a non-root user to your image or set multiUser to true in your feature configuration."
28+
exit 1
29+
fi
3330

34-
# Verify dependencies
35-
apt_get_update_if_exists
36-
check_command curl "curl ca-certificates" "curl ca-certificates" "curl ca-certificates"
37-
check_command gpg2 gnupg2 gnupg gnupg2
38-
check_command dirmngr dirmngr dirmngr dirmngr
39-
check_command xz xz-utils xz xz
40-
check_command git git git git
41-
check_command xargs findutils findutils findutils
31+
# Verify dependencies
32+
apt_get_update_if_exists
33+
check_command curl "curl ca-certificates" "curl ca-certificates" "curl ca-certificates"
34+
check_command gpg2 gnupg2 gnupg gnupg2
35+
check_command dirmngr dirmngr dirmngr dirmngr
36+
check_command xz xz-utils xz xz
37+
check_command git git git git
38+
check_command xargs findutils findutils findutils
4239

43-
# Determine version
44-
find_version_from_git_tags VERSION https://github.com/NixOS/nix "tags/"
40+
# Determine version
41+
find_version_from_git_tags VERSION https://github.com/NixOS/nix "tags/"
4542

46-
# Download and verify install per https://nixos.org/download.html#nix-verify-installation
47-
tmpdir="$(mktemp -d)"
48-
echo "(*) Downloading Nix installer..."
49-
set +e
43+
# Download and verify install per https://nixos.org/download.html#nix-verify-installation
44+
tmpdir="$(mktemp -d)"
45+
echo "(*) Downloading Nix installer..."
46+
set +e
47+
curl -sSLf -o "${tmpdir}/install-nix" https://releases.nixos.org/nix/nix-${VERSION}/install
48+
exit_code=$?
49+
set -e
50+
if [ "$exit_code" != "0" ]; then
51+
# Handle situation where git tags are ahead of what was is available to actually download
52+
echo "(!) Nix version ${VERSION} failed to download. Attempting to fall back one version to retry..."
53+
find_prev_version_from_git_tags VERSION https://github.com/NixOS/nix "tags/"
5054
curl -sSLf -o "${tmpdir}/install-nix" https://releases.nixos.org/nix/nix-${VERSION}/install
51-
exit_code=$?
52-
set -e
53-
if [ "$exit_code" != "0" ]; then
54-
# Handle situation where git tags are ahead of what was is available to actually download
55-
echo "(!) Nix version ${VERSION} failed to download. Attempting to fall back one version to retry..."
56-
find_prev_version_from_git_tags VERSION https://github.com/NixOS/nix "tags/"
57-
curl -sSLf -o "${tmpdir}/install-nix" https://releases.nixos.org/nix/nix-${VERSION}/install
58-
fi
59-
cd "${FEATURE_DIR}"
55+
fi
56+
cd "${FEATURE_DIR}"
6057

61-
# Do a multi or single-user setup based on feature config
62-
if [ "${MULTIUSER}" = "true" ]; then
63-
echo "(*) Performing multi-user install..."
64-
sh "${tmpdir}/install-nix" --daemon
65-
else
66-
home_dir="$(eval echo ~${USERNAME})"
67-
if [ ! -e "${home_dir}" ]; then
68-
echo "(!) Home directory ${home_dir} does not exist for ${USERNAME}. Nix install will fail."
69-
exit 1
70-
fi
71-
echo "(*) Performing single-user install..."
72-
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"
73-
# Install per https://nixos.org/manual/nix/stable/installation/installing-binary.html#single-user-installation
74-
mkdir -p /nix
75-
chown ${USERNAME} /nix ${tmpdir}
76-
su ${USERNAME} -c "sh \"${tmpdir}/install-nix\" --no-daemon --no-modify-profile"
77-
# nix installer does not update ~/.bashrc, and USER may or may not be defined, so update rc/profile files directly to handle that
78-
snippet='
79-
if [ "${PATH#*$HOME/.nix-profile/bin}" = "${PATH}" ]; then if [ -z "$USER" ]; then USER=$(whoami); fi; . $HOME/.nix-profile/etc/profile.d/nix.sh; fi
80-
'
81-
update_rc_file "$home_dir/.bashrc" "${snippet}"
82-
update_rc_file "$home_dir/.zshenv" "${snippet}"
83-
update_rc_file "$home_dir/.profile" "${snippet}"
58+
# Do a multi or single-user setup based on feature config
59+
if [ "${MULTIUSER}" = "true" ]; then
60+
echo "(*) Performing multi-user install..."
61+
sh "${tmpdir}/install-nix" --daemon
62+
else
63+
home_dir="$(eval echo ~${USERNAME})"
64+
if [ ! -e "${home_dir}" ]; then
65+
echo "(!) Home directory ${home_dir} does not exist for ${USERNAME}. Nix install will fail."
66+
exit 1
8467
fi
85-
rm -rf "${tmpdir}" "/tmp/tmp-gnupg"
68+
echo "(*) Performing single-user install..."
69+
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"
70+
# Install per https://nixos.org/manual/nix/stable/installation/installing-binary.html#single-user-installation
71+
mkdir -p /nix
72+
chown ${USERNAME} /nix ${tmpdir}
73+
su ${USERNAME} -c "sh \"${tmpdir}/install-nix\" --no-daemon --no-modify-profile"
74+
# nix installer does not update ~/.bashrc, and USER may or may not be defined, so update rc/profile files directly to handle that
75+
snippet='
76+
if [ "${PATH#*$HOME/.nix-profile/bin}" = "${PATH}" ]; then if [ -z "$USER" ]; then USER=$(whoami); fi; . $HOME/.nix-profile/etc/profile.d/nix.sh; fi
77+
'
78+
update_rc_file "$home_dir/.bashrc" "${snippet}"
79+
update_rc_file "$home_dir/.zshenv" "${snippet}"
80+
update_rc_file "$home_dir/.profile" "${snippet}"
8681
fi
82+
rm -rf "${tmpdir}" "/tmp/tmp-gnupg"
8783

8884
# Set nix config
8985
mkdir -p /etc/nix

test/nix/scenarios.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
}
9292
}
9393
},
94-
9594
"flake": {
9695
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
9796
"remoteUser": "vscode",

0 commit comments

Comments
 (0)