From ca4a05467f98065e7d2e28564ed2937fe0a15527 Mon Sep 17 00:00:00 2001 From: nohzafk <149959021+nohzafk@users.noreply.github.com> Date: Mon, 1 Jan 2024 19:18:02 +0800 Subject: [PATCH 1/5] add option `useAttrPath` to support `nix-env -iA` --- src/nix/devcontainer-feature.json | 9 +++++++-- src/nix/install.sh | 9 +++++---- src/nix/post-install-steps.sh | 21 ++++++++++++++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/nix/devcontainer-feature.json b/src/nix/devcontainer-feature.json index 96a75016b..90de08fe0 100644 --- a/src/nix/devcontainer-feature.json +++ b/src/nix/devcontainer-feature.json @@ -3,7 +3,7 @@ "version": "1.1.3", "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.", + "description": "Installs the Nix package manager and optionally a set of packages.", "options": { "version": { "type": "string", @@ -21,6 +21,11 @@ "default": "", "description": "Optional comma separated list of Nix packages to install in profile." }, + "useAttrPath": { + "type": "boolean", + "default": false, + "description": "Enable this option to use exact attribute path of the package in the Nixpkgs repository, aligning with the nix-env -iA command." + }, "flakeUri": { "type": "string", "default": "", @@ -39,4 +44,4 @@ "PATH": "/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:${PATH}" }, "entrypoint": "/usr/local/share/nix-entrypoint.sh" -} \ No newline at end of file +} diff --git a/src/nix/install.sh b/src/nix/install.sh index ed048fe8d..9ea4f0030 100755 --- a/src/nix/install.sh +++ b/src/nix/install.sh @@ -8,6 +8,7 @@ cd "${FEATURE_DIR}" VERSION="${VERSION:-"latest"}" MULTIUSER="${MULTIUSER:-"true"}" PACKAGES="${PACKAGES//,/ }" +USEATTRPATH="${USEATTRPATH:-"false"}" FLAKEURI="${FLAKEURI:-""}" EXTRANIXCONFIG="${EXTRANIXCONFIG:-""}" USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}" @@ -68,7 +69,7 @@ else 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" + 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} @@ -79,14 +80,14 @@ else ' update_rc_file "$home_dir/.bashrc" "${snippet}" update_rc_file "$home_dir/.zshenv" "${snippet}" - update_rc_file "$home_dir/.profile" "${snippet}" + update_rc_file "$home_dir/.profile" "${snippet}" fi rm -rf "${tmpdir}" "/tmp/tmp-gnupg" fi # Set nix config mkdir -p /etc/nix -create_or_update_file /etc/nix/nix.conf 'sandbox = false' +create_or_update_file /etc/nix/nix.conf 'sandbox = false' if [ ! -z "${FLAKEURI}" ] && [ "${FLAKEURI}" != "none" ]; then create_or_update_file /etc/nix/nix.conf 'experimental-features = nix-command flakes' fi @@ -127,4 +128,4 @@ else " fi -echo "Done!" \ No newline at end of file +echo "Done!" diff --git a/src/nix/post-install-steps.sh b/src/nix/post-install-steps.sh index aa466798b..2b4452280 100755 --- a/src/nix/post-install-steps.sh +++ b/src/nix/post-install-steps.sh @@ -2,10 +2,29 @@ set -e echo "(*) Executing post-installation steps..." +# if not starts with "nixpkgs." add it as prefix to package name +add_nixpkgs_prefix() { + local packages=$1 + local -a addr + IFS=' ' read -ra addr <<<"$packages" + for i in "${!addr[@]}"; do + if [[ ${addr[i]} != nixpkgs.* ]]; then + addr[i]="nixpkgs.${addr[i]}" + fi + done + IFS=' ' echo "${addr[*]}" +} + # Install list of packages in profile if specified. if [ ! -z "${PACKAGES}" ] && [ "${PACKAGES}" != "none" ]; then + if [ "${USEATTRPATH}" = "true" ]; then + PACKAGES=$(add_nixpkgs_prefix "$PACKAGES") + echo "Installing packages \"${PACKAGES}\" in profile..." + nix-env -iA "${PACKAGES}" + else echo "Installing packages \"${PACKAGES}\" in profile..." - nix-env --install ${PACKAGES} + nix-env --install "${PACKAGES}" + fi fi # Install Nix flake in profile if specified From da7abcca2b5986a3c2cb3ddf62e1067d2fa2a19f Mon Sep 17 00:00:00 2001 From: nohzafk <149959021+nohzafk@users.noreply.github.com> Date: Mon, 1 Jan 2024 21:39:37 +0800 Subject: [PATCH 2/5] add test and fix problem --- src/nix/post-install-steps.sh | 4 ++-- test/nix/packages-use-attr-path.sh | 35 ++++++++++++++++++++++++++++++ test/nix/scenarios.json | 13 ++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100755 test/nix/packages-use-attr-path.sh diff --git a/src/nix/post-install-steps.sh b/src/nix/post-install-steps.sh index 2b4452280..6b1cba472 100755 --- a/src/nix/post-install-steps.sh +++ b/src/nix/post-install-steps.sh @@ -20,10 +20,10 @@ if [ ! -z "${PACKAGES}" ] && [ "${PACKAGES}" != "none" ]; then if [ "${USEATTRPATH}" = "true" ]; then PACKAGES=$(add_nixpkgs_prefix "$PACKAGES") echo "Installing packages \"${PACKAGES}\" in profile..." - nix-env -iA "${PACKAGES}" + nix-env -iA ${PACKAGES} else echo "Installing packages \"${PACKAGES}\" in profile..." - nix-env --install "${PACKAGES}" + nix-env --install ${PACKAGES} fi fi diff --git a/test/nix/packages-use-attr-path.sh b/test/nix/packages-use-attr-path.sh new file mode 100755 index 000000000..7f34a96ea --- /dev/null +++ b/test/nix/packages-use-attr-path.sh @@ -0,0 +1,35 @@ +#!/bin/bash +set -e + +# Optional: Import test library bundled with the devcontainer CLI +source dev-container-features-test-lib + +uid="$(id -u)" +echo "Current user UID is ${uid}." +if [ "${uid}" != "1000" ]; then + echo "Current user UID was adjusted." +fi +set +e +vscode_uid="$(id -u vscode)" +set -e +if [ "${vscode_uid}" != "" ]; then + echo "User vscode UID is ${vscode_uid}." + if [ "${vscode_uid}" != "1000" ]; then + echo "User vscode UID was adjusted." + fi +fi +nix_uid="$(stat /nix -c "%u")" +echo "/nix UID is ${nix_uid}." + +cat /etc/os-release + +# Feature-specific tests +# The 'check' command comes from the dev-container-features-test-lib. +check "nix-env" type nix-env +check "vim_installed" type vim +check "node_installed" type node +check "yarn_installed" type yarn + +# Report result +# If any of the checks above exited with a non-zero exit code, the test will fail. +reportResults &2>1 diff --git a/test/nix/scenarios.json b/test/nix/scenarios.json index c287f58c7..e976955af 100644 --- a/test/nix/scenarios.json +++ b/test/nix/scenarios.json @@ -81,6 +81,17 @@ } } }, + "packages-use-attr-path": { + "image": "mcr.microsoft.com/devcontainers/base:ubuntu", + "remoteUser": "vscode", + "features": { + "nix": { + "packages": "nodePackages.nodejs,nixpkgs.vim,nixpkgs.yarn", + "useAttrPath": true + } + } + }, + "flake": { "image": "mcr.microsoft.com/devcontainers/base:ubuntu", "remoteUser": "vscode", @@ -99,4 +110,4 @@ } } } -} \ No newline at end of file +} From b07b84c59706ff3eca3f04c354fe838b739b4c43 Mon Sep 17 00:00:00 2001 From: nohzafk <149959021+nohzafk@users.noreply.github.com> Date: Thu, 18 Jan 2024 10:56:49 +0800 Subject: [PATCH 3/5] change to useAttributePath --- src/nix/devcontainer-feature.json | 2 +- src/nix/install.sh | 2 +- src/nix/post-install-steps.sh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nix/devcontainer-feature.json b/src/nix/devcontainer-feature.json index 90de08fe0..49805a0c2 100644 --- a/src/nix/devcontainer-feature.json +++ b/src/nix/devcontainer-feature.json @@ -21,7 +21,7 @@ "default": "", "description": "Optional comma separated list of Nix packages to install in profile." }, - "useAttrPath": { + "useAttributePath": { "type": "boolean", "default": false, "description": "Enable this option to use exact attribute path of the package in the Nixpkgs repository, aligning with the nix-env -iA command." diff --git a/src/nix/install.sh b/src/nix/install.sh index 9ea4f0030..5b8fe8f28 100755 --- a/src/nix/install.sh +++ b/src/nix/install.sh @@ -8,7 +8,7 @@ cd "${FEATURE_DIR}" VERSION="${VERSION:-"latest"}" MULTIUSER="${MULTIUSER:-"true"}" PACKAGES="${PACKAGES//,/ }" -USEATTRPATH="${USEATTRPATH:-"false"}" +USEATTRIBUTEPATH="${USEATTRIBUTEPATH:-"false"}" FLAKEURI="${FLAKEURI:-""}" EXTRANIXCONFIG="${EXTRANIXCONFIG:-""}" USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}" diff --git a/src/nix/post-install-steps.sh b/src/nix/post-install-steps.sh index 6b1cba472..68f93a391 100755 --- a/src/nix/post-install-steps.sh +++ b/src/nix/post-install-steps.sh @@ -17,7 +17,7 @@ add_nixpkgs_prefix() { # Install list of packages in profile if specified. if [ ! -z "${PACKAGES}" ] && [ "${PACKAGES}" != "none" ]; then - if [ "${USEATTRPATH}" = "true" ]; then + if [ "${USEATTRIBUTEPATH}" = "true" ]; then PACKAGES=$(add_nixpkgs_prefix "$PACKAGES") echo "Installing packages \"${PACKAGES}\" in profile..." nix-env -iA ${PACKAGES} From 8df9934568eb329e35150d13996c7a0ed2e1fa94 Mon Sep 17 00:00:00 2001 From: nohzafk <149959021+nohzafk@users.noreply.github.com> Date: Thu, 18 Jan 2024 10:57:19 +0800 Subject: [PATCH 4/5] bump the 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 49805a0c2..0e37efed2 100644 --- a/src/nix/devcontainer-feature.json +++ b/src/nix/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "nix", - "version": "1.1.3", + "version": "1.2.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.", From 6238fdd2fb5b453c6707f35f3ddd1091a5d4e784 Mon Sep 17 00:00:00 2001 From: nohzafk <149959021+nohzafk@users.noreply.github.com> Date: Thu, 18 Jan 2024 11:47:31 +0800 Subject: [PATCH 5/5] update test --- test/nix/scenarios.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/nix/scenarios.json b/test/nix/scenarios.json index e976955af..80ead7ff6 100644 --- a/test/nix/scenarios.json +++ b/test/nix/scenarios.json @@ -87,7 +87,7 @@ "features": { "nix": { "packages": "nodePackages.nodejs,nixpkgs.vim,nixpkgs.yarn", - "useAttrPath": true + "useAttributePath": true } } },