Environment Variables
Learn about environment variables and how to use them in your projects.
Introduction
Environment variables are a fundamental concept in programming and software development. They are key-value pairs that are stored in the environment that a process runs in. They are often used to store configuration settings and sensitive information like API keys, database passwords, and other secrets.
Most languages have a pre-defined way to access environment variables. Here's a few examples:
The key advantage of using environment variables is that they can (and should) be set outside of the application code. This allows them to hold sensitive information without exposing it in the codebase. For this reason, environment variables should never be committed to version control.
Runtime Environment Variables
Sometimes, you may want to set environment variables before or at runtime. This can be done from the command line before running the application, or programmatically at runtime.
OnlyNv Specifics
OnlyNv offers a suite of extensions to the standard environment variable system.
Environments
OnlyNv allows you to do more than just have a single set of variables in your environment. With prefixes, you can assign multiple values to a single variable name. This allows you to have different values for different environments, such as development, staging, and production.
Be Careful
All variables, including those that are environment-specific, are injected into your local runtime. Do not include any production credentials that you are not comfortable with being exposed in your local environment.
When a sync is performed, OnlyNv will automatically inject the correct environment variables based on the current environment. This allows you to easily switch between different environments without having to manually change your environment variables.
Templates
By default, OnlyNv will not persist formatting in your .env
file. This means that if you have your variables formatted in a certain way, it will be lost when you sync your environment variables.
In order to persist formatting, you must provide a template file. Usually, this will be a .env.example
file that contains the formatting you want to keep. OnlyNv will use this file as a template when syncing your environment variables.
To automatically create a template file, you can use nv strip
:
By default, OnlyNv will look for template files matching the following patterns:
{file}.example
{file}.template