Unverified Commit 2b2a5f65 authored by nohz.afk's avatar nohz.afk Committed by GitHub

feature nix: add option useAttrPath to use packages attribute path (#787)

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

* add test and fix problem

* change to useAttributePath

* bump the version

* update test
parent f3cbd989
{
"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.",
......@@ -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": "",
......
......@@ -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"}"}"
......
......@@ -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
......
#!/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
......@@ -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",
......
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