Skip to content

Commit c893dab

Browse files
rfayclaude
andauthored
fix: persist Homebrew across workspace restarts, fix brew upgrade EXDEV error (#186)
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 49e6652 commit c893dab

9 files changed

Lines changed: 87 additions & 3 deletions

File tree

‎CLAUDE.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ The startup script is inline in each template's `template.tf` (e.g. `freeform/te
233233
### Volume Strategy
234234
- **Home directory**: Host path `/coder-workspaces/<owner>-<workspace>` → Container `/home/coder`
235235
- **Docker cache**: Named volume `coder-<owner>-<workspace>-dind-cache` → `/var/lib/docker`
236-
- **Isolation**: Each workspace gets separate host directory and Docker volume
236+
- **Homebrew**: Named volume `coder-<owner>-<workspace>-linuxbrew` → `/home/linuxbrew` — persists the Homebrew Cellar across workspace stop/start, so `brew upgrade`/`brew install` done inside a workspace survives restarts. Unlike the `/home/coder` bind mount, a fresh named volume is auto-populated by Docker from the image's existing `/home/linuxbrew` contents on first mount, so no manual copy step is needed — only an ownership fix (`sudo chown -R coder:coder /home/linuxbrew`) in the startup script. `HOMEBREW_CACHE` is set to `/home/linuxbrew/.cache/Homebrew` (via the Dockerfile) so the cache and Cellar always stay on the same filesystem, avoiding cross-device link (`EXDEV`) errors.
237+
- **Isolation**: Each workspace gets separate host directory and Docker volumes
237238

238239
### Terraform Variables
239240
Key template variables (e.g. in `freeform/template.tf`):

‎VERSION‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.4
1+
v0.5

‎drupal-contrib/template.tf‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ resource "coder_agent" "main" {
313313
fi
314314
315315
sudo chown coder:coder /home/coder
316+
sudo chown -R coder:coder /home/linuxbrew
316317
317318
if [ ! -f "/home/coder/.bashrc" ]; then
318319
echo "Initializing home directory..."
@@ -1073,6 +1074,15 @@ resource "docker_volume" "coder_dind_cache" {
10731074
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}-dind-cache"
10741075
}
10751076

1077+
# Persists /home/linuxbrew (Homebrew Cellar) across workspace stop/start so
1078+
# `brew upgrade`/`brew install` survives restarts. Not gated by start_count,
1079+
# matching coder_dind_cache above. Docker auto-populates a newly-created
1080+
# named volume from the image's existing directory contents on first mount,
1081+
# so no manual copy-from-image seed step is needed here.
1082+
resource "docker_volume" "coder_linuxbrew" {
1083+
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}-linuxbrew"
1084+
}
1085+
10761086
module "vscode-web" {
10771087
count = data.coder_workspace.me.start_count
10781088
source = "registry.coder.com/coder/vscode-web/coder"
@@ -1188,6 +1198,12 @@ resource "docker_container" "workspace" {
11881198
target = "/var/lib/docker"
11891199
}
11901200

1201+
mounts {
1202+
type = "volume"
1203+
source = docker_volume.coder_linuxbrew.name
1204+
target = "/home/linuxbrew"
1205+
}
1206+
11911207
env = [
11921208
"CODER_AGENT_TOKEN=${coder_agent.main.token}",
11931209
"CODER_WORKSPACE_NAME=${data.coder_workspace.me.name}",

‎drupal-contrib/tests/validate.tftest.hcl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,11 @@ run "memory_above_maximum" {
7878
}
7979
expect_failures = [var.memory]
8080
}
81+
82+
run "linuxbrew_volume_created" {
83+
command = plan
84+
assert {
85+
condition = docker_volume.coder_linuxbrew.name == "coder-testuser-test-workspace-linuxbrew"
86+
error_message = "docker_volume.coder_linuxbrew must be named per the coder-<owner>-<workspace>-linuxbrew convention"
87+
}
88+
}

‎drupal-core/template.tf‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ resource "coder_agent" "main" {
355355
# Since the volume comes from the host, it might have host permissions.
356356
# We fix this on every startup.
357357
sudo chown coder:coder /home/coder
358+
sudo chown -R coder:coder /home/linuxbrew
358359
359360
# Copy defaults if empty (first run)
360361
if [ ! -f "/home/coder/.bashrc" ]; then
@@ -1362,6 +1363,15 @@ resource "docker_volume" "coder_dind_cache" {
13621363
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}-dind-cache"
13631364
}
13641365

1366+
# Persists /home/linuxbrew (Homebrew Cellar) across workspace stop/start so
1367+
# `brew upgrade`/`brew install` survives restarts. Not gated by start_count,
1368+
# matching coder_dind_cache above. Docker auto-populates a newly-created
1369+
# named volume from the image's existing directory contents on first mount,
1370+
# so no manual copy-from-image seed step is needed here.
1371+
resource "docker_volume" "coder_linuxbrew" {
1372+
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}-linuxbrew"
1373+
}
1374+
13651375
# VS Code for Web
13661376
module "vscode-web" {
13671377
count = data.coder_workspace.me.start_count
@@ -1523,6 +1533,12 @@ resource "docker_container" "workspace" {
15231533
target = "/var/lib/docker"
15241534
}
15251535

1536+
mounts {
1537+
type = "volume"
1538+
source = docker_volume.coder_linuxbrew.name
1539+
target = "/home/linuxbrew"
1540+
}
1541+
15261542
# Environment variables
15271543
env = [
15281544
"CODER_AGENT_TOKEN=${coder_agent.main.token}",

‎drupal-core/tests/validate.tftest.hcl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,14 @@ run "memory_above_maximum" {
8282
}
8383
expect_failures = [var.memory]
8484
}
85+
86+
run "linuxbrew_volume_created" {
87+
command = plan
88+
variables {
89+
cache_path = "/tmp/mock-cache"
90+
}
91+
assert {
92+
condition = docker_volume.coder_linuxbrew.name == "coder-testuser-test-workspace-linuxbrew"
93+
error_message = "docker_volume.coder_linuxbrew must be named per the coder-<owner>-<workspace>-linuxbrew convention"
94+
}
95+
}

‎freeform/template.tf‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ resource "coder_agent" "main" {
206206
fi
207207
208208
sudo chown coder:coder /home/coder
209+
sudo chown -R coder:coder /home/linuxbrew
209210
210211
if [ ! -f "/home/coder/.bashrc" ]; then
211212
echo "Initializing home directory..."
@@ -443,6 +444,15 @@ resource "docker_volume" "coder_dind_cache" {
443444
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}-dind-cache"
444445
}
445446

447+
# Persists /home/linuxbrew (Homebrew Cellar) across workspace stop/start so
448+
# `brew upgrade`/`brew install` survives restarts. Not gated by start_count,
449+
# matching coder_dind_cache above. Docker auto-populates a newly-created
450+
# named volume from the image's existing directory contents on first mount,
451+
# so no manual copy-from-image seed step is needed here.
452+
resource "docker_volume" "coder_linuxbrew" {
453+
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}-linuxbrew"
454+
}
455+
446456
module "vscode-web" {
447457
count = data.coder_workspace.me.start_count
448458
source = "registry.coder.com/coder/vscode-web/coder"
@@ -577,6 +587,12 @@ resource "docker_container" "workspace" {
577587
target = "/var/lib/docker"
578588
}
579589

590+
mounts {
591+
type = "volume"
592+
source = docker_volume.coder_linuxbrew.name
593+
target = "/home/linuxbrew"
594+
}
595+
580596
env = [
581597
"CODER_AGENT_TOKEN=${coder_agent.main.token}",
582598
"CODER_WORKSPACE_NAME=${data.coder_workspace.me.name}",

‎freeform/tests/validate.tftest.hcl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,11 @@ run "memory_above_maximum" {
132132
}
133133
expect_failures = [var.memory]
134134
}
135+
136+
run "linuxbrew_volume_created" {
137+
command = plan
138+
assert {
139+
condition = docker_volume.coder_linuxbrew.name == "coder-testuser-test-workspace-linuxbrew"
140+
error_message = "docker_volume.coder_linuxbrew must be named per the coder-<owner>-<workspace>-linuxbrew convention"
141+
}
142+
}

‎image/Dockerfile‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,17 @@ RUN printf '#!/usr/bin/env bash\nddev php "$@"\n' > /usr/local/bin/php && \
165165

166166
RUN mkdir -p /home/linuxbrew/.linuxbrew && chown -R coder:coder /home/linuxbrew/
167167

168+
# Homebrew's cache must live on the same filesystem as the Cellar
169+
# (/home/linuxbrew/.linuxbrew) to avoid cross-device link (EXDEV) errors when
170+
# brew hardlinks a downloaded bottle from cache into the Cellar. At runtime,
171+
# /home/linuxbrew is mounted as a persistent per-workspace volume (see each
172+
# template's template.tf), so this path stays on the same filesystem as the
173+
# Cellar across workspace restarts too.
174+
ENV HOMEBREW_CACHE=/home/linuxbrew/.cache/Homebrew
175+
168176
USER coder
169177
RUN /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
170-
RUN /home/linuxbrew/.linuxbrew/bin/brew install \
178+
RUN mkdir -p "$HOMEBREW_CACHE" && /home/linuxbrew/.linuxbrew/bin/brew install \
171179
bats-core \
172180
bats-core/bats-core/bats-assert \
173181
bats-core/bats-core/bats-file \

0 commit comments

Comments
 (0)