Skip to content

Commit

Permalink
feature nix: add option useAttrPath to use packages attribute path (#787
Browse files Browse the repository at this point in the history
)

* add option `useAttrPath` to support `nix-env -iA`

* add test and fix problem

* change to useAttributePath

* bump the version

* update test
  • Loading branch information
nohzafk authored Jan 18, 2024
1 parent f3cbd98 commit 2b2a5f6
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/nix/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"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.",
"description": "Installs the Nix package manager and optionally a set of packages.",
"options": {
"version": {
"type": "string",
Expand All @@ -21,6 +21,11 @@
"default": "",
"description": "Optional comma separated list of Nix packages to install in profile."
},
"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."
},
"flakeUri": {
"type": "string",
"default": "",
Expand All @@ -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"
}
}
9 changes: 5 additions & 4 deletions src/nix/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cd "${FEATURE_DIR}"
VERSION="${VERSION:-"latest"}"
MULTIUSER="${MULTIUSER:-"true"}"
PACKAGES="${PACKAGES//,/ }"
USEATTRIBUTEPATH="${USEATTRIBUTEPATH:-"false"}"
FLAKEURI="${FLAKEURI:-""}"
EXTRANIXCONFIG="${EXTRANIXCONFIG:-""}"
USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"
Expand Down Expand Up @@ -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}
Expand All @@ -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
Expand Down Expand Up @@ -127,4 +128,4 @@ else
"
fi

echo "Done!"
echo "Done!"
19 changes: 19 additions & 0 deletions src/nix/post-install-steps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 [ "${USEATTRIBUTEPATH}" = "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}
fi
fi

# Install Nix flake in profile if specified
Expand Down
35 changes: 35 additions & 0 deletions test/nix/packages-use-attr-path.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion test/nix/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"useAttributePath": true
}
}
},

"flake": {
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"remoteUser": "vscode",
Expand All @@ -99,4 +110,4 @@
}
}
}
}
}

0 comments on commit 2b2a5f6

Please sign in to comment.