Skip to content

Commit 43e5f3f

Browse files
[rust] Add Alpine support for rust feature
musl is supported by Rustup; it is just required to download the right version of rustup-init.
1 parent 849a5e2 commit 43e5f3f

File tree

5 files changed

+98
-29
lines changed

5 files changed

+98
-29
lines changed

src/rust/NOTES.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
## OS Support
44

5-
This Feature should work on recent versions of Debian/Ubuntu, RedHat Enterprise Linux, Fedora, Alma, RockyLinux
6-
and Mariner distributions with the `apt`, `yum`, `dnf`, `microdnf` and `tdnf` package manager installed.
7-
8-
9-
**Note:** Alpine is not supported because the rustup-init binary requires glibc to run, but Alpine Linux does not include `glibc`
10-
by default. Instead, it uses musl libc, which is not binary-compatible with glibc.
5+
This Feature should work on recent versions of Debian/Ubuntu, RedHat Enterprise Linux, Fedora, Alma, RockyLinux,
6+
Mariner and Alpine distributions with the `apt`, `yum`, `dnf`, `microdnf`, `tdnf` and `apk` package manager installed.
117

128
`bash` is required to execute the `install.sh` script.

src/rust/README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ Installs Rust, common Rust utilities, and their required dependencies
3232

3333
## OS Support
3434

35-
This Feature should work on recent versions of Debian/Ubuntu, RedHat Enterprise Linux, Fedora, Alma, RockyLinux
36-
and Mariner distributions with the `apt`, `yum`, `dnf`, `microdnf` and `tdnf` package manager installed.
37-
38-
39-
**Note:** Alpine is not supported because the rustup-init binary requires glibc to run, but Alpine Linux does not include `glibc`
40-
by default. Instead, it uses musl libc, which is not binary-compatible with glibc.
35+
This Feature should work on recent versions of Debian/Ubuntu, RedHat Enterprise Linux, Fedora, Alma, RockyLinux,
36+
Mariner and Alpine distributions with the `apt`, `yum`, `dnf`, `microdnf`, `tdnf` and `apk` package manager installed.
4137

4238
`bash` is required to execute the `install.sh` script.
4339

src/rust/install.sh

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ elif command -v microdnf >/dev/null 2>&1; then
5858
PKG_MANAGER="microdnf"
5959
elif command -v tdnf >/dev/null 2>&1; then
6060
PKG_MANAGER="tdnf"
61+
elif command -v apk >/dev/null 2>&1; then
62+
PKG_MANAGER="apk"
6163
else
62-
echo "No supported package manager found. Supported: apt, dnf, yum, microdnf, tdnf"
64+
echo "No supported package manager found. Supported: apt, dnf, yum, microdnf, tdnf, apk"
6365
exit 1
6466
fi
6567

@@ -85,6 +87,11 @@ clean_package_cache() {
8587
tdnf)
8688
tdnf clean all
8789
;;
90+
apk)
91+
if [ "$(ls -1 /var/cache/apk/ 2>/dev/null | wc -l)" -gt 0 ]; then
92+
rm -rf /var/cache/apk/*
93+
fi
94+
;;
8895
esac
8996
}
9097

@@ -217,6 +224,12 @@ pkg_mgr_update() {
217224
tdnf)
218225
tdnf makecache || true
219226
;;
227+
apk)
228+
if [ "$(find /var/cache/apk/* | wc -l)" = "0" ]; then
229+
echo "Running apk update..."
230+
apk update
231+
fi
232+
;;
220233
esac
221234
}
222235

@@ -230,6 +243,9 @@ is_package_installed() {
230243
dnf|yum|microdnf|tdnf)
231244
rpm -q "$package" >/dev/null 2>&1
232245
;;
246+
apk)
247+
apk info --installed "$package" >/dev/null 2>&1
248+
;;
233249
esac
234250
}
235251

@@ -266,6 +282,14 @@ check_packages() {
266282
"gnupg2") packages[$i]="gnupg" ;;
267283
esac
268284
;;
285+
apk)
286+
case "${packages[$i]}" in
287+
"libc6-dev") packages[$i]="libc-dev" ;;
288+
"python3-minimal") packages[$i]="python3" ;;
289+
"libpython3.*") packages[$i]="python3-dev" ;;
290+
"gnupg2") packages[$i]="gnupg" ;;
291+
esac
292+
;;
269293
esac
270294
done
271295

@@ -295,6 +319,9 @@ check_packages() {
295319
tdnf)
296320
tdnf install -y "${missing_packages[@]}"
297321
;;
322+
apk)
323+
apk add --no-cache "${missing_packages[@]}"
324+
;;
298325
esac
299326
fi
300327
}
@@ -317,6 +344,9 @@ case "$PKG_MANAGER" in
317344
check_packages python3 python3-devel || true
318345
# LLDB might not be available in Photon/Mariner
319346
;;
347+
apk)
348+
check_packages lldb python3 python3-dev || true
349+
;;
320350
esac
321351

322352
# Get architecture
@@ -348,6 +378,11 @@ case ${download_architecture} in
348378
;;
349379
esac
350380

381+
clibtype="gnu"
382+
if ldd --version 2>&1 | grep -q 'musl'; then
383+
clibtype="musl"
384+
fi
385+
351386
# Install Rust
352387
umask 0002
353388
if ! grep -e "^rustlang:" /etc/group > /dev/null 2>&1; then
@@ -378,14 +413,14 @@ else
378413
fi
379414
echo "Installing Rust..."
380415
# Download and verify rustup sha
381-
mkdir -p /tmp/rustup/target/${download_architecture}-unknown-linux-gnu/release/
382-
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
383-
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
416+
mkdir -p /tmp/rustup/target/${download_architecture}-unknown-linux-${clibtype}/release/
417+
curl -sSL --proto '=https' --tlsv1.2 "https://static.rust-lang.org/rustup/dist/${download_architecture}-unknown-linux-${clibtype}/rustup-init" -o /tmp/rustup/target/${download_architecture}-unknown-linux-${clibtype}/release/rustup-init
418+
curl -sSL --proto '=https' --tlsv1.2 "https://static.rust-lang.org/rustup/dist/${download_architecture}-unknown-linux-${clibtype}/rustup-init.sha256" -o /tmp/rustup/rustup-init.sha256
384419
cd /tmp/rustup
385-
cp /tmp/rustup/target/${download_architecture}-unknown-linux-gnu/release/rustup-init /tmp/rustup/rustup-init
420+
cp /tmp/rustup/target/${download_architecture}-unknown-linux-${clibtype}/release/rustup-init /tmp/rustup/rustup-init
386421
sha256sum -c rustup-init.sha256
387-
chmod +x target/${download_architecture}-unknown-linux-gnu/release/rustup-init
388-
target/${download_architecture}-unknown-linux-gnu/release/rustup-init -y --no-modify-path --profile "${RUSTUP_PROFILE}" ${default_toolchain_arg}
422+
chmod +x target/${download_architecture}-unknown-linux-${clibtype}/release/rustup-init
423+
target/${download_architecture}-unknown-linux-${clibtype}/release/rustup-init -y --no-modify-path --profile "${RUSTUP_PROFILE}" ${default_toolchain_arg}
389424
cd ~
390425
rm -rf /tmp/rustup
391426
fi

test/rust/rust_with_alpine.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Helper function to check component is installed
9+
check_component_installed() {
10+
local component=$1
11+
if rustup component list | grep -q "${component}.*installed"; then
12+
return 0 # Component is installed (success)
13+
else
14+
return 1 # Component is not installed (failure)
15+
fi
16+
}
17+
18+
# Definition specific tests
19+
check "cargo version" cargo --version
20+
check "rustc version" rustc --version
21+
check "correct rust version" rustup target list | grep "aarch64-unknown-linux-musl.*installed"
22+
23+
# Check that all specified extended components are installed
24+
check "rust-analyzer is installed" check_component_installed "rust-analyzer"
25+
check "rust-src is installed" check_component_installed "rust-src"
26+
check "rustfmt is installed" check_component_installed "rustfmt"
27+
check "clippy is installed" check_component_installed "clippy"
28+
check "rust-docs is installed" check_component_installed "rust-docs"
29+
30+
# Report result
31+
reportResults
32+

test/rust/scenarios.json

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@
6464
"components": ""
6565
}
6666
}
67-
},
67+
},
6868
"rust_with_centos": {
6969
"image": "centos:centos7",
7070
"features": {
7171
"rust": {
7272
"version": "latest",
7373
"targets": "aarch64-unknown-linux-gnu",
74-
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
74+
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
7575
}
7676
}
7777
},
@@ -81,7 +81,7 @@
8181
"rust": {
8282
"version": "latest",
8383
"targets": "aarch64-unknown-linux-gnu",
84-
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
84+
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
8585
}
8686
}
8787
},
@@ -91,7 +91,7 @@
9191
"rust": {
9292
"version": "latest",
9393
"targets": "aarch64-unknown-linux-gnu",
94-
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
94+
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
9595
}
9696
}
9797
},
@@ -101,7 +101,7 @@
101101
"rust": {
102102
"version": "latest",
103103
"targets": "aarch64-unknown-linux-gnu",
104-
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
104+
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
105105
}
106106
}
107107
},
@@ -111,7 +111,7 @@
111111
"rust": {
112112
"version": "latest",
113113
"targets": "aarch64-unknown-linux-gnu",
114-
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
114+
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
115115
}
116116
}
117117
},
@@ -121,7 +121,7 @@
121121
"rust": {
122122
"version": "latest",
123123
"targets": "aarch64-unknown-linux-gnu",
124-
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
124+
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
125125
}
126126
}
127127
},
@@ -131,8 +131,18 @@
131131
"rust": {
132132
"version": "latest",
133133
"targets": "aarch64-unknown-linux-gnu",
134-
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
134+
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
135+
}
136+
}
137+
},
138+
"rust_with_alpine": {
139+
"image": "mcr.microsoft.com/devcontainers/base:alpine-3.21",
140+
"features": {
141+
"rust": {
142+
"version": "latest",
143+
"targets": "aarch64-unknown-linux-musl",
144+
"components": "rust-analyzer,rust-src,rustfmt,clippy,rust-docs"
135145
}
136146
}
137-
}
138-
}
147+
}
148+
}

0 commit comments

Comments
 (0)