A better alternative to .env files

Bilal
3 min readFeb 29, 2024

--

Photo by Arnold Francisca on Unsplash

As developers, we regularly use environment variables to set configuration parameters especially when running programs and scripts locally. One way to handle this is to use a .env file. Often you might have multiple .env files, especially when switching between different environments. For example, you might be running some script that can send requests to production or a staging service depending on the environment variables. One way is to have two .env files and load the appropriate one. A better way that I found is to use envio.

The main benefits of envio is

  1. Seamless switching between environments
  2. It’s more secure since instead of a .env file. You would have your env encrypted either using a passphrase or GPG encryption
  3. Run programs with your env profiles
  4. List, load, and unload env profiles easily

Installation

You can install envio using the following commands on MacOS. Instructions for Windows and Linux are in envio’s documentation.

$ brew tap envio-cli/tap
$ brew install envio gnupg gpgme

For now, GPG installation is compulsory even if you aren’t going to use it.

Let’s dive into a quick crash course on the most useful subcommands of envio.

Creating a profile

If you have an env file like this

AUTH_TOKEN=staging-token
SERVICE_URL=https://staging.service.com

You can create envio profile using the following command. You would be asked to enter a passphrase (I am using passphrase based encryption)

$ envio create staging_env -f .staging-env

If instead, you want to create a profile using CLI input directly then that’s also possible

$ envio create production_env
$ envio add production_env -e AUTH_TOKEN SERVICE_URL=https://production.service.com
Loading Profile
Enter your encryption key
> Enter your encryption key: ********
> Enter the value for AUTH_TOKEN: prod-token
Applying Changes

Add a variable to an existing profile

envio add production_env -e DATABASE_URL=postgres://productiondb/mydb

Update a variable in an existing profile

$ envio update staging_env -e SERVICE_URL=https://staging-new.service.com

Load a profile

$ envio load staging_env
Reload your shell to apply changes or run `source ~/.zshrc`

After loading you might also need to source the rc file as the message I got on my Mac.

List available profiles

$ envio list --profiles
+----------------+
| Profiles |
+================+
| staging_env |
|----------------|
| production_env |
+----------------+

Inspective environment variables in a profiles

$ envio list -n production_env
Loading Profile
Enter your encryption key
> Enter your encryption key: ********
+----------------------+--------------------------------+
| Environment Variable | Value |
+=======================================================+
| AUTH_TOKEN | prod-token |
|----------------------+--------------------------------|
| SERVICE_URL | https://production.service.com |
|----------------------+--------------------------------|
| DATABASE_URL | postgres://productiondb/mydb |
+----------------------+--------------------------------+

Remove a variable from a profile

$ envio remove staging_env -e SERVICE_URL

Unload a profile

$ envio unload staging_env 

or

$ envio unload

Delete a profile

envio remove staging_env

Exporting profile to an env file

$ envio export staging_env -f .staging_env_file

Launching a program with a profile

Lastly, you can launch a script with a specific profile. This is very useful for scripts

$ envio launch staging_env 'python my_script.py'

That’s all from me for this quick crash course on a useful little tool. Hope you found this useful.

--

--

Bilal
Bilal

Written by Bilal

Learning new things everyday. Writing about things that I learn and observe. PhD in computer science. https://www.linkedin.com/in/mbilalce/

No responses yet