File Layering

One of dotenvage's key features is automatic file layering - multiple .env* files are loaded and merged with a clear precedence order. Later files override values from earlier files.

Loading Order

Files are loaded using a flexible power-set algorithm that generates all possible combinations of ENV, OS, ARCH, USER, and VARIANT. This allows any combination you need without being constrained by a fixed hierarchy.

Key principle: All multi-part file names use dots as separators only (not dashes), ensuring unambiguous parsing.

Files are loaded in specificity order (later overrides earlier):

  1. .env - Base configuration (always first)
  2. Single-part patterns: .env.<ENV>, .env.<OS>, .env.<ARCH>, .env.<USER>, .env.<VARIANT>
  3. Two-part combinations: .env.<ENV>.<OS>, .env.<ENV>.<ARCH>, .env.<ENV>.<USER>, .env.<ENV>.<VARIANT>, etc.
  4. Three-part combinations: .env.<ENV>.<OS>.<ARCH>, .env.<ENV>.<OS>.<USER>, .env.<ENV>.<OS>.<VARIANT>, etc.
  5. Four-part combinations: .env.<ENV>.<OS>.<ARCH>.<USER>, .env.<ENV>.<OS>.<ARCH>.<VARIANT>, etc.
  6. Five-part combination: .env.<ENV>.<OS>.<ARCH>.<USER>.<VARIANT> (most specific)
  7. .env.pr-<NUMBER> - PR-specific (GitHub Actions only, always last)

All files can be safely committed to git since secrets are encrypted.

Example Combinations

With ENV=prod, OS=linux, ARCH=amd64, USER=alice, VARIANT=docker, these files would be loaded (in order, showing a subset):

  • .env
  • .env.prod
  • .env.linux
  • .env.amd64
  • .env.alice
  • .env.docker
  • .env.prod.linux
  • .env.prod.amd64
  • .env.prod.alice
  • .env.prod.docker
  • .env.linux.amd64
  • .env.linux.alice
  • .env.linux.docker
  • ... (more combinations)
  • .env.prod.linux.amd64.alice
  • .env.prod.linux.amd64.docker
  • .env.prod.linux.alice.docker
  • .env.prod.amd64.alice.docker
  • .env.linux.amd64.alice.docker
  • .env.prod.linux.amd64.alice.docker

With all 5 dimensions set, up to 31 file combinations are checked. You only need to create the files you use - the loader checks which exist.

Placeholders

PlaceholderEnvironment Variables (priority order)Default / Notes
<ENV>DOTENVAGE_ENV, EKG_ENV, VERCEL_ENV, NODE_ENVDefaults to local
<OS>DOTENVAGE_OS, EKG_OS, CARGO_CFG_TARGET_OS, TARGET, RUNNER_OSRuntime detection if not set
<ARCH>DOTENVAGE_ARCH, EKG_ARCH, CARGO_CFG_TARGET_ARCH, TARGET, TARGETARCH, TARGETPLATFORM, RUNNER_ARCHNone if not detected
<USER>DOTENVAGE_USER, EKG_USER, GITHUB_ACTOR, GITHUB_TRIGGERING_ACTOR, GITHUB_REPOSITORY_OWNER, USER, USERNAMESystem username
<VARIANT>DOTENVAGE_VARIANT, EKG_VARIANT, VARIANTNone if not set
<PR_NUMBER>PR_NUMBER, GITHUB_REFGitHub Actions only

Supported Operating Systems

The <OS> placeholder supports these canonical values (with normalization):

CanonicalFile ExampleAliases (normalized to canonical)
linux.env.prod.linux-
macos.env.prod.macosdarwin, osx
windows.env.prod.windowswin32, win
freebsd.env.prod.freebsd-
openbsd.env.prod.openbsd-
netbsd.env.prod.netbsd-
android.env.prod.android-
ios.env.prod.ios-

Supported Architectures

The <ARCH> placeholder supports these canonical values (with normalization):

CanonicalFile ExampleAliases (normalized to canonical)
amd64.env.prod.amd64x64, x86_64
arm64.env.prod.arm64aarch64
arm.env.prod.armarmv7, armv7l, armhf
i386.env.prod.i386i686, x86
riscv64.env.prod.riscv64riscv64gc
ppc64le.env.prod.ppc64lepowerpc64le
s390x.env.prod.s390x-

Note: Custom architecture values (e.g., docker-s3) are passed through as lowercase and can include dashes within the value itself (e.g., .env.prod.docker-s3), but dots remain the separator between file name parts.

Example

Given these files:

# .env - Base config (safe to commit)
DATABASE_URL=postgres://localhost/dev
API_KEY=public_key

# .env.local - Local overrides (safe to commit with encryption)
DATABASE_URL=postgres://localhost/mydb
SECRET_TOKEN=age[...]  # encrypted, safe to commit!

Running dotenvage dump produces:

# .env
API_KEY=public_key
DATABASE_URL=postgres://localhost/dev

# .env.local
DATABASE_URL=postgres://localhost/mydb
SECRET_TOKEN=decrypted_value

Running dotenvage dump --export produces (note: --export automatically enables bash-compliant escaping):

# .env
export API_KEY=public_key
export DATABASE_URL=postgres://localhost/dev

# .env.local
export DATABASE_URL=postgres://localhost/mydb
export SECRET_TOKEN=decrypted_value

Dynamic Dimension Discovery

Dimension values (ENV, OS, ARCH, USER, VARIANT) can be discovered from loaded .env files, not just environment variables. This enables powerful chained configurations:

# .env - Sets the environment
NODE_ENV=production

# .env.production - Loaded because NODE_ENV=production was discovered
VARIANT=docker

# .env.production.docker - Loaded because VARIANT=docker was discovered
DOCKER_HOST=tcp://localhost:2375

The loader iteratively:

  1. Loads .env first
  2. Discovers dimension values from loaded variables
  3. Computes additional file paths based on discovered values
  4. Repeats until no new files are found

This allows you to set NODE_ENV=staging in .env and have .env.staging automatically loaded, which might set VARIANT=canary causing .env.staging.canary to load as well.

Note: Encrypted dimension values are skipped during discovery (they can't be decrypted until the key is loaded).

This layering system allows you to:

  • Commit ALL .env* files to version control - secrets are encrypted
  • Share environment-specific configs across the team (.env.production, .env.staging)
  • Provide user-specific overrides (.env.local.alice) without conflicts
  • Configure architecture-specific settings (.env.local.arm64)

Search documentation

Enter words to search this site.