From 6ae5bfc5b1c6c7e240b66d50606f820980330bfc Mon Sep 17 00:00:00 2001 From: Flo Date: Mon, 23 Sep 2024 12:34:23 +0200 Subject: [PATCH 1/5] feat(nix): Enable persistent volume for shared nix-store --- src/nix/devcontainer-feature.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/nix/devcontainer-feature.json b/src/nix/devcontainer-feature.json index 0e37efed2..2f43b829e 100644 --- a/src/nix/devcontainer-feature.json +++ b/src/nix/devcontainer-feature.json @@ -43,5 +43,12 @@ "containerEnv": { "PATH": "/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:${PATH}" }, + "mounts": [ + { + "source": "devcontainer-nix-store", + "target": "/nix/store", + "type": "volume" + } + ], "entrypoint": "/usr/local/share/nix-entrypoint.sh" } From b5e158034c8f90a301dc3dd8c7a73c78d0a2ac45 Mon Sep 17 00:00:00 2001 From: Flo Date: Tue, 24 Sep 2024 15:17:26 +0200 Subject: [PATCH 2/5] fix: Wrong mount --- src/nix/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix/devcontainer-feature.json b/src/nix/devcontainer-feature.json index 2f43b829e..5996930e4 100644 --- a/src/nix/devcontainer-feature.json +++ b/src/nix/devcontainer-feature.json @@ -46,7 +46,7 @@ "mounts": [ { "source": "devcontainer-nix-store", - "target": "/nix/store", + "target": "/nix", "type": "volume" } ], From 292248fa8b1f44cc20861e0df3009c6a8bafce7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flo=20M=C3=BCller?= Date: Wed, 25 Sep 2024 14:31:08 +0000 Subject: [PATCH 3/5] fix: Switch to persistent volumes per devcontainer, fixes renamed nix package used in test --- src/nix/devcontainer-feature.json | 13 ++-- src/nix/install.sh | 104 ++++++++++++++---------------- test/nix/scenarios.json | 1 - 3 files changed, 58 insertions(+), 60 deletions(-) diff --git a/src/nix/devcontainer-feature.json b/src/nix/devcontainer-feature.json index 5996930e4..6f53d8e2a 100644 --- a/src/nix/devcontainer-feature.json +++ b/src/nix/devcontainer-feature.json @@ -7,7 +7,10 @@ "options": { "version": { "type": "string", - "proposals": ["latest", "2.11"], + "proposals": [ + "latest", + "2.11" + ], "default": "latest", "description": "Version of Nix to install." }, @@ -44,10 +47,10 @@ "PATH": "/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:${PATH}" }, "mounts": [ - { - "source": "devcontainer-nix-store", - "target": "/nix", - "type": "volume" + { + "source": "${devcontainerId}-nix-store", + "target": "/nix", + "type": "volume" } ], "entrypoint": "/usr/local/share/nix-entrypoint.sh" diff --git a/src/nix/install.sh b/src/nix/install.sh index 5b8fe8f28..0030c2b18 100755 --- a/src/nix/install.sh +++ b/src/nix/install.sh @@ -23,67 +23,63 @@ fi detect_user USERNAME -if [ -e "/nix" ]; then - echo "(!) Nix is already installed! Skipping installation." -else - if [ "${USERNAME}" = "root" ] && [ "${MULTIUSER}" != "true" ]; then - 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 +if [ "${USERNAME}" = "root" ] && [ "${MULTIUSER}" != "true" ]; then + 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 - apt_get_update_if_exists - check_command curl "curl ca-certificates" "curl ca-certificates" "curl ca-certificates" - check_command gpg2 gnupg2 gnupg gnupg2 - check_command dirmngr dirmngr dirmngr dirmngr - check_command xz xz-utils xz xz - check_command git git git git - check_command xargs findutils findutils findutils +# Verify dependencies +apt_get_update_if_exists +check_command curl "curl ca-certificates" "curl ca-certificates" "curl ca-certificates" +check_command gpg2 gnupg2 gnupg gnupg2 +check_command dirmngr dirmngr dirmngr dirmngr +check_command xz xz-utils xz xz +check_command git git git git +check_command xargs findutils findutils findutils - # Determine version - find_version_from_git_tags VERSION https://github.com/NixOS/nix "tags/" +# Determine version +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 - tmpdir="$(mktemp -d)" - echo "(*) Downloading Nix installer..." - set +e +# Download and verify install per https://nixos.org/download.html#nix-verify-installation +tmpdir="$(mktemp -d)" +echo "(*) Downloading Nix installer..." +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 - 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 - fi - cd "${FEATURE_DIR}" +fi +cd "${FEATURE_DIR}" - # Do a multi or single-user setup based on feature config - if [ "${MULTIUSER}" = "true" ]; then - echo "(*) Performing multi-user install..." - sh "${tmpdir}/install-nix" --daemon - else - home_dir="$(eval echo ~${USERNAME})" - if [ ! -e "${home_dir}" ]; then - echo "(!) Home directory ${home_dir} does not exist for ${USERNAME}. Nix install will fail." - 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}" +# Do a multi or single-user setup based on feature config +if [ "${MULTIUSER}" = "true" ]; then + echo "(*) Performing multi-user install..." + sh "${tmpdir}/install-nix" --daemon +else + home_dir="$(eval echo ~${USERNAME})" + if [ ! -e "${home_dir}" ]; then + echo "(!) Home directory ${home_dir} does not exist for ${USERNAME}. Nix install will fail." + exit 1 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 +rm -rf "${tmpdir}" "/tmp/tmp-gnupg" # Set nix config mkdir -p /etc/nix diff --git a/test/nix/scenarios.json b/test/nix/scenarios.json index 80ead7ff6..dac5624f1 100644 --- a/test/nix/scenarios.json +++ b/test/nix/scenarios.json @@ -91,7 +91,6 @@ } } }, - "flake": { "image": "mcr.microsoft.com/devcontainers/base:ubuntu", "remoteUser": "vscode", From c9c2c84fe56a8886145bad37504c464078079167 Mon Sep 17 00:00:00 2001 From: Flo Date: Thu, 26 Sep 2024 00:48:29 +0200 Subject: [PATCH 4/5] Update src/nix/devcontainer-feature.json Co-authored-by: Samruddhi Khandale --- src/nix/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix/devcontainer-feature.json b/src/nix/devcontainer-feature.json index 6f53d8e2a..2bd6d2bb2 100644 --- a/src/nix/devcontainer-feature.json +++ b/src/nix/devcontainer-feature.json @@ -48,7 +48,7 @@ }, "mounts": [ { - "source": "${devcontainerId}-nix-store", + "source": "nix-store-${devcontainerId}", "target": "/nix", "type": "volume" } From 371b3bb0cbecf8262f17e22ea4575b8b10db9ae7 Mon Sep 17 00:00:00 2001 From: Flo Date: Thu, 26 Sep 2024 00:50:51 +0200 Subject: [PATCH 5/5] fix: Bump version --- src/nix/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix/devcontainer-feature.json b/src/nix/devcontainer-feature.json index 2bd6d2bb2..7fadd63fa 100644 --- a/src/nix/devcontainer-feature.json +++ b/src/nix/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "nix", - "version": "1.2.0", + "version": "1.3.0", "name": "Nix Package Manager", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/nix", "description": "Installs the Nix package manager and optionally a set of packages.",