Environment Configuration (Nex.Env)
Nex provides a simple yet powerful environment configuration system that not only automatically loads environment variables but also intelligently locates the project root directory in development environments.
1. Core Features
-
Auto-loading: Automatically searches for and loads the
.envfile in the root directory upon project startup. -
Multi-environment Support: Supports multiple suffixes like
.env,.env.local,.env.dev(loading order:.envfirst, subsequent files override previous ones). -
Smart Root Directory Detection: Uses the
Nex.Env.root_path/0algorithm to automatically handle path differences between development environments (like the_builddirectory) and source code directories.
2. Usage
You can safely retrieve configuration via Nex.Env.get/2.
Example
Define in .env:
STRIPE_API_KEY=sk_test_12345
MAX_RETRY=5
Read in code:
# Get string configuration
api_key = Nex.Env.get("STRIPE_API_KEY")
# Provide a default value
retry_count = Nex.Env.get("MAX_RETRY", "3") |> String.to_integer()
3. Smart Root Directory Detection
Because Elixir compiled files are usually located in the _build directory, using File.cwd! directly sometimes fails to find the project root correctly. Nex uses a recursive detection algorithm:
- Starts searching upwards from the current working directory.
-
Looks for folders containing
mix.exsor.env. - Ensures that the developer can correctly load configuration files when starting the project (or running tests) from any location.
4. Best Practices
-
Don’t commit
.envto Git: You should ignore.envin your.gitignoreand provide a.env.exampletemplate. -
Type Conversion: Environment variables are always stored as strings. After reading, please convert them as needed using functions like
String.to_integer/1. - Production Environment: On Docker or cloud platforms, it’s recommended to set operating system-level environment variables directly; Nex will prioritize reading these.