Repository navigation
04 Environments
croft controls the environment variables your commands see. It
loads .env files, lets you compute values (e.g. a port that's
unique per git checkout), and forwards a project-declared subset
into container exec sessions. The same model drives recipe runs,
compose preflight, and the optional .env writer.
1. List your dotenv files in croft.toml:
[env_files]
files = [".env", ".env.local"].env.local overrides .env, both override the inherited shell.
2. Add a computed value:
[env]
LOG_LEVEL = { default = "info" }
APP_PORT = { base = "8080", offset = "CROFT_WORKTREE_OFFSET" }
DATABASE_URL = { from_command = "scripts/db-url.sh", required = true }-
defaultis used only if no earlier layer set it. -
base + offsetpicks a per-worktree port so two checkouts can run side-by-side. See Worktrees. -
from_commandruns a shell command and uses its trimmed stdout.
3. Verify what's resolved:
croft envPrints every key as KEY=VALUE lines.
-
Layers, later wins. The merge order is fixed: process env →
[env_files]dotenvs →[env]table → recipeenv =overrides. Once you know what's in each layer, you can predict the result. -
Host gets everything; containers get a subset. A
host-executed recipe inherits the whole merged set. A
containerised recipe (
in = "<service>"or under a devcontainer) only gets the project-declared keys forwarded via-e KEY=VAL— hostPATH,HOME, etc. stay out so they don't override the container's own setup. -
Per-key resolution has its own micro-rules. Inside
[env], one key can declare several fallbacks (value,base+offset,from_command,default); first match wins. Pre-existing values from earlier layers count too.
[env_files]
files = [".env", ".env.local"]${VAR} expansion inside dotenv values resolves against earlier
layers, so a value in .env.local can reference a value from
.env.
[env]
LOG_LEVEL = { default = "info" }If LOG_LEVEL is already in the process env or a dotenv, that
wins. Otherwise info.
[env]
EDITOR = { value = "vim" }Use value when you don't want the user's shell to be able to
override.
[env]
DATABASE_URL = { from_command = "scripts/db-url.sh", required = true }
GIT_SHA = { from_command = "git rev-parse --short HEAD" }The command runs once per croft invocation; stdout (trimmed) is
the value. required = true errors at preflight if no value
resolves.
[env]
APP_PORT = { base = "8080", offset = "CROFT_WORKTREE_OFFSET" }
DB_PORT = { base = "5432", offset = "CROFT_WORKTREE_OFFSET" }CROFT_WORKTREE_OFFSET is injected automatically (see Worktrees).
Two checkouts get different offsets → different ports → no
collisions when both stacks are up.
[command.test]
run = "composer test"
env = { XDEBUG_MODE = "off", APP_ENV = "testing" }Recipe env = is the last layer. Activated profiles
(--profile ci) can add their own override layer on top.
Container exec strips inherited process env on purpose. To
forward a host var explicitly, declare it under [env]:
[env]
GITHUB_TOKEN = { default = "" } # default empty string; picks up the shell value if setThis becomes part of the project-declared subset that's forwarded
with -e GITHUB_TOKEN=....
[worktrees]
dotenv = ".env"Every croft <anything> invocation re-writes the managed block in
.env so docker compose up (run directly), IDE launch configs,
and bin/rails s all see the same values. The write is
idempotent — when the contents already match, the file's mtime
stays put.
croft env --write .env does the same one-shot.
croft env # everything
croft env | grep '^APP_' # just APP_*Useful when you've layered three from_commands and can't
remember which one won.
In order, later wins:
- Inherited process env. Whatever shell variables croft was launched with.
-
Dotenv files, in
[env_files].filesorder.${VAR}expansion inside dotenv values resolves against earlier layers. -
[env]table, key by key (see per-key resolution below). -
Recipe
env =overrides for the currently running recipe (and any active profile overlay).
Each [env.<KEY>] is one of these shapes (fields combine inside
a single key declaration):
[env]
LOG_LEVEL = { default = "info" }
APP_PORT = { base = "8080", offset = "CROFT_WORKTREE_OFFSET" }
DB_URL = { from_command = "scripts/db-url.sh", required = true }
EDITOR = { value = "vim" }Resolution within one key, first match wins:
-
value— hard-set; trumps everything else. -
base + offset— integer-typed shorthand.value = base.parse::<i64>() + existing[offset].parse::<i64>(). Missingoffsetvar falls back tobase. Non-integer base errors. - Pre-existing process / dotenv value for the same name.
-
from_commandstdout (trimmed). -
default.
required = true with no resolved value errors at preflight.
Host: child process inherits the full merged set above —
PATH, HOME, cargo and git find what they expect.
Container (in = "<service>" or under a devcontainer): only the
project-declared subset is forwarded via -e KEY=VAL:
- Keys loaded from
[env_files]dotenv files. - Keys defined under
[env]. - Injected
CROFT_WORKTREE_*/COMPOSE_PROJECT_NAMEvars. - Per-recipe / per-script
env = {...}overrides.
Inherited host process env is intentionally not propagated —
leaking the host PATH via -e PATH=... overrides the
container's own and breaks command resolution (exec "sh": not found).
Always injected, ahead of [env] resolution:
| Key | Source |
|---|---|
CROFT_WORKTREE_SLUG |
Detected from the active git checkout. |
CROFT_WORKTREE_OFFSET |
Pinned ([worktrees.assign]) or hashed from the slug. |
COMPOSE_PROJECT_NAME |
<project>-<slug> when [worktrees].isolate_compose is on and the user hasn't set it. |
Set by the script and install-flow runners, outside the merge
pipeline. Reach .croft/commands/ scripts and .croft/install/
steps only — not [command.*] recipes, not the dotenv writer,
not croft env:
| Key | Source |
|---|---|
CROFT_PROJECT_DIR |
Host path to the worktree project root. |
CROFT_SCRIPT_DIR |
Host path to the script file's parent directory. |
Both are host-side paths even when the script runs inside a
service or devcontainer. Inline install steps get
CROFT_PROJECT_DIR only — there's no script file for
CROFT_SCRIPT_DIR to point at.
See Recipes and Scripts and Install Flow for the script contracts in full.
-
Worktrees for the slug + offset model that powers
base + offset. -
Hooks for how the post-checkout / post-merge auto-wiring
keeps
.envfresh. -
Configuration Reference:
[env].
croft is dual-licensed under MIT or Apache 2.0. Source: github.com/nsrosenqvist/croft. Back to Home.