Skip to content

Commit 08e6fec

Browse files
authored
refactor(devcontainer): support local environment overrides (#433)
Move the tracked Dev Container build defaults from the repository-level .env file to .devcontainer/.env.defaults. Generate an ignored .env.compose during initializeCommand by copying the defaults and appending optional values from .env.local. Point .devcontainer/.env at the generated file so Compose discovers the merged configuration without requiring an extra setup command. Ignore root-level .env variants in Git and Docker build contexts, document dependency overrides and per-checkout Compose project names, and broaden the editor guidance to IDEs implementing the Development Container Specification.
1 parent 0bd23dd commit 08e6fec

8 files changed

Lines changed: 46 additions & 16 deletions

File tree

.devcontainer/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../.env
1+
../.env.compose
File renamed without changes.

.devcontainer/devcontainer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"version": "23"
1616
}
1717
},
18-
"initializeCommand": "mkdir -p \"${localEnv:HOME}/.codex\"",
18+
"initializeCommand": {
19+
"codex-home": "mkdir -p \"${localEnv:HOME}/.codex\"",
20+
"compose-env": "bash \"${localWorkspaceFolder}/.devcontainer/initialize-compose-env.sh\""
21+
},
1922
"postCreateCommand": "curl -fsSL --output /tmp/codex-install.sh https://chatgpt.com/codex/install.sh && CODEX_NON_INTERACTIVE=1 sh /tmp/codex-install.sh; status=$?; rm -f /tmp/codex-install.sh; exit $status",
2023
"mounts": [
2124
"source=${localEnv:HOME}/.codex,target=/home/dev/.codex,type=bind"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
repo_root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
6+
compose_env="${repo_root}/.env.compose"
7+
user_name=${USER:-$(id -un)}
8+
checkout_name=$(basename -- "${repo_root}")
9+
10+
cp "${repo_root}/.devcontainer/.env.defaults" "${compose_env}"
11+
printf '\nCOMPOSE_PROJECT_NAME=simphony-%s-%s\n' ${user_name} ${checkout_name} >> ${compose_env}
12+
if [[ -f "${repo_root}/.env.local" ]]; then
13+
printf '\n' >> "${compose_env}"
14+
cat "${repo_root}/.env.local" >> "${compose_env}"
15+
fi

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
.github/
33
.devcontainer/
44
.env
5+
.env.*
56
.venv
67
__pycache__/

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*~
2+
/.env.*
23
dd4hepplugins/examples/drich-dev/
34
dd4hepplugins/examples/calibrations/
45
dd4hepplugins/examples/fieldmaps/

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ devcontainer exec bash
5050
The source tree is mounted into the container. Once inside, use the same CMake
5151
commands above and rerun only the relevant build and tests as you work.
5252

53-
The root `.env` selects the OS and toolchain versions. See [Choose dependency
54-
versions](docs/getting-started.md#choose-dependency-versions) before changing
55-
them.
53+
The `.devcontainer/.env.defaults` file selects the default OS and toolchain
54+
versions. See [Choose dependency
55+
versions](docs/getting-started.md#choose-dependency-versions) to override them
56+
locally.
5657

5758
To run GPU-backed code, install the [NVIDIA Container
5859
Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)

docs/getting-started.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,21 @@ create one with `git switch -c new-branch`.
4545

4646
### Choose dependency versions
4747

48-
Skip this section if the default dependency versions meet your needs. The root
49-
`.env` file selects the OS and toolchain used to build the Dev Container.
50-
`.devcontainer/.env` links to it so Compose can discover the same settings.
51-
Edit `.env` when you want to use another supported combination or experiment
52-
with a new dependency version.
48+
If the default dependency versions meet your needs, skip this section and
49+
continue to [Start the environment](#start-the-environment).
50+
51+
The `.devcontainer/.env.defaults` file contains the default OS and toolchain
52+
used to build the Dev Container. To use another supported combination or
53+
experiment with a new dependency version, put your overrides in the `.env.local`
54+
file at the repository root. During `devcontainer up`, the initialization step
55+
combines the defaults and local overrides into the `.env.compose` file that
56+
Compose reads.
5357

5458
The defaults match the `base` alias in the [published container
5559
matrix](../README.md#published-container-images). Start with another `base`
5660
combination from this matrix when possible.
5761

58-
After editing `.env`, recreate the container so it uses the new values:
62+
After editing `.env.local`, recreate the container so it uses the new values:
5963

6064
```shell
6165
devcontainer up --remove-existing-container
@@ -89,15 +93,20 @@ devcontainer up --remove-existing-container
8993
You do not need to recreate the container after switching source branches. The
9094
checkout remains mounted directly into the environment.
9195

92-
The default Compose project name keeps different system users from colliding,
93-
even when they share a Docker daemon. If one account uses multiple checkouts or
94-
runs concurrent jobs, set a unique project name before starting the container:
96+
The default Compose project name includes the system user and checkout
97+
directory name, keeping users and checkouts from colliding when they share a
98+
Docker daemon. To use a different name, append it to `.env.local` before
99+
starting the container:
95100

96101
```shell
97-
export COMPOSE_PROJECT_NAME="simphony-${USER}-my_cool_feature"
102+
echo "COMPOSE_PROJECT_NAME=simphony-${USER}-my_cool_feature" >> .env.local
98103
```
99104

100-
The same configuration also works with the VS Code Dev Containers extension.
105+
Replace `my_cool_feature` with a short unique name for that checkout.
106+
107+
This configuration also works with IDEs and tools that support the [Development
108+
Container Specification](https://containers.dev/). When using VS Code, its Dev
109+
Containers extension also installs the recommended extensions.
101110

102111
### Build and test
103112

0 commit comments

Comments
 (0)