Skip to content

Commit 8f1cdd4

Browse files
feat: add Bun feature
1 parent 611d590 commit 8f1cdd4

11 files changed

Lines changed: 419 additions & 0 deletions

src/bun/NOTES.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Additional Notes
2+
3+
- Bun Website: [https://bun.com/](https://bun.com/)
4+
- Bun GitHub: [https://github.com/oven-sh/bun](https://github.com/oven-sh/bun)
5+
6+
## Install Method
7+
8+
This feature installs Bun via the repository's `gh-release` helper, which downloads the correct release asset for the current platform and architecture and places the `bun` binary in `/usr/local/bin`. This follows the repo’s best practices for reproducibility and small image layers.
9+
10+
## bunx shim
11+
12+
Bun supports executing binaries with `bun x <pkg>`. Some environments/tools expect a `bunx` executable. To provide a consistent experience, this feature adds a small shim at `/usr/local/bin/bunx` that forwards to `bun x`. If the upstream release reliably includes a `bunx` binary in the future, the shim can be removed without breaking users.
13+
14+
## Usage Examples
15+
16+
Default (latest):
17+
18+
```json
19+
"features": {
20+
"ghcr.io/devcontainers-extra/features/bun:1": {}
21+
}
22+
```
23+
24+
Pin a specific version:
25+
26+
```json
27+
"features": {
28+
"ghcr.io/devcontainers-extra/features/bun:1": {
29+
"version": "1.1.38"
30+
}
31+
}
32+
```
33+
34+
## Support
35+
36+
- Distros: `Debian` / `Ubuntu` and `Alpine`
37+
- Architectures: `x86_64` and `arm64`
38+
39+
## Tests
40+
41+
This feature is tested against various Distro:
42+
43+
- `Ubuntu` (latest)
44+
- `Debian` (latest)
45+
- `Alpine` (latest)
46+
- `Debian` with a pinned version of Bun (e.g., Bun `v1.1.38`)
47+
48+
## Considerations / Future Enhancements
49+
50+
- If Bun’s release assets ever require explicit filtering, we can wire an `assetRegex` or pass `additionalFlags` to the `gh-release` helper. The helper already auto-filters by platform/arch, so no extra flags are set currently.
51+
- Coexistence with Node.js: this feature only installs `bun` and a `bunx` shim and does not modify Node.js.
52+
- PATH/profile: installation to `/usr/local/bin` avoids profile changes.

src/bun/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Bun (via Github Releases) (bun)
2+
3+
Installs the Bun JavaScript runtime and package manager via GitHub Releases using the gh-release helper.
4+
5+
## Example Usage
6+
7+
```json
8+
"features": {
9+
"ghcr.io/devcontainers-extra/features/bun:1": {}
10+
}
11+
```
12+
13+
## Options
14+
15+
| Options Id | Description | Type | Default Value |
16+
|-----|-----|-----|-----|
17+
| Options Id | Description | Type | Default Value |
18+
|-----|-----|-----|-----|
19+
| version | Select the version to install. | string | latest |
20+
21+
---
22+
23+
_Note: This file was auto-generated from the [devcontainer-feature.json](devcontainer-feature.json). Add additional notes to a `NOTES.md`._

src/bun/devcontainer-feature.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"id": "bun",
3+
"version": "1.0.0",
4+
"name": "Bun",
5+
"documentationURL": "http://github.com/devcontainers-extra/features/tree/main/src/bun",
6+
"description": "Installs the Bun JavaScript runtime and package manager via GitHub Releases using the gh-release helper.",
7+
"options": {
8+
"version": {
9+
"default": "latest",
10+
"description": "Select the version to install.",
11+
"proposals": [
12+
"latest"
13+
],
14+
"type": "string"
15+
}
16+
},
17+
"installsAfter": [
18+
"ghcr.io/devcontainers-extra/features/gh-release"
19+
]
20+
}

src/bun/install.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
source ./library_scripts.sh
6+
7+
# nanolayer is a cli utility which keeps container layers as small as possible
8+
# source code: https://github.com/devcontainers-extra/nanolayer
9+
# `ensure_nanolayer` is a bash function that will find any existing nanolayer installations,
10+
# and if missing - will download a temporary copy that automatically get deleted at the end
11+
# of the script
12+
ensure_nanolayer nanolayer_location "v0.5.6"
13+
14+
# Canonicalize VERSION for Bun's tag scheme when pinning
15+
version_for_release="$VERSION"
16+
if [ "${VERSION:-latest}" != "latest" ] && [ -n "$VERSION" ]; then
17+
if [[ "$VERSION" =~ ^bun-v ]]; then
18+
version_for_release="$VERSION"
19+
elif [[ "$VERSION" =~ ^v ]]; then
20+
version_for_release="bun-$VERSION"
21+
else
22+
version_for_release="bun-v$VERSION"
23+
fi
24+
fi
25+
26+
# Figure out arch and libc to disambiguate Bun's multiple Linux assets
27+
arch_segment=""
28+
case "$(uname -m)" in
29+
x86_64)
30+
arch_segment="x64"
31+
;;
32+
aarch64|arm64)
33+
arch_segment="aarch64"
34+
;;
35+
*)
36+
# Fallback to uname -m (unlikely used by bun release naming)
37+
arch_segment="$(uname -m)"
38+
;;
39+
esac
40+
41+
# Detect musl (Alpine) vs glibc
42+
libc_suffix=""
43+
if [ -x "/sbin/apk" ]; then
44+
libc_suffix="-musl"
45+
fi
46+
47+
# Prefer baseline builds for widest CPU compatibility; exclude profile variants
48+
# Examples matched:
49+
# - bun-linux-x64-baseline.zip
50+
# - bun-linux-x64-musl-baseline.zip
51+
# - bun-linux-aarch64-baseline.zip
52+
# - bun-linux-aarch64-musl-baseline.zip
53+
asset_regex="^bun-linux-${arch_segment}${libc_suffix}-baseline\\.zip$"
54+
55+
# Bun tags are of the form 'bun-vX.Y.Z'; constrain tag discovery accordingly
56+
release_tag_regex="^bun-v"
57+
58+
# Install Bun via gh-release helper with explicit asset/tag filters
59+
$nanolayer_location \
60+
install \
61+
devcontainer-feature \
62+
"ghcr.io/devcontainers-extra/features/gh-release:1" \
63+
--option repo='oven-sh/bun' \
64+
--option binaryNames='bun' \
65+
--option version="$version_for_release" \
66+
--option assetRegex="$asset_regex" \
67+
--option releaseTagRegex="$release_tag_regex"
68+
69+
# Provide a convenient bunx shim
70+
if [ -x "/usr/local/bin/bun" ] && ! [ -x "/usr/local/bin/bunx" ]; then
71+
cat >/usr/local/bin/bunx <<'EOF'
72+
#!/usr/bin/env bash
73+
exec bun x "$@"
74+
EOF
75+
chmod +x /usr/local/bin/bunx
76+
fi
77+
78+
echo 'Bun installation completed.'

src/bun/library_scripts.sh

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#!/usr/bin/env bash
2+
3+
clean_download() {
4+
# The purpose of this function is to download a file with minimal impact on container layer size
5+
# this means if no valid downloader is found (curl or wget) then we install a downloader (currently wget) in a
6+
# temporary manner, and making sure to
7+
# 1. uninstall the downloader at the return of the function
8+
# 2. revert back any changes to the package installer database/cache (for example apt-get lists)
9+
# The above steps will minimize the leftovers being created while installing the downloader
10+
# Supported distros:
11+
# debian/ubuntu/alpine
12+
13+
url=$1
14+
output_location=$2
15+
tempdir=$(mktemp -d)
16+
downloader_installed=""
17+
18+
function _apt_get_install() {
19+
tempdir=$1
20+
21+
# copy current state of apt list - in order to revert back later (minimize contianer layer size)
22+
cp -p -R /var/lib/apt/lists $tempdir
23+
apt-get update -y
24+
apt-get -y install --no-install-recommends wget ca-certificates
25+
}
26+
27+
function _apt_get_cleanup() {
28+
tempdir=$1
29+
30+
echo "removing wget"
31+
apt-get -y purge wget --auto-remove
32+
33+
echo "revert back apt lists"
34+
rm -rf /var/lib/apt/lists/*
35+
rm -r /var/lib/apt/lists && mv $tempdir/lists /var/lib/apt/lists
36+
}
37+
38+
function _apk_install() {
39+
tempdir=$1
40+
# copy current state of apk cache - in order to revert back later (minimize contianer layer size)
41+
cp -p -R /var/cache/apk $tempdir
42+
43+
apk add --no-cache wget
44+
}
45+
46+
function _apk_cleanup() {
47+
tempdir=$1
48+
49+
echo "removing wget"
50+
apk del wget
51+
}
52+
# try to use either wget or curl if one of them already installer
53+
if type curl >/dev/null 2>&1; then
54+
downloader=curl
55+
elif type wget >/dev/null 2>&1; then
56+
downloader=wget
57+
else
58+
downloader=""
59+
fi
60+
61+
# in case none of them is installed, install wget temporarly
62+
if [ -z $downloader ]; then
63+
if [ -x "/usr/bin/apt-get" ]; then
64+
_apt_get_install $tempdir
65+
elif [ -x "/sbin/apk" ]; then
66+
_apk_install $tempdir
67+
else
68+
echo "distro not supported"
69+
exit 1
70+
fi
71+
downloader="wget"
72+
downloader_installed="true"
73+
fi
74+
75+
if [ $downloader = "wget" ]; then
76+
wget -q $url -O $output_location
77+
else
78+
curl -sfL $url -o $output_location
79+
fi
80+
81+
# NOTE: the cleanup procedure was not implemented using `trap X RETURN` only because
82+
# alpine lack bash, and RETURN is not a valid signal under sh shell
83+
if ! [ -z $downloader_installed ]; then
84+
if [ -x "/usr/bin/apt-get" ]; then
85+
_apt_get_cleanup $tempdir
86+
elif [ -x "/sbin/apk" ]; then
87+
_apk_cleanup $tempdir
88+
else
89+
echo "distro not supported"
90+
exit 1
91+
fi
92+
fi
93+
94+
}
95+
96+
ensure_nanolayer() {
97+
# Ensure existance of the nanolayer cli program
98+
local variable_name=$1
99+
100+
local required_version=$2
101+
# normalize version
102+
if ! [[ $required_version == v* ]]; then
103+
required_version=v$required_version
104+
fi
105+
106+
local nanolayer_location=""
107+
108+
# If possible - try to use an already installed nanolayer
109+
if [[ -z "${NANOLAYER_FORCE_CLI_INSTALLATION}" ]]; then
110+
if [[ -z "${NANOLAYER_CLI_LOCATION}" ]]; then
111+
if type nanolayer >/dev/null 2>&1; then
112+
echo "Found a pre-existing nanolayer in PATH"
113+
nanolayer_location=nanolayer
114+
fi
115+
elif [ -f "${NANOLAYER_CLI_LOCATION}" ] && [ -x "${NANOLAYER_CLI_LOCATION}" ]; then
116+
nanolayer_location=${NANOLAYER_CLI_LOCATION}
117+
echo "Found a pre-existing nanolayer which were given in env variable: $nanolayer_location"
118+
fi
119+
120+
# make sure its of the required version
121+
if ! [[ -z "${nanolayer_location}" ]]; then
122+
local current_version
123+
current_version=$($nanolayer_location --version)
124+
if ! [[ $current_version == v* ]]; then
125+
current_version=v$current_version
126+
fi
127+
128+
if ! [ $current_version == $required_version ]; then
129+
echo "skipping usage of pre-existing nanolayer. (required version $required_version does not match existing version $current_version)"
130+
nanolayer_location=""
131+
fi
132+
fi
133+
134+
fi
135+
136+
# If not previuse installation found, download it temporarly and delete at the end of the script
137+
if [[ -z "${nanolayer_location}" ]]; then
138+
139+
if [ "$(uname -sm)" == "Linux x86_64" ] || [ "$(uname -sm)" == "Linux aarch64" ]; then
140+
tmp_dir=$(mktemp -d -t nanolayer-XXXXXXXXXX)
141+
142+
clean_up() {
143+
ARG=$?
144+
rm -rf $tmp_dir
145+
exit $ARG
146+
}
147+
trap clean_up EXIT
148+
149+
if [ -x "/sbin/apk" ]; then
150+
clib_type=musl
151+
else
152+
clib_type=gnu
153+
fi
154+
155+
tar_filename=nanolayer-"$(uname -m)"-unknown-linux-$clib_type.tgz
156+
157+
# clean download will minimize leftover in case a downloaderlike wget or curl need to be installed
158+
clean_download https://github.com/devcontainers-extra/nanolayer/releases/download/$required_version/$tar_filename $tmp_dir/$tar_filename
159+
160+
tar xfzv $tmp_dir/$tar_filename -C "$tmp_dir"
161+
chmod a+x $tmp_dir/nanolayer
162+
nanolayer_location=$tmp_dir/nanolayer
163+
164+
else
165+
echo "No binaries compiled for non-x86-linux architectures yet: $(uname -m)"
166+
exit 1
167+
fi
168+
fi
169+
170+
# Expose outside the resolved location
171+
declare -g ${variable_name}=$nanolayer_location
172+
173+
}

test/bun/scenarios.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"test_ubuntu": {
3+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
4+
"features": {
5+
"bun": {}
6+
}
7+
},
8+
"test_debian": {
9+
"image": "mcr.microsoft.com/devcontainers/base:debian",
10+
"features": {
11+
"bun": {}
12+
}
13+
},
14+
"test_alpine": {
15+
"image": "mcr.microsoft.com/devcontainers/base:alpine",
16+
"features": {
17+
"bun": {}
18+
}
19+
},
20+
"test_specific_version": {
21+
"image": "mcr.microsoft.com/devcontainers/base:debian",
22+
"features": {
23+
"bun": {
24+
"version": "1.2.21"
25+
}
26+
}
27+
}
28+
}

test/bun/test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
7+
check "Bun is installed (via GitHub Releases)" bun --version
8+
9+
reportResults

0 commit comments

Comments
 (0)