Skip to content

Commit c57f96c

Browse files
committed
devcontainer: opt-in CN apt mirror via AGENTENV_CN_MIRROR=1
Pulls the mirror-swap logic out of devcontainer.json into a postcreate.sh script. AGENTENV_CN_MIRROR flows in from the host via containerEnv interpolation; when =1, rewrite /etc/apt sources to mirrors.aliyun.com so apt-get doesn't stall on deb.debian.org. Default empty → no-op for OSS contributors outside China.
1 parent edb9f78 commit c57f96c

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818

1919
// No `features` block: the MS Go devcontainer image already ships the
2020
// vscode user, sudo, git, curl, zsh, oh-my-zsh, and everything else
21-
// common-utils would have installed — adding the feature just re-runs
22-
// a 2-minute apt-get for nothing (and stalls behind deb.debian.org on
23-
// slow networks).
21+
// common-utils would have installed.
2422
//
25-
// libbtrfs-dev lets you build with `-tags btrfs` (the optional fast-path
26-
// backend that needs cgo); the default static build needs nothing.
27-
// `sudo` because lifecycle commands run as remoteUser (vscode), and the
28-
// base image's vscode user has passwordless sudo configured.
29-
"postCreateCommand": "sudo apt-get update && sudo apt-get install -y -qq libbtrfs-dev make",
23+
// postcreate.sh installs libbtrfs-dev + make (so `-tags btrfs` builds
24+
// work), and — if AGENTENV_CN_MIRROR=1 was set on the host — swaps
25+
// /etc/apt sources to mirrors.aliyun.com so apt-get doesn't stall
26+
// behind deb.debian.org from mainland China. The env var flows in via
27+
// containerEnv below; OSS users outside China leave it unset and the
28+
// mirror swap is a no-op.
29+
"postCreateCommand": ".devcontainer/postcreate.sh",
30+
"containerEnv": {
31+
"AGENTENV_CN_MIRROR": "${localEnv:AGENTENV_CN_MIRROR}"
32+
},
3033
// Show the Makefile target list every time the container is attached, so a new
3134
// contributor immediately sees what they can do (build / test / verify / demo).
3235
"postAttachCommand": "make help",

.devcontainer/postcreate.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
# Devcontainer postCreate hook.
3+
#
4+
# Two responsibilities:
5+
# 1. If $AGENTENV_CN_MIRROR=1 (passed in from the host's env via
6+
# devcontainer.json's containerEnv block), rewrite Debian's apt sources
7+
# to mirrors.aliyun.com so apt-get doesn't stall on deb.debian.org.
8+
# Useful inside mainland China — OSS contributors elsewhere skip it.
9+
# 2. Install the optional build deps libbtrfs-dev (for `-tags btrfs`
10+
# builds — the default pure-Go build needs nothing) and make.
11+
12+
set -e
13+
14+
if [ "$AGENTENV_CN_MIRROR" = "1" ]; then
15+
echo "[postcreate] AGENTENV_CN_MIRROR=1 → switching apt sources to mirrors.aliyun.com"
16+
# `*.sources` is Debian's modern deb822 format (trixie/sid); older releases
17+
# still use the single-line sources.list. Rewrite whichever is present.
18+
sudo sed -ri 's|http(s)?://(deb|security)\.debian\.org|http\1://mirrors.aliyun.com|g' \
19+
/etc/apt/sources.list.d/*.sources /etc/apt/sources.list 2>/dev/null || true
20+
fi
21+
22+
sudo apt-get update
23+
sudo apt-get install -y -qq libbtrfs-dev make

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Most of agentenv is Linux-only (namespaces, pivot_root). On macOS you can edit a
1616
cross-compile (`GOOS=linux CGO_ENABLED=0 go build ./...`) and run the portable
1717
unit tests, but exercise the runtime in a Linux VM or Docker.
1818

19+
The `.devcontainer/` config is opt-in to a mainland-China apt mirror — set
20+
`AGENTENV_CN_MIRROR=1` on your host before "Reopen in Container" if
21+
deb.debian.org is slow for you; leave it unset everywhere else.
22+
1923
## Before opening a PR
2024

2125
```bash

0 commit comments

Comments
 (0)