Activate environments when you change directory
This page shows how to make your shell switch a project's environment on when you enter the project's directory and off when you leave, the way direnv does.
Before you start
- Install reprobuild.
- The project's
repro.nimdeclares a development environment.
Steps
1. Load the hook
Load the hook from your shell's start-up file, once:
Add to ~/.bashrc:
eval "$(repro shell hook bash)"Add to ~/.zshrc:
eval "$(repro shell hook zsh)"Add to ~/.config/fish/config.fish:
repro shell hook fish | sourceAdd to your $PROFILE:
repro shell hook pwsh | Out-String | Invoke-ExpressionSave the output of repro shell hook nushell to a file and source it from your Nushell environment configuration.
2. Open a new shell
Open a new shell, so the start-up file runs.
3. Allow the project directory
Enter the project's directory and allow it:
cd my-project
repro allowThe environment switches on. From now on it switches on whenever you enter this directory, and off when you leave it.
Trust follows the directory, not its contents. After repro allow, a change to the project's repro.nim, such as one that arrives with git pull, takes effect the next time you enter the directory, without asking you again. Allow only directories whose changes you would run yourself.
Withdraw trust
In the project's directory, run:
repro denyThe hook no longer activates the environment there. repro allow and repro deny also take a directory as an argument.
Skip the trust check where no one can answer it
In CI and other non-interactive shells, set REPRO_DEV_ENV_AUTO_ALLOW=1 to activate environments without repro allow.
With REPRO_DEV_ENV_AUTO_ALLOW=1, every directory with a project file is activated, including one you have never inspected. Set it only in a disposable environment that runs code you already trust.
Check that it worked
Enter the project directory and run a tool the project declares, such as its compiler, with --version. Leave the directory and run it again: the version changes back to your system's, or the command is not found.
Related
- Enter a project's environment, to use an environment without the hook
- Coming from direnv, devenv and mise
- Trust in the glossary