Как установить direnv: .envrc, allow и автозагрузка переменных окружения

direnv + .envrc + allow — env по каталогам.

У каждого репозитория свои ключи и PATH — direnv читает .envrc при cd. Материал: its own API keys, database URLs, and PATH tweaks, but manually sourceing .env files gets old. direnv reads .envrc in a project folder: cd in to load environment variables, cd out to unload. установка macOS/Linux, shell hook, основы .envrc, direnv allow, авто load/unload для читателей hereink.

The query is how to install direnv and write .envrc to load project env vars automatically. You should finish with direnv installed, a first .envrc, direnv allow at the repo root, and variables appearing when you enter the directory and disappearing when you leave.

installbrew · apt · nix
config.envrc · allow
behaviorcd in · cd out

Что решает direnv

Per-directory environments

Global export pollutes every shell. Variables that belong to one repo should mount with that directory. direnv injects on cd and rolls back on exit, which cuts down wrong-script mistakes from stale PATH.

Works with .env files

.envrc can dotenv an existing .env or use plain export. Keep secrets in .gitignore; commit a safe .envrc template without live tokens.

Upstream: github.com/direnv/direnv, docs at direnv.net. Run direnv version after install.

Установка direnv

  1. 1
    macOS (Homebrew)

    brew install direnv. Verify with which direnv and direnv version.

  2. 2
    Linux (distro packages)

    Debian/Ubuntu: sudo apt update && sudo apt install direnv. Fedora: sudo dnf install direnv. Arch: sudo pacman -S direnv.

  3. 3
    Shell hook (required)

    Add to ~/.zshrc or ~/.bashrc: eval "$(direnv hook zsh)" (use bash for bash). Open a new terminal or source the file.

  4. 4
    fish

    In ~/.config/fish/config.fish: direnv hook fish | source. See official docs for other shells.

Основы .envrc

.envrc is a shell snippet at the project root (or a subdirectory). Common patterns:

export API_BASE=https://api.staging.example.com
export NODE_ENV=development

PATH_add ./bin

dotenv_if_exists

Do not commit real tokens. Teams can commit non-secret defaults in .envrc and keep secrets in local .env loaded via dotenv_if_exists.

Allow в каталоге проекта

The first time you enter a folder with .envrc, direnv blocks execution until you explicitly trust the file:

cd ~/projects/my-app
# direnv: error .envrc is blocked. Run `direnv allow` to approve its content.

direnv allow
# direnv: loading ~/projects/my-app/.envrc
echo $API_BASE

direnv allow stores a hash whitelist under .direnv/. After you edit .envrc, run direnv allow again. That is intentional: a cloned repo cannot silently change your environment.

Автозагрузка и снятие

With the hook installed, you do not need manual source each time:

cd ~/projects/my-app    # loads .envrc
env | grep API_BASE

cd ~                    # unloads project vars
env | grep API_BASE     # should be empty

Nested directories: a child .envrc applies when you enter that path. Monorepos often use a root .envrc with PATH_add for shared tooling—see source_up in direnv docs.

vs ручной source .env

ApproachWhen it loadsOn leave
source .envManual every timeVars stay in shell
direnv + .envrcOn cdAuto unload
IDE injectionOpen projectDepends on IDE
Подходит
Switching repos, per-project PATH, staging vs prod endpoints tied to folders.
Не подходит
One global export forever, or CI images—set env in Dockerfile or workflow instead.
.envrc changed but nothing updates?

Run direnv allow or direnv reload. direnv compares hashes so silent tampering cannot pass.

Conflict with asdf or mise?

They can coexist: version managers live in the shell; direnv adds per-directory PATH and vars. Many teams call tool-specific direnv integrations from .envrc—check each tool’s docs.

Windows support?

Official focus is Unix shells. Use WSL and follow Linux steps. Native PowerShell support is limited—see current direnv documentation.

После env — схема на доске

Once staging endpoints make sense in the terminal, service boundaries still belong on a shared canvas. See как создать пространство.

Начать