Unverified Commit 302fecab authored by Flo's avatar Flo Committed by GitHub

feat(nix): Enable persistent volume for shared nix-store (#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: 's avatarSamruddhi Khandale <samruddhikhandale@github.com>

* fix: Bump version

---------
Co-authored-by: 's avatarFlo Müller <flo.mueller@gec.io>
Co-authored-by: 's avatarSamruddhi Khandale <samruddhikhandale@github.com>
parent 5077a2cb
{ {
"id": "nix", "id": "nix",
"version": "1.2.0", "version": "1.3.0",
"name": "Nix Package Manager", "name": "Nix Package Manager",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/nix", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/nix",
"description": "Installs the Nix package manager and optionally a set of packages.", "description": "Installs the Nix package manager and optionally a set of packages.",
"options": { "options": {
"version": { "version": {
"type": "string", "type": "string",
"proposals": ["latest", "2.11"], "proposals": [
"latest",
"2.11"
],
"default": "latest", "default": "latest",
"description": "Version of Nix to install." "description": "Version of Nix to install."
}, },
...@@ -43,5 +46,12 @@ ...@@ -43,5 +46,12 @@
"containerEnv": { "containerEnv": {
"PATH": "/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:${PATH}" "PATH": "/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:${PATH}"
}, },
"mounts": [
{
"source": "nix-store-${devcontainerId}",
"target": "/nix",
"type": "volume"
}
],
"entrypoint": "/usr/local/share/nix-entrypoint.sh" "entrypoint": "/usr/local/share/nix-entrypoint.sh"
} }
...@@ -23,67 +23,63 @@ fi ...@@ -23,67 +23,63 @@ fi
detect_user USERNAME detect_user USERNAME
if [ -e "/nix" ]; then if [ "${USERNAME}" = "root" ] && [ "${MULTIUSER}" != "true" ]; then
echo "(!) Nix is already installed! Skipping installation." 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."
else exit 1
if [ "${USERNAME}" = "root" ] && [ "${MULTIUSER}" != "true" ]; then fi
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."
exit 1
fi
# Verify dependencies # Verify dependencies
apt_get_update_if_exists apt_get_update_if_exists
check_command curl "curl ca-certificates" "curl ca-certificates" "curl ca-certificates" check_command curl "curl ca-certificates" "curl ca-certificates" "curl ca-certificates"
check_command gpg2 gnupg2 gnupg gnupg2 check_command gpg2 gnupg2 gnupg gnupg2
check_command dirmngr dirmngr dirmngr dirmngr check_command dirmngr dirmngr dirmngr dirmngr
check_command xz xz-utils xz xz check_command xz xz-utils xz xz
check_command git git git git check_command git git git git
check_command xargs findutils findutils findutils check_command xargs findutils findutils findutils
# Determine version # Determine version
find_version_from_git_tags VERSION https://github.com/NixOS/nix "tags/" find_version_from_git_tags VERSION https://github.com/NixOS/nix "tags/"
# Download and verify install per https://nixos.org/download.html#nix-verify-installation # Download and verify install per https://nixos.org/download.html#nix-verify-installation
tmpdir="$(mktemp -d)" tmpdir="$(mktemp -d)"
echo "(*) Downloading Nix installer..." echo "(*) Downloading Nix installer..."
set +e set +e
curl -sSLf -o "${tmpdir}/install-nix" https://releases.nixos.org/nix/nix-${VERSION}/install
exit_code=$?
set -e
if [ "$exit_code" != "0" ]; then
# Handle situation where git tags are ahead of what was is available to actually download
echo "(!) Nix version ${VERSION} failed to download. Attempting to fall back one version to retry..."
find_prev_version_from_git_tags VERSION https://github.com/NixOS/nix "tags/"
curl -sSLf -o "${tmpdir}/install-nix" https://releases.nixos.org/nix/nix-${VERSION}/install curl -sSLf -o "${tmpdir}/install-nix" https://releases.nixos.org/nix/nix-${VERSION}/install
exit_code=$? fi
set -e cd "${FEATURE_DIR}"
if [ "$exit_code" != "0" ]; then
# Handle situation where git tags are ahead of what was is available to actually download
echo "(!) Nix version ${VERSION} failed to download. Attempting to fall back one version to retry..."
find_prev_version_from_git_tags VERSION https://github.com/NixOS/nix "tags/"
curl -sSLf -o "${tmpdir}/install-nix" https://releases.nixos.org/nix/nix-${VERSION}/install
fi
cd "${FEATURE_DIR}"
# Do a multi or single-user setup based on feature config # Do a multi or single-user setup based on feature config
if [ "${MULTIUSER}" = "true" ]; then if [ "${MULTIUSER}" = "true" ]; then
echo "(*) Performing multi-user install..." echo "(*) Performing multi-user install..."
sh "${tmpdir}/install-nix" --daemon sh "${tmpdir}/install-nix" --daemon
else else
home_dir="$(eval echo ~${USERNAME})" home_dir="$(eval echo ~${USERNAME})"
if [ ! -e "${home_dir}" ]; then if [ ! -e "${home_dir}" ]; then
echo "(!) Home directory ${home_dir} does not exist for ${USERNAME}. Nix install will fail." echo "(!) Home directory ${home_dir} does not exist for ${USERNAME}. Nix install will fail."
exit 1 exit 1
fi
echo "(*) Performing single-user install..."
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"
# Install per https://nixos.org/manual/nix/stable/installation/installing-binary.html#single-user-installation
mkdir -p /nix
chown ${USERNAME} /nix ${tmpdir}
su ${USERNAME} -c "sh \"${tmpdir}/install-nix\" --no-daemon --no-modify-profile"
# nix installer does not update ~/.bashrc, and USER may or may not be defined, so update rc/profile files directly to handle that
snippet='
if [ "${PATH#*$HOME/.nix-profile/bin}" = "${PATH}" ]; then if [ -z "$USER" ]; then USER=$(whoami); fi; . $HOME/.nix-profile/etc/profile.d/nix.sh; fi
'
update_rc_file "$home_dir/.bashrc" "${snippet}"
update_rc_file "$home_dir/.zshenv" "${snippet}"
update_rc_file "$home_dir/.profile" "${snippet}"
fi fi
rm -rf "${tmpdir}" "/tmp/tmp-gnupg" echo "(*) Performing single-user install..."
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"
# Install per https://nixos.org/manual/nix/stable/installation/installing-binary.html#single-user-installation
mkdir -p /nix
chown ${USERNAME} /nix ${tmpdir}
su ${USERNAME} -c "sh \"${tmpdir}/install-nix\" --no-daemon --no-modify-profile"
# nix installer does not update ~/.bashrc, and USER may or may not be defined, so update rc/profile files directly to handle that
snippet='
if [ "${PATH#*$HOME/.nix-profile/bin}" = "${PATH}" ]; then if [ -z "$USER" ]; then USER=$(whoami); fi; . $HOME/.nix-profile/etc/profile.d/nix.sh; fi
'
update_rc_file "$home_dir/.bashrc" "${snippet}"
update_rc_file "$home_dir/.zshenv" "${snippet}"
update_rc_file "$home_dir/.profile" "${snippet}"
fi fi
rm -rf "${tmpdir}" "/tmp/tmp-gnupg"
# Set nix config # Set nix config
mkdir -p /etc/nix mkdir -p /etc/nix
......
...@@ -91,7 +91,6 @@ ...@@ -91,7 +91,6 @@
} }
} }
}, },
"flake": { "flake": {
"image": "mcr.microsoft.com/devcontainers/base:ubuntu", "image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"remoteUser": "vscode", "remoteUser": "vscode",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment