# Environment Variables (env)

An environment's `env` block (under `environments.<name>.env`) overrides
environment variables for exactly three things:

- running services that declare `reload_on_prepare: true` — they are
  restarted by `prepare` (and by an in-place move to a new commit), so
  they always see the current chain's merged env;
- `lifecycle.prepare` commands, run fresh on every prepare;
- actions, which run with the prepare env plus that call's
  `INPUT_<NAME>` values.

```yaml
environments:
  beta-features:
    extends: default
    description: Same as default, with the timeline view turned on.
    env:
      FEATURE_FLAGS: timeline
```

It does NOT reach:

- `lifecycle.build` steps. Build steps run once into the repo-base layer,
  before any environment is chosen, so an environment's `env` does not
  exist at build time. A value your build bakes into its output (e.g. a
  front-end bundle's API URL or feature flags) can't vary by
  environment: read it at serve time instead, or set it with a seed
  action.
- services that don't declare `reload_on_prepare`. A service without it
  only ever sees the definition's top-level `env` and layer-time param
  values (from when its layer was built) — it is not restarted when a
  sandbox is prepared, so a per-environment override would never take
  effect for it.

An environment's `env` values are used as written: `${params.name}` is
substituted only in the definition's top-level `env`. To vary a value per
environment that must not live in the repository (an API key), use a
secret scoped to that environment: see the `secrets` topic.

## `env` merge order

Composition follows the same parent-then-child rule as seeds (see
`environments`): the resolved chain's env is the parent's `env` merged
first, then the child's own, so a child environment can override a
specific key its parent sets without having to repeat the rest. Secrets
(see the `secrets` topic) merge last, above every `env`.

## Relation to the definition's top-level `env`

The definition's top-level `env` (outside any environment) is the base:
fixed values and `${params.name}` substitutions that apply regardless of
which environment is claimed — `DATABASE_URL`, `PUBLIC_URL` and so on in
the example app. An environment's `env` is layered on top of that base
for the reachable surfaces above, not a replacement for it.
