Shell hooks and activation
Shell integration lets fnox load and remove secret values as a user moves through a project tree. The activate command prints code for the current shell, and that code connects prompt or directory events to hook-env; the broader process fits under execution and environment integration, differs from one-shot command execution and environment injection, and shares environment rules with environment inspection and serialization.
Activation flow
Activation is a code-generation step. The user evaluates or sources the printed code in the current shell so that fnox can change that shell's environment rather than a child process.
The activation and refresh relationship is a loop: generated shell code calls hook-env, and the returned changes are applied before the next prompt.
flowchart LR
A[Shell startup] --> B[fnox activate]
B --> C[Generated wrapper and hook]
C --> D[Prompt or directory event]
D --> E[fnox hook-env]
E --> F[Shell commands or JSON changes]
F --> G[Current shell environment]
G --> D
Shell selection
fnox activate [SHELL] accepts bash, zsh, fish, nu, pwsh, or powershell for PowerShell. If the shell name is not given, fnox first checks FNOX_SHELL and then uses the final name in SHELL, such as bash from /bin/bash; an unknown name stops activation with an error. activate also embeds the path of the fnox executable that will serve later hook calls. References: src/…/activate.rs• L5-41, src/…/mod.rs• L89-116
Activation output
The generated code marks the session with FNOX_SHELL and defines a fnox shell wrapper. Commands that need to change the current shell, such as fnox shell and fnox deactivate, are evaluated in that shell; other commands run as normal child processes. With --no-hook-env, activation still defines the wrapper but does not register automatic secret loading. In Bash, Fish, and Zsh, the wrapper can remove its hooks when the fnox executable is no longer available, while it keeps session state for later cleanup. References: src/…/mod.rs• L28-86, src/…/bash.rs• L7-74, src/…/zsh.rs• L7-113, src/…/fish.rs• L7-67, src/…/pwsh.rs• L7-120, src/…/nushell.rs• L7-88
The normal setup is one command in the shell's startup file. Bash and Zsh use eval, Fish uses source, PowerShell evaluates the generated text, and Nushell saves or loads the generated script as a vendor autoload file. The integration only becomes active after the shell evaluates this output. References: docs/…/shell-integration.md• L5-40, src/…/activate.rs• L30-41
Generated shell integrations
Each supported shell receives the same product behavior through its own event model. The integration sets environment values in the current shell, but the event that starts a refresh and the form used to apply changes depend on the shell.
Bash integration
Bash adds _fnox_hook to PROMPT_COMMAND, so fnox checks the environment before each prompt. The fnox wrapper evaluates output for shell-changing commands and runs all other commands directly. If the executable is missing, the deactivate path still removes the wrapper and prompt hook without requiring a child process; it leaves session state available for a later cleanup. References: src/…/bash.rs• L7-105
Zsh integration
Zsh runs its hook before prompts and after directory changes. It also registers an exit hook that asks fnox to clean up persistent files made for file-based secrets. A child Zsh discards its inherited encoded session and records its own process identity, so child cleanup does not remove files owned by the parent shell. If the executable is missing, the local hooks can still be removed while the session marker remains for later cleanup. References: src/…/zsh.rs• L7-152
Fish integration
Fish runs the hook on fish_prompt and when PWD changes. Activation performs an initial hook call, which loads secrets for the directory that was active when integration started. Deactivation removes both event handlers before it removes the wrapper and fnox environment markers. If the executable is missing, the wrapper still removes the Fish integration without starting a child process. References: src/…/fish.rs• L7-116
PowerShell integration
PowerShell wraps the global prompt function and calls the fnox hook before the existing prompt. It also attaches to location changes, so directory changes can refresh the environment before the next prompt. The generated wrappers retain the prior prompt and location handler, run only the newest fnox hook, and forward control to other tools that changed those handlers. The hook applies generated PowerShell commands in the current session and can print a compact change summary after those commands run. Deactivation restores a prior handler only when fnox still owns the active one, then removes the fnox functions and environment markers. References: src/…/pwsh.rs• L7-143, src/…/pwsh.rs• L145-203
Nushell integration
Nushell does not use eval for environment changes. Its wrapper and prompt hook call fnox for structured JSON, then apply the set and unset entries with Nushell environment commands. The hook runs at pre_prompt; directory changes cause a new prompt, so a second directory hook is not needed. References: src/…/nushell.rs• L7-145
Hook environment
hook-env is the refresh operation behind automatic loading. It finds the applicable configuration, resolves the secrets allowed in the interactive shell, compares that result with the last hook session, and prints only the environment changes that the active shell can apply.
Configuration discovery
The hook searches from the current directory toward the file-system root for the active configuration names and falls back to the global configuration when no local file exists. It uses the selected profile and the normal configuration hierarchy, so a child project can receive both inherited and local secret values. References: src/hook_env.rs• L213-252, src/hook_env.rs• L291-323, src/…/hook_env.rs• L227-290
Secret eligibility
Only secrets marked for shell injection enter hook output. Secrets configured for exec-only use remain available to fnox exec but do not enter the interactive shell, while secrets marked as never injected are not placed in an environment variable. A file-based secret enters the shell as a path to a temporary file instead of as its content. References: crates/…/config.rs• L523-555, src/…/hook_env.rs• L294-339
Session state
Each successful refresh stores a compact __FNOX_SESSION value in the shell. The session records enough state to find directory, configuration, secret, and temporary-file changes without storing the secret values themselves in the change record. The next hook uses this state to remove values that no longer apply and to retire old temporary files. References: src/hook_env.rs• L9-43, src/hook_env.rs• L55-127, src/…/hook_env.rs• L105-126, src/…/hook_env.rs• L342-357
Refresh conditions
The hook can return with no output when the directory, configuration hierarchy, and relevant FNOX_* environment state are unchanged. It refreshes when the user changes directory, changes or removes a local or parent configuration, changes the selected profile, or changes another tracked fnox setting. This avoids repeated provider work while keeping environment state aligned with the project location. References: src/hook_env.rs• L154-211, src/hook_env.rs• L268-289
Shell output
For Bash, Zsh, Fish, and PowerShell, the hook prints shell commands that set new values, unset removed values, and update __FNOX_SESSION. Nushell receives JSON with set and unset collections because its integration applies structured data in the current shell. The FNOX_SHELL_OUTPUT setting controls the change summary: normal output shows counts and names, debug output adds detail, and none suppresses the summary while keeping the environment update. References: src/…/mod.rs• L51-86, src/…/pwsh.rs• L145-203, src/…/nushell.rs• L107-137, src/…/hook_env.rs• L13-36, src/…/hook_env.rs• L108-132, docs/…/shell-integration.md• L60-73
Hook error behavior
A hook refresh does not break the interactive shell when configuration or secret resolution fails. fnox reports the problem through its warning path, skips the failed secret set, and still returns session output so later hooks can retry after the cause changes. References: src/…/hook_env.rs• L82-103, src/…/hook_env.rs• L273-291, test/shell-integration.bats#L509-L540
Deactivation lifecycle
Deactivation ends integration in the current shell. The command emits shell-specific cleanup output, while the wrapper evaluates or applies that output so hooks and environment values disappear from the parent shell.
Current-session cleanup
fnox deactivate requires an active FNOX_SHELL marker and detects the shell from that marker. With the executable available, it removes the secret names held by the prior session, cleans up persistent hook files, removes event hooks, and clears FNOX_SHELL and __FNOX_SESSION; it does not change other shell processes or future shell startup files. Bash, Fish, and Zsh also have a local fallback that removes their integration when the executable is unavailable, but that fallback preserves session state because it cannot remove the session's secrets or files. References: src/…/deactivate.rs• L8-43, src/…/bash.rs• L65-105, src/…/zsh.rs• L41-145, src/…/fish.rs• L23-99, src/…/pwsh.rs• L123-143, src/…/nushell.rs• L34-55, src/…/nushell.rs• L128-137
Shell exit cleanup
Zsh has an extra exit path for persistent file-based secrets because those files can outlive the short hook-env process. Deactivation cleans them before the session marker is discarded, and the Zsh exit hook provides cleanup when the user closes the shell without explicit deactivation. References: src/…/zsh.rs• L67-110, src/…/deactivate.rs• L30-40, src/…/hook_env.rs• L138-181
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the shell name accepted by
activate, shell detection, generatedFNOX_SHELL, andhook-env -saligned. - Keep each shell's activation, hook output, and deactivation output in the same data model. Eval-based shells consume commands, while Nushell consumes JSON.
- Keep
__FNOX_SESSIONupdates paired with secret add and remove operations. A stale session can leave old values or temporary files in the shell. - Keep event registration and event removal symmetric. Prompt hooks, directory-change hooks, prompt wrappers, and exit hooks must not remain after deactivation.
- Keep shell eligibility rules aligned with the
envmode contract so exec-only secrets do not leak into hook output. - Keep file-based secret cleanup tied to session ownership, especially for nested Zsh sessions.
- Keep prompt and location-handler ownership checks paired with restoration logic in PowerShell so another tool's active handlers are not replaced or called recursively.
- Keep missing-executable cleanup separate from normal deactivation. The fallback must remove local integration without discarding session state needed for later cleanup.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Activation output is run as a child process instead of evaluated or sourced | The parent shell gets no wrapper, hooks, or secret values | Use the shell-specific startup form from the shell integration guide |
activate receives an unsupported shell name or cannot detect one | No integration code is generated | Pass one of the supported shell names or set a usable SHELL/FNOX_SHELL value |
| A hook runs after the user changes directory or profile but the shell does not apply its output | Old secrets remain or new secrets do not reach the current shell | Use the generated wrapper and hook for that shell |
| A secret is configured for exec-only or never-injected use | The value is absent from shell integration by design | Use fnox exec or fnox get for the intended access path |
| A session-owned temporary file is removed before its shell session ends | A file-based secret points to a missing path | Let the matching shell session perform deactivation or exit cleanup |
| The fnox executable disappears and the shell uses normal deactivation | The shell cannot ask fnox to remove loaded values or files | Use the shell's local fallback to remove hooks, then reactivate fnox before cleanup is needed |
Deactivation is called without FNOX_SHELL | fnox refuses to guess which integration to remove | Activate the current shell first or remove an inactive startup entry manually |
| A PowerShell prompt or location handler changes after activation | Deactivation could remove another tool's handler or create a recursive chain | Compare the active handler with fnox's saved wrapper before restore |
Key files
| File | Why to read it |
|---|---|
| src/…/activate.rs | Defines activation inputs, shell detection, and generated-script output |
| src/…/hook_env.rs | Connects configuration and secret resolution to refresh, change reporting, and session updates |
| src/…/deactivate.rs | Defines current-session shutdown and temporary-file cleanup order |
| src/hook_env.rs | Defines session state, refresh checks, configuration search, and tracked environment state |
| src/…/mod.rs | Defines the shared shell contract and supported shell selection |
| src/…/bash.rs | Shows the Bash prompt hook, eval-based environment updates, and missing-executable cleanup |
| src/…/zsh.rs | Shows prompt, directory, nested-session, exit, and missing-executable behavior |
| src/…/fish.rs | Shows Fish event hooks, initial refresh, and current-shell cleanup |
| src/…/pwsh.rs | Shows PowerShell prompt and location hooks, handler ownership, and change display |
| src/…/nushell.rs | Shows structured JSON application without eval |
| crates/…/config.rs | Defines whether a secret can enter shell or exec environments |
| docs/…/shell-integration.md | Shows the user-facing setup forms and lifecycle behavior for each shell |