This directory demonstrates a complete, working devcontainer setup integrated with Nix and devcontainer-env.
-
.devcontainer/devcontainer.json— DevContainer configuration defining:- Workspace service (base image)
- PostgreSQL service for local development
containerEnv— Environment variables thatdevcontainer-envexports to the host (more on this below)
-
.devcontainer/docker-compose.yml— Docker Compose definition for services:workspaceservice running the development environmentpostgresservice with proper health checks and port mapping- Uses dynamic port mapping (
ports: [5432]) so multiple projects don't conflict
-
.github/workflows/ci.yml— GitHub Actions workflow demonstrating:- Starting the DevContainer in CI with
devcontainer-env/devcontainer-ciaction - Setting up Nix and caching
- Running tests within
nix developwith exported environment variables available
- Starting the DevContainer in CI with
-
flake.nix/flake.lock— Nix flake showing how to:- Consume
devcontainer-envfrom the main repository - Set up a development shell that automatically exports container environment variables
- Consume
When you run nix develop in this directory:
-
Nix evaluates the flake — It reads
flake.nixand resolves all dependencies (including the devcontainer-env package) -
Creates an isolated dev shell — A shell environment is created with the packages defined in
devShells.default -
Runs the shellHook — Before giving you the shell, it executes:
eval "$(devcontainer-env export)"
-
Exports container environment — This command:
- Reads your
.devcontainer/devcontainer.json - Starts the Docker containers (if not already running)
- Extracts environment variables like
EXAMPLE_API_DATABASE_URLfrom the container - Sets them in your shell session
- Reads your
-
You can now run commands — Your host shell now has all container services available:
$ nix develop $ psql $EXAMPLE_API_DATABASE_URL # PostgreSQL is accessible $ env | grep EXAMPLE_API # Container env vars are available
- Copy
.devcontainer/to your project - Customize
devcontainer.jsonanddocker-compose.ymlfor your services - Copy
flake.nixand update the service name, image, and environment variables - Initialize the devcontainer:
devcontainer up --workspace-folder . - Run
nix developto enter the development shell with all services running
When you're done, exit the shell (exit or Ctrl+D) and the containers remain running. Run nix develop again to re-enter with the same environment.
- Declarative — All configuration in code (git-friendly)
- Reproducible — Same environment across machines with Nix
- Automatic — No manual container startup;
devcontainer upandnix develophandle it - Integrated — Host tools can talk to container services using exported environment variables