Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 8ed3437

Browse files
committed
support download from mirrors
1 parent 204c88f commit 8ed3437

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ EOF
1515
Then install asdf-zig
1616

1717
```bash
18-
asdf plugin-add zig https://github.com/zigcc/asdf-zig.git
18+
asdf plugin add zig https://github.com/zigcc/asdf-zig.git
19+
20+
# Update to latest if already installed
21+
asdf plugin update zig
1922
```
2023

2124
Then install latest Zig
@@ -30,6 +33,22 @@ zig version
3033
Check [asdf](https://github.com/asdf-vm/asdf) readme for instructions on how to
3134
install & manage versions.
3235

36+
### Zig download mirror
37+
38+
As suggested in [Migrating from AWS to Self-Hosting
39+
](https://ziglang.org/news/migrate-to-self-hosting/), asdf-zig will first try download from mirrors.
40+
41+
Currently there are(same with [setup-zig](https://github.com/mlugg/setup-zig/blob/main/mirrors.json)):
42+
- https://pkg.machengine.org/zig
43+
- https://zigmirror.hryx.net/zig
44+
- https://zig.linus.dev/zig
45+
- https://fs.liujiacai.net/zigbuilds
46+
47+
48+
You can also configure where to download `index.json` with `ASDF_ZIG_INDEX_URL` env var, it's https://ziglang.org/download/index.json by default. Availables:
49+
50+
- https://liujiacai.net/zigbuilds/index.json
51+
3352
## License
3453

3554
Licensed under the

bin/install

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,56 @@ fail() {
2121
exit 1
2222
}
2323

24+
# https://github.com/mlugg/setup-zig/blob/main/mirrors.json
25+
# If any of these mirrors are down, please open an issue!
26+
mirrors=(
27+
"https://pkg.machengine.org/zig"
28+
"https://zigmirror.hryx.net/zig"
29+
"https://zig.linus.dev/zig"
30+
"https://fs.liujiacai.net/zigbuilds"
31+
)
32+
mirror_length=${#mirrors[@]}
33+
34+
download_and_check() {
35+
local url=$1
36+
local output=$2
37+
local expected_shasum=$3
38+
39+
curl --fail --progress-bar --location --create-dirs --output "$output" "$url"
40+
41+
local actual_shasum
42+
actual_shasum="$(shasum -a 256 "$output" | awk '{print $1}')"
43+
44+
if [ "${expected_shasum}" != "${actual_shasum}" ]; then
45+
echo "Checksum not same, expected: ${expected_shasum}, actual: ${actual_shasum}, file: ${source_path}"
46+
return 1
47+
fi
48+
}
49+
50+
download_zig() {
51+
local download_url=$1
52+
local source_path=$2
53+
local expected_shasum=$3
54+
55+
local filename
56+
filename=$(echo "$download_url" | awk -F '/' '{print $NF}')
57+
local start_index=$((RANDOM % mirror_length))
58+
# Loop through the mirrors
59+
for i in $(seq 0 $((mirror_length - 1))); do
60+
current_index=$(( (start_index + i) % mirror_length ))
61+
local current_mirror=${mirrors[$current_index]}
62+
63+
local current_url="${current_mirror}/${filename}?source=zigcc-asdf-zig"
64+
echo "Download from mirror $current_url"
65+
download_and_check "${current_url}" "$source_path" "$expected_shasum" && return 0
66+
done
67+
68+
echo "Download $download_url"
69+
if ! download_and_check "$download_url" "$source_path" "$expected_shasum";then
70+
fail "Checksum not same, expected: ${expected_shasum}, actual: ${actual_shasum}, file: ${source_path}"
71+
fi
72+
}
73+
2474
install_zig() {
2575
local install_type=$1
2676
local version=$2
@@ -52,7 +102,8 @@ install_zig() {
52102

53103
local json
54104
echo "∗ Downloading index.json..."
55-
json=$(curl --fail --progress-bar --location https://ziglang.org/download/index.json)
105+
index_url="${ASDF_ZIG_INDEX_URL:-https://ziglang.org/download/index.json}"
106+
json=$(curl --fail --progress-bar --location "${index_url}")
56107

57108
local download_url
58109
download_url=$(
@@ -71,17 +122,10 @@ install_zig() {
71122

72123
local source_path="${install_path}/zig-${platform}-${architecture}-${version}.tar.xz"
73124

74-
echo "∗ Downloading and installing Zig..."
75-
echo "$download_url"
76-
curl --fail --progress-bar --location --create-dirs --output "$source_path" "$download_url"
77-
78-
local actual_shasum
79-
actual_shasum="$(shasum -a 256 "$source_path" | awk '{print $1}')"
80-
81-
if [ "${expected_shasum}" != "${actual_shasum}" ]; then
82-
fail "Checksum not same, expected: ${expected_shasum}, actual: ${actual_shasum}, file: ${source_path}"
83-
fi
125+
echo "∗ Downloading..."
126+
download_zig "$download_url" "$source_path" "$expected_shasum"
84127

128+
echo "∗ Extracting and install..."
85129
tar -xf "$source_path" -C "$install_path" --strip-components=1
86130
mkdir -p "${install_path}/bin"
87131
ln -s "${install_path}/zig" "${install_path}/bin/zig"

0 commit comments

Comments
 (0)