diff options
author | NeonXP <i@neonxp.ru> | 2024-01-28 20:43:11 +0300 |
---|---|---|
committer | NeonXP <i@neonxp.ru> | 2024-01-28 22:07:35 +0300 |
commit | 11412b2e720f754900b39d195f33af39ed11d085 (patch) | |
tree | 96d7e561f21809aead3c0f25034f083f46ef064c /features/src/rust | |
parent | 6c00916212073678bbeb3bdde3f121c4efad707a (diff) |
Diffstat (limited to 'features/src/rust')
-rw-r--r-- | features/src/rust/NOTES.md | 7 | ||||
-rw-r--r-- | features/src/rust/README.md | 41 | ||||
-rw-r--r-- | features/src/rust/devcontainer-feature.json | 67 | ||||
-rwxr-xr-x | features/src/rust/install.sh | 219 |
4 files changed, 334 insertions, 0 deletions
diff --git a/features/src/rust/NOTES.md b/features/src/rust/NOTES.md new file mode 100644 index 0000000..19fe92f --- /dev/null +++ b/features/src/rust/NOTES.md @@ -0,0 +1,7 @@ + + +## OS Support + +This Feature should work on recent versions of Debian/Ubuntu-based distributions with the `apt` package manager installed. + +`bash` is required to execute the `install.sh` script. diff --git a/features/src/rust/README.md b/features/src/rust/README.md new file mode 100644 index 0000000..974d785 --- /dev/null +++ b/features/src/rust/README.md @@ -0,0 +1,41 @@ + +# Rust (rust) + +Installs Rust, common Rust utilities, and their required dependencies + +## Example Usage + +```json +"features": { + "ghcr.io/devcontainers/features/rust:1": {} +} +``` + +## Options + +| Options Id | Description | Type | Default Value | +|-----|-----|-----|-----| +| version | Select or enter a version of Rust to install. | string | latest | +| profile | Select a rustup install profile. | string | minimal | + +## Customizations + +### VS Code Extensions + +- `vadimcn.vscode-lldb` +- `rust-lang.rust-analyzer` +- `tamasfe.even-better-toml` +- `serayuzgur.crates` + + + +## OS Support + +This Feature should work on recent versions of Debian/Ubuntu-based distributions with the `apt` package manager installed. + +`bash` is required to execute the `install.sh` script. + + +--- + +_Note: This file was auto-generated from the [devcontainer-feature.json](https://github.com/devcontainers/features/blob/main/src/rust/devcontainer-feature.json). Add additional notes to a `NOTES.md`._ diff --git a/features/src/rust/devcontainer-feature.json b/features/src/rust/devcontainer-feature.json new file mode 100644 index 0000000..8e855c1 --- /dev/null +++ b/features/src/rust/devcontainer-feature.json @@ -0,0 +1,67 @@ +{ + "id": "rust", + "version": "1.1.1", + "name": "Rust", + "documentationURL": "https://github.com/devcontainers/features/tree/main/src/rust", + "description": "Installs Rust, common Rust utilities, and their required dependencies", + "options": { + "version": { + "type": "string", + "proposals": [ + "latest", + "none", + "1.70", + "1.69", + "1.68", + "1.67", + "1.66", + "1.65", + "1.64", + "1.63", + "1.62", + "1.61" + ], + "default": "latest", + "description": "Select or enter a version of Rust to install." + }, + "profile": { + "type": "string", + "proposals": [ + "minimal", + "default", + "complete" + ], + "default": "minimal", + "description": "Select a rustup install profile." + } + }, + "customizations": { + "vscode": { + "extensions": [ + "vadimcn.vscode-lldb", + "rust-lang.rust-analyzer", + "tamasfe.even-better-toml", + "serayuzgur.crates" + ], + "settings": { + "files.watcherExclude": { + "**/target/**": true + } + } + } + }, + "containerEnv": { + "CARGO_HOME": "/usr/local/cargo", + "RUSTUP_HOME": "/usr/local/rustup", + "PATH": "/usr/local/cargo/bin:${PATH}" + }, + "capAdd": [ + "SYS_PTRACE" + ], + "securityOpt": [ + "seccomp=unconfined" + ], + "installsAfter": [ + "https://gitrepo.ru/api/packages/NeonXP/generic/features/latest/devcontainer-feature-common-utils.tgz" + ] +} diff --git a/features/src/rust/install.sh b/features/src/rust/install.sh new file mode 100755 index 0000000..00c0a6e --- /dev/null +++ b/features/src/rust/install.sh @@ -0,0 +1,219 @@ +#!/usr/bin/env bash +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- +# +# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/rust.md +# Maintainer: The VS Code and Codespaces Teams + +RUST_VERSION="${VERSION:-"latest"}" +RUSTUP_PROFILE="${PROFILE:-"minimal"}" + +export CARGO_HOME="${CARGO_HOME:-"/usr/local/cargo"}" +export RUSTUP_HOME="${RUSTUP_HOME:-"/usr/local/rustup"}" +USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}" +UPDATE_RC="${UPDATE_RC:-"true"}" +UPDATE_RUST="${UPDATE_RUST:-"false"}" + +set -e + +# Clean up +rm -rf /var/lib/apt/lists/* + +if [ "$(id -u)" -ne 0 ]; then + echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' + exit 1 +fi + +# Ensure that login shells get the correct path if the user updated the PATH using ENV. +rm -f /etc/profile.d/00-restore-env.sh +echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh +chmod +x /etc/profile.d/00-restore-env.sh + +# Determine the appropriate non-root user +if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then + USERNAME="" + POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)") + for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do + if id -u ${CURRENT_USER} > /dev/null 2>&1; then + USERNAME=${CURRENT_USER} + break + fi + done + if [ "${USERNAME}" = "" ]; then + USERNAME=root + fi +elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then + USERNAME=root +fi + +# Figure out correct version of a three part version number is not passed +find_version_from_git_tags() { + local variable_name=$1 + local requested_version=${!variable_name} + if [ "${requested_version}" = "none" ]; then return; fi + local repository=$2 + local prefix=${3:-"tags/v"} + local separator=${4:-"."} + local last_part_optional=${5:-"false"} + if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then + local escaped_separator=${separator//./\\.} + local last_part + if [ "${last_part_optional}" = "true" ]; then + last_part="(${escaped_separator}[0-9]+)?" + else + last_part="${escaped_separator}[0-9]+" + fi + local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$" + local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)" + if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then + declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)" + else + set +e + declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")" + set -e + fi + fi + if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then + echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2 + exit 1 + fi + echo "${variable_name}=${!variable_name}" +} + +check_nightly_version_formatting() { + local variable_name=$1 + local requested_version=${!variable_name} + if [ "${requested_version}" = "none" ]; then return; fi + + local version_date=$(echo ${requested_version} | sed -e "s/^nightly-//") + + date -d ${version_date} &>/dev/null + if [ $? != 0 ]; then + echo -e "Invalid ${variable_name} value: ${requested_version}\nNightly version should be in the format nightly-YYYY-MM-DD" >&2 + exit 1 + fi + + if [ $(date -d ${version_date} +%s) -ge $(date +%s) ]; then + echo -e "Invalid ${variable_name} value: ${requested_version}\nNightly version should not exceed current date" >&2 + exit 1 + fi +} + +updaterc() { + if [ "${UPDATE_RC}" = "true" ]; then + echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..." + if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then + echo -e "$1" >> /etc/bash.bashrc + fi + if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then + echo -e "$1" >> /etc/zsh/zshrc + fi + fi +} + +apt_get_update() +{ + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then + echo "Running apt-get update..." + apt-get update -y + fi +} + +# Checks if packages are installed and installs them if not +check_packages() { + if ! dpkg -s "$@" >/dev/null 2>&1; then + apt_get_update + apt-get -y install --no-install-recommends "$@" + fi +} + +export DEBIAN_FRONTEND=noninteractive + +# Install curl, lldb, python3-minimal,libpython and rust dependencies if missing +if ! dpkg -s curl ca-certificates gnupg2 lldb python3-minimal gcc libc6-dev > /dev/null 2>&1; then + apt_get_update + apt-get -y install --no-install-recommends curl ca-certificates gcc libc6-dev + apt-get -y install lldb python3-minimal libpython3.? +fi + +architecture="$(dpkg --print-architecture)" +download_architecture="${architecture}" +case ${download_architecture} in + amd64) + download_architecture="x86_64" + ;; + arm64) + download_architecture="aarch64" + ;; + *) echo "(!) Architecture ${architecture} not supported." + exit 1 + ;; +esac + +# Install Rust +umask 0002 +if ! cat /etc/group | grep -e "^rustlang:" > /dev/null 2>&1; then + groupadd -r rustlang +fi +usermod -a -G rustlang "${USERNAME}" +mkdir -p "${CARGO_HOME}" "${RUSTUP_HOME}" +chown "${USERNAME}:rustlang" "${RUSTUP_HOME}" "${CARGO_HOME}" +chmod g+r+w+s "${RUSTUP_HOME}" "${CARGO_HOME}" + +if [ "${RUST_VERSION}" = "none" ] || type rustup > /dev/null 2>&1; then + echo "Rust already installed. Skipping..." +else + if [ "${RUST_VERSION}" != "latest" ] && [ "${RUST_VERSION}" != "lts" ] && [ "${RUST_VERSION}" != "stable" ]; then + # Find version using soft match + if ! type git > /dev/null 2>&1; then + check_packages git + fi + + is_nightly=0 + echo ${RUST_VERSION} | grep -q "nightly" || is_nightly=$? + if [ $is_nightly = 0 ]; then + check_nightly_version_formatting RUST_VERSION + else + find_version_from_git_tags RUST_VERSION "https://github.com/rust-lang/rust" "tags/" + fi + default_toolchain_arg="--default-toolchain ${RUST_VERSION}" + fi + echo "Installing Rust..." + # Download and verify rustup sha + mkdir -p /tmp/rustup/target/${download_architecture}-unknown-linux-gnu/release/ + curl -sSL --proto '=https' --tlsv1.2 "https://static.rust-lang.org/rustup/dist/${download_architecture}-unknown-linux-gnu/rustup-init" -o /tmp/rustup/target/${download_architecture}-unknown-linux-gnu/release/rustup-init + curl -sSL --proto '=https' --tlsv1.2 "https://static.rust-lang.org/rustup/dist/${download_architecture}-unknown-linux-gnu/rustup-init.sha256" -o /tmp/rustup/rustup-init.sha256 + cd /tmp/rustup + sha256sum -c rustup-init.sha256 + chmod +x target/${download_architecture}-unknown-linux-gnu/release/rustup-init + target/${download_architecture}-unknown-linux-gnu/release/rustup-init -y --no-modify-path --profile ${RUSTUP_PROFILE} ${default_toolchain_arg} + cd ~ + rm -rf /tmp/rustup +fi + +export PATH=${CARGO_HOME}/bin:${PATH} +if [ "${UPDATE_RUST}" = "true" ]; then + echo "Updating Rust..." + rustup update 2>&1 +fi +echo "Installing common Rust dependencies..." +rustup component add rls rust-analysis rust-src rustfmt clippy 2>&1 + +# Add CARGO_HOME, RUSTUP_HOME and bin directory into bashrc/zshrc files (unless disabled) +updaterc "$(cat << EOF +export RUSTUP_HOME="${RUSTUP_HOME}" +export CARGO_HOME="${CARGO_HOME}" +if [[ "\${PATH}" != *"\${CARGO_HOME}/bin"* ]]; then export PATH="\${CARGO_HOME}/bin:\${PATH}"; fi +EOF +)" + +# Make files writable for rustlang group +chmod -R g+r+w "${RUSTUP_HOME}" "${CARGO_HOME}" + +# Clean up +rm -rf /var/lib/apt/lists/* + +echo "Done!" + |