Autoenv: Directory-based Environments 
Magic per-project shell environments
What is it?
If a directory contains a .env file, it will automatically be executed when you cd into it. When enabled (set AUTOENV_ENABLE_LEAVE to a non-empty string), if a directory contains a .env.leave file, it will automatically be executed when you leave it.
This is great for...
- auto-activating virtualenvs
- auto-deactivating virtualenvs
- project-specific environment variables
- making millions
You can also nest envs within each other. How awesome is that!?
When executing, autoenv, will walk up the directories until the mount
point and execute all .env files beginning at the top.
Usage
Follow the white rabbit:
$ echo "echo 'whoa'" > ./project/.env
$ cd ./project
whoaInstallation (automated)
Note that these are NEW instructions. If you are experiencing problems, please file an issue.
# with cURL
curl -#fLo- 'https://raw.githubusercontent.com/hyperupcall/autoenv/master/scripts/install.sh' | sh
# with wget
wget --show-progress -o /dev/null -O- 'https://raw.githubusercontent.com/hyperupcall/autoenv/master/scripts/install.sh' | shIf you encounter some variant of a curl: command not found or wget: command not found error, please install either cURL or wget (with your package manager) and try again.
Installation (manual)
Note that these are NEW instructions. If you are experiencing problems, please file an issue.
When installing manually, you first install autoenv with either Homebrew, npm, or Git. Then, you run a command to ensure autoenv is loaded when you open a terminal (this command depends on your default shell).
Installation Method
Using Homebrew
Prefer this if you're running macOS. Homebrew must be installed.
Click to expand content
First, download the autoenv homebrew formulae:
$ brew install 'autoenv'Then, execute run of the following to ensure autoenv is loaded when you open a terminal:
# For Zsh shell (default on macOS since Catalina)
$ printf '%s\n' "source $(brew --prefix autoenv)/activate.sh" >> "${ZDOTDIR:-$HOME}/.zprofile"
# For Bash shell (default on most Linux distributions)
$ printf '%s\n' "source $(brew --prefix autoenv)/activate.sh" >> ~/.bash_profileUsing npm
Prefer this if you're running Linux or an unsupported version of macOS. npm must be installed (usually through NodeJS).
Click to expand content
First, download the @hyperupcall/autoenv npm package:
$ npm install -g '@hyperupcall/autoenv'Then, execute run of the following to ensure autoenv is loaded when you open a terminal:
# For Zsh shell (default on macOS since Catalina)
$ printf '%s\n' "source $(npm root -g)/activate.sh" >> "${ZDOTDIR:-$HOME}/.zprofile"
# For Bash shell (default on most Linux distributions)
$ printf '%s\n' "source $(npm root -g)/activate.sh" >> ~/.bash_profileUsing Git
Use this if you cannot install with Homebrew or npm.
Click to expand content
First, clone this repository:
$ git clone 'https://github.com/hyperupcall/autoenv' ~/.autoenvThen, execute run of the following to ensure autoenv is loaded when you open a terminal:
# For Zsh shell (default on macOS since Catalina)
$ printf '%s\n' "source ~/.autoenv/activate.sh" >> "${ZDOTDIR:-$HOME}/.zprofile"
# For Bash shell (default on most Linux distributions)
$ printf '%s\n' "source ~/.autoenv/activate.sh" >> ~/.bash_profileConfiguration
Before sourceing activate.sh, you can set the following variables:
AUTOENV_AUTH_FILE: Authorized env files; defaults to~/.autoenv_authorizedif it exists, otherwise,~/.local/state/autoenv/authorized_listAUTOENV_ENV_FILENAME: Name of the.envfile; defaults to.envAUTOENV_LOWER_FIRST: Set this variable to a non-empty string to flip the order of.envfiles executedAUTOENV_ENV_LEAVE_FILENAME: Name of the.env.leavefile; defaults to.env.leaveAUTOENV_ENABLE_LEAVE: Set this to a non-empty string in order to enable source env when leavingAUTOENV_ASSUME_YES: Set this variable to a non-empty string to silently authorize the initialization of new environmentsAUTOENV_VIEWER: Program used to display env files prior to authorization; defaults toless -NAUTOENV_PRESERVE_CD: Set this variable to a non-empty string to prevent thecdbuiltin from being overridden (to active autoenv, you must invokeautoenv_initwithin acdfunction of your own)
Shells
autoenv is tested on:
- Bash
- Zsh
- Dash
- Fish is supported by autoenv_fish
- More to come
Disclaimer
Autoenv overrides cd (unless AUTOENV_PRESERVE_CD is set to a non-empty string). If you already do this, invoke autoenv_init within your custom cd after sourcing activate.sh.
Autoenv can be disabled via unset -f cd if you experience I/O issues with certain file systems, particularly those that are FUSE-based (such as smbnetfs).
Other info
To uninstall autoenv, see ./docs/uninstall.md.
To update autoenv, see ./docs/updating.md.
Attributions
Autoenv was originally created by @kennethreitz. Later, ownership was transfered to @inishchith. As of August 22nd, 2021, Edwin Kofler (@hyperupcall) owns and maintains the project.
