Building Hermetic Dev Environments
Hermetic development environments ensure reproducibility, isolation, and consistency across machines, teams, and CI/CD pipelines. No "works on my machine" issues. We'll use:
- VS Code Dev Containers (.devcontainer/) for instant onboarding.
- Containerized toolchains via multi-stage Dockerfiles (e.g., for Rust, Go, Node—extensible).
- Remote caches (Bazel + Remote Execution/Cache or Nix flakes for pure hermeticity).
- Onboarding via a single
git clone && code . flow.
This setup supports polyglot repos. Example: Rust + Go + Node toolchain. Adapt as needed.
1. Project Structure
my-project/
├── .devcontainer/
│ ├── devcontainer.json
│ ├── Dockerfile
│ └── docker-compose.yml # For services (DB, etc.)
├── .bazelrc # Or flake.nix for Nix
├── WORKSPACE # Bazel monorepo setup
├── README.md # Onboarding
└── src/ # Your code
2. Devcontainer Config (.devcontainer/devcontainer.json)
This spins up a full env with VS Code extensions, mounts your code, and pre-installs tools.
{
"name": "Hermetic Dev Env",
"dockerComposeFile": "docker-compose.yml",
"service": "dev",
"workspaceFolder": "/workspace",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {}
},
"postCreateCommand": "bazelisk setup --verify && bazelrc --setup",
"customizations": {
"vscode": {
"extensions": [
"bazelbuild.vscode-bazel",
"rust-lang.rust-analyzer",
"golang.go",
"ms-vscode.vscode-typescript-next"
]
}
},
"remoteUser": "vscode",
"mounts": [
"source=${localEnv:HOME}${localEnv:USERPROFILE}/.config/bazel-remote,target=/config/remote,type=bind,consistency=cached"
]
}
- postCreateCommand: Sets up Bazel (or Nix).
- mounts: Persists remote cache auth locally.
- Run
Dev Containers: Reopen in Container in VS Code.
3. Containerized Toolchain (.devcontainer/Dockerfile)
Multi-stage for hermetic toolchains. Pins versions for reproducibility.
# Base: Ubuntu for stability
FROM ubuntu:22.04 AS base
# Common deps
RUN apt-get update && apt-get install -y \
curl wget git build-essential pkg-config \
&& rm -rf /var/lib/apt/lists/*
# Rust toolchain (cargo, rustc pinned)
FROM base AS rust
ARG RUST_VERSION=1.75.0
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${RUST_VERSION}
ENV PATH="/root/.cargo/bin:${PATH}"
RUN rustup component add rustfmt clippy
# Go toolchain (pinned)
FROM base AS go
ARG GO_VERSION=1.21.5
RUN wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz && \
tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz && \
rm go${GO_VERSION}.linux-amd64.tar.gz
ENV PATH="/usr/local/go/bin:${PATH}"
# Node (via NodeSource for latest LTS)
FROM base AS node
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && \
apt-get install -y nodejs && rm -rf /var/lib/apt/lists/*
# Final image: Merge toolchains + Bazelisk
FROM base
COPY --from=rust /root/.cargo /root/.cargo
COPY --from=rust /root/.rustup /root/.rustup
COPY --from=go /usr/local/go /usr/local/go
COPY --from=node /usr/bin/node /usr/bin/node
COPY --from=node /usr/bin/npm /usr/bin/npm
ENV PATH="/root/.cargo/bin:/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin:/usr/local/go/bin:/usr/bin/node/bin:${PATH}"
# Bazelisk (auto-downloads Bazel)
RUN curl -fsLO https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64 && \
mv bazelisk-linux-amd64 /usr/local/bin/bazel && chmod +x /usr/local/bin/bazel
# User setup
RUN useradd -m vscode && chown -R vscode:vscode /workspace
USER vscode
WORKDIR /workspace
4. Docker Compose (.devcontainer/docker-compose.yml)
For services (e.g., Postgres) and remote cache proxy.
services:
dev:
build: .
volumes:
- ..:/workspace:cached
command: sleep infinity
network_mode: host # For cache access
cache-proxy: # Optional: Bazel remote cache proxy (e.g., via engprod/remote-cache-proxy)
image: gcr.io/trailblazer-labs/bazel-remote-cache-proxy:latest
ports:
- "8080:8080"
volumes:
- remote-cache:/cache
command: --storage=/cache --port=8080
volumes:
remote-cache:
5. Remote Caches
Option A: Bazel (Recommended for monorepos)
Option B: Nix Flakes (Pure Hermeticity)
Replace Bazel with flake.nix:
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nix-23.11";
outputs = { self, nixpkgs }: {
devShells.x86_64-linux.default = let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in pkgs.mkShell {
buildInputs = [ pkgs.rustc pkgs.cargo pkgs.go pkgs.nodejs ];
RUST_BACKTRACE = 1;
};
};
}
- Cache: Push to Cachix:
cachix watch exec myproject.cachix.
- Devcontainer postCreate:
nix develop.
Option C: Language-Specific (Fallback)
- Rust:
sccache --start-server + S3 bucket.
- Go:
GOCACHE=/shared-cache.
6. Onboarding Guide (README.md)
# Onboarding (2 minutes)
## Prerequisites
- VS Code + [Dev Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension.
- GitHub account (for Codespaces fallback).
## Steps
1. `git clone https://github.com/your-org/my-project.git`
2. `cd my-project`
3. `code .` (opens VS Code)
4. **Reopen in Container** (Ctrl+Shift+P → Dev Containers: Reopen in Container).
5. Wait 1-2 min (pulls ~5GB image + cache).
6. `bazel build //...` (hits remote cache instantly).
## Verify
- `rustc --version` → 1.75.0
- `go version` → 1.21.5
- `bazel test //...` → Cached hits.
## CI/CD Integration
Use same image in GitHub Actions:
```yaml
- uses: docker://your-registry/hermetic-dev:latest
run: bazel build //...
Troubleshooting
- Cache misses?
bazel clean --expunge.
- Slow first build? Pre-warm cache in CI.
- Codespaces: Enable via
.devcontainer.json.
## Benefits & Metrics
- **Reproducible**: Pinned versions, no host OS deps.
- **Fast**: Remote cache → 10x build speedup.
- **Onboard Time**: <5 min.
- **Scale**: Works in Codespaces, Gitpod, local, CI.
- **Cost**: ~$0.01/build with shared cache.
Extend for Kubernetes (via DevSpace/Tilt), WSL2, or ARM (multi-arch builds). Ping for customizations! 🚀