Environment inspection and serialization
fnox env --json builds the environment that a command or shell may receive and renders it as one machine-readable document. It gives process managers the same selection, secret resolution, lease, and filtering rules as fnox exec without starting the final process. See execution and environment integration for the wider model and command execution and environment injection for the child-process boundary.
Environment plan
The home/…/env.rs command turns the active profile into an environment plan. A plan separates values to set, values that need a file, inherited names to remove, unresolved names, and leases that ran. This lets a caller apply the result without depending on fnox's process model.
Scope selection
The scope defines which configuration entries may become environment values. exec matches the environment available to a command and can run leases. shell matches the interactive shell and excludes command-only secrets and all leases.
| Scope | Secret modes included | Lease behavior |
|---|---|---|
exec | env = true or unset, and env = "exec" | Selected leases may produce values |
shell | env = true or unset | No leases are selected |
Without --keys, the command selects every secret allowed by the scope. It also selects every lease for exec. With --keys, it selects only the named keys and keeps the first copy of a repeated key. A key produced by a lease is valid for exec, and the lease value takes priority over a same-name secret.
An env = false secret is not an injectable root. A secret with env = "exec" is not valid for shell. Unknown keys and keys outside the selected scope are rejected before provider work begins. The error can include close-name suggestions so a machine consumer can report a useful correction.
References: src/…/env.rs• L34-50 src/child_env.rs• L26-32 src/child_env.rs• L109-230 docs/…/env-json.md• L28-54
Dependency resolution
The plan resolves selected roots and the secrets needed to produce them. Dependencies can come from default interpolation, provider environment inputs, or the inputs of a selected lease. A dependency may be resolved without being emitted when its environment mode forbids injection.
The command does not resolve unrelated hidden secrets. This filtering avoids unnecessary provider access and keeps values such as an env = false lease input out of the result. A command lease has no fixed output names, so selecting one may require resolution of the whole profile; its produced keys appear only after the lease runs.
Resolution can use the daemon when the project enables it. The daemon and direct path produce the same document, while --describe skips resolution and daemon access. Temporary values used to create leases exist only during that internal step and do not become inherited values for the eventual consumer.
The following flow shows how selection becomes a child-environment document.
flowchart LR
A[Active profile and scope] --> B[Select roots and leases]
B --> C[Resolve roots and dependencies]
C --> D[Run selected exec leases]
D --> E[Assemble set files remove missing]
E --> F[Render schema 1 JSON]
References: src/child_env.rs• L233-276 src/child_env.rs• L395-492 src/daemon.rs• L1013-1098 docs/…/env-json.md• L56-62
Environment assembly
Assembly converts resolved values into instructions for a caller that already has an inherited environment. Ordinary values enter set. An as_file value enters files as raw content, so the caller can create a file and set the variable to that file path. The command itself does not leave those files behind.
The remove list protects the boundary in two ways. It removes credentials that fnox uses internally, such as ambient decryption inputs, and removes configured secrets that are outside the selected scope. A name is not removed when the plan also sets it or supplies it through files. Missing values are reported for requested or in-scope keys that resolve to no value when the missing-value policy permits the command to continue.
Consumers apply the plan in this order:
- Remove names in
removefrom the inherited environment. - Set values in
set. - Write each
filesvalue and set its key to the new file path.
References: src/child_env.rs• L278-391 crates/…/document.rs• L113-134 docs/…/env-json.md• L68-99
JSON contract
The JSON output is a versioned interface for tools that start processes themselves. The command writes one compact JSON document followed by a newline. Secret values are present in plain text in successful set and files documents, so callers must keep standard output out of logs.
Success document
The success document identifies the schema, fnox version, scope, and active profile. Its environment fields describe actions rather than a complete replacement environment.
| Field | Meaning |
|---|---|
schema | Breaking-contract version. The current value is 1. |
fnox_version | Version that created the document. |
scope | exec or shell. |
profile | Ordered active profiles used to resolve values. |
set | Plain values to add to the inherited environment. |
files | Raw contents for as_file values. |
remove | Inherited names to delete before adding values. |
missing | In-scope or requested names with no value under a non-error policy. |
leases | Lease names that actually ran. |
Map order follows the environment plan: configured secret values come in configuration order, and lease credentials follow them. A lease credential replaces a same-name ordinary secret. Consumers must ignore unknown fields within schema 1. A breaking change requires a new schema value.
References: crates/…/document.rs• L13-14 crates/…/document.rs• L113-159 src/child_env.rs• L495-514 docs/…/env-json.md• L64-99
Describe document
--describe provides an inspection view without resolving any secret. It shows each known key, whether it is a secret or lease result, its environment mode, file form, description, and whether exec or shell may inject it.
The describe result also names dynamic command leases whose output keys are not known before execution. It reports whether the project would use the daemon. Since this path performs no provider lookup and runs no lease, it is the safe interface for checking available keys without exposing values.
References: src/…/env.rs• L97-115 src/child_env.rs• L516-577 crates/…/document.rs• L162-247 docs/…/env-json.md• L101-145
Error document
Failures after --json produce a schema 1 error document on stdout and exit with status 1. The error kind identifies the stage that failed: configuration loading, key validation, or resolution. Key validation errors can include unknown keys, suggestions, and keys that are not injectable for the chosen scope.
Argument parsing errors occur before the JSON contract and use status 2 without a JSON document. Omitting --json is also a command error without JSON. Warnings, authentication prompts, and the human error report use stderr so stdout remains parseable.
References: src/…/env.rs• L52-80 crates/…/document.rs• L250-320 docs/…/env-json.md• L147-177
Consumer boundary
The output is an instruction set for a process manager such as mise. It is not a dotenv file and it is not the same contract as fnox export --format json: env preserves scope, missing keys, lease activity, and the caller-owned file step.
Consumers should treat the document as sensitive input. They should parse the schema, preserve the apply order, keep stdout private, and keep any files created from files alive for the process that uses them. A caller that needs shell state can request --for shell, but it still applies the result as environment changes rather than as a full environment replacement.
References: src/…/env.rs• L28-32 crates/…/document.rs• L80-116 docs/…/env-json.md• L5-18
Change checklist
When these files or sections are changed, remember to consider these:
- Keep scope filtering aligned with
EnvModein configuration,fnox exec, shell hooks, and the JSON describe result. - Preserve lease precedence when a lease output key matches a configured secret key.
- Keep dependency selection separate from output selection so hidden inputs can support resolution without entering
setorfiles. - Keep the
remove,set, andfilesapply order stable for every machine consumer. - Update the schema contract and client document types together if a breaking field or meaning changes.
- Keep direct resolution and daemon cache hits on the same document shape and filtering rules.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A caller logs stdout from a successful document | Secret values in set or files become exposed | Keep stdout private and send only the parsed plan to the process manager. |
A caller applies set before remove | An out-of-scope inherited value can remain or override the intended state | Apply remove, then set, then files. |
A caller treats files values as paths | The target receives content where it expects a file name | Write each file value and set the key to the created path. |
--keys names an env = false or shell-incompatible secret | The command returns an invalid_keys document instead of resolving it | Use an injectable scope or use fnox get for non-injectable values. |
A required provider value is missing under if_missing = "error" | The command returns a resolution error and no success plan | Use an intentional missing-value policy and fix the provider input. |
| A consumer rejects unknown schema fields or requires an exact field set | A compatible schema 1 document may fail to load | Ignore unknown fields and reject only unsupported schema versions. |
Key files
| File | Why to read it |
|---|---|
| src/…/env.rs | Command entry point, scope flags, phase errors, and JSON output selection. |
| src/child_env.rs | Shared selection, dependency planning, environment assembly, and document creation used by env and exec. |
| crates/…/document.rs | Versioned success, describe, and error document types for machine consumers. |
| src/daemon.rs | Daemon cache path and its equivalence with direct environment document creation. |
| src/…/exec.rs | The process consumer that applies the same environment plan to a child command. |
| docs/…/env-json.md | User-facing JSON fields, filtering rules, streams, and compatibility contract. |
test/env_json.bats | End-to-end guarantees for filtering, dependencies, files, errors, leases, and daemon reuse. |