Skip to main content
Version: Next

Env Files

Load environment variables from .env files. This is a common pattern for managing configuration, especially secrets that shouldn't be committed to version control.

Basic Usage​

{
name: "build"
env: {
files: [".env"]
}
implementations: [...]
}

With a .env file:

# .env
API_KEY=secret123
DATABASE_URL=postgres://localhost/mydb
DEBUG=false

Variables are loaded and available in your script.

File Format​

Standard .env format:

# Comments start with #
KEY=value

# Quoted values (spaces preserved)
MESSAGE="Hello World"
PATH_WITH_SPACES='/path/to/my file'

# Empty value
EMPTY_VAR=

# No value (same as empty)
NO_VALUE

# Multiline (use quotes)
MULTILINE="line1
line2
line3"

Optional Files​

Suffix with ? to make a file optional:

env: {
files: [
".env", // Required - error if missing
".env.local?", // Optional - ignored if missing
".env.secrets?", // Optional
]
}

This is useful for:

  • Local overrides that may not exist
  • Environment-specific files
  • Developer-specific settings

File Order​

Files are loaded in order; later files override earlier ones:

env: {
files: [
".env", // Base config
".env.production?", // Environment-specific overrides
".env.local?", // Local overrides (highest priority)
]
}

Example:

# .env
API_URL=http://localhost:3000
DEBUG=true

# .env.production
API_URL=https://api.example.com
DEBUG=false

# .env.local (developer override)
DEBUG=true

Result:

  • API_URL=https://api.example.com (from .env.production)
  • DEBUG=true (from .env.local)

Path Resolution​

Paths are relative to the invowkfile location:

project/
├── invowkfile.cue
├── .env # files: [".env"]
├── config/
│ └── .env.prod # files: ["config/.env.prod"]
└── src/

For modules, paths are relative to the module root, and every env file must resolve inside the module directory. Links on the path are followed, but a link that leads outside the module (including one under invowk_modules/), a link with an absolute target, or a link that does not resolve fails the command with env file resolves outside module. For a module, an optional ? file is skipped only when nothing exists at its path; a broken or escaping link still fails.

Env files of a plain invowkfile.cue (not in a module) are not restricted this way: a .env that is a link to a file elsewhere, such as ~/secrets/.env, keeps working.

Literal File Paths​

env.files entries are treated as literal paths (apart from the optional trailing ? suffix) and do not expand ${VAR} environment variables. To select per-environment files dynamically at runtime, pass --ivk-env-file on the command line or define separate commands/implementations.

Scope Levels​

Env files can be loaded at multiple levels:

Root Level​

env: {
files: [".env"] // Loaded for all commands
}

cmds: [...]

Command Level​

{
name: "build"
env: {
files: [".env.build"] // Only for this command
}
implementations: [...]
}

Implementation Level​

{
name: "build"
implementations: [
{
script: {content: "npm run build"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
env: {
files: [".env.node"] // Only for this implementation
}
}
]
}

Combined with Variables​

Use both files and direct variables:

env: {
files: [".env"]
vars: {
// These override values from .env
OVERRIDE_VALUE: "from-invowkfile"
}
}

Variables in vars always override values from files. All files are loaded first (root → command → implementation), then all vars are applied on top. See Precedence for the full order.

CLI Override​

Load additional files at runtime:

# Load extra file
invowk cmd build --ivk-env-file .env.custom

# Multiple files
invowk cmd build --ivk-env-file .env.custom --ivk-env-file .env.secrets

CLI files have highest priority and override all invowkfile-defined sources.

Real-World Examples​

Development vs Production​

{
name: "start"
env: {
files: [
".env", // Base config
".env.local?", // Local developer overrides (optional)
]
}
implementations: [{
script: {content: "node server.js"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}
// Override for production at runtime:
// invowk cmd start --ivk-env-file .env.production

Secrets Management​

{
name: "deploy"
env: {
files: [
".env", // Non-sensitive config
".env.secrets?", // Sensitive - not in git
]
}
implementations: [{
script: {content: """
echo "Deploying with API_KEY..."
./deploy.sh
"""}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}]
}

.gitignore:

.env.secrets
.env.local

Multi-Environment​

project/
├── invowkfile.cue
├── .env # Shared defaults
├── .env.development # Dev settings
├── .env.staging # Staging settings
└── .env.production # Production settings
cmds: [
{
name: "deploy staging"
env: {files: [".env", ".env.staging"]}
implementations: [{
script: {content: "./deploy.sh"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}]
},
{
name: "deploy production"
env: {files: [".env", ".env.production"]}
implementations: [{
script: {content: "./deploy.sh"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}]
},
]

Best Practices​

  1. Use .env for defaults: Base configuration that works for everyone
  2. Use .env.local for overrides: Developer-specific settings, not in git
  3. Use separate files per environment: Load .env.production or .env.staging via --ivk-env-file or dedicated commands
  4. Mark sensitive files optional: They may not exist in all environments
  5. Don't commit secrets: Add .env.secrets, .env.local to .gitignore

Next Steps​