🔧 Elixir Configuration & Environment variables
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
Intro
One of confusions for me when work with Elixir is configuration of Elixir application. That not same with common configuration style I worked before.
So this topic I will deep dive to configuration in Elixir for more clearly then newbies don't be confused when work with Elixir.
We will talk about two main things, one is application configuration (or application environment), another is OS environment.
Application configuration
Application configuration is so confused, That separated to compiled time & runtime. And compiled time has several type like dev, prod, test & general configuration people so confused because of this, from my view I think better rename it for less confusion.
Original of configuration is from Erlang OTP application environment variables (start with -env or in application *.app.src file). That is root cause make dev from other languages so confused.
In Elixir, we have compiled time & runtime configuration.
Usually, we have two way to declare configurations for an application.
The first one is add config directly to mix.exs file like:
def application do
[env: [media_path: "/public/media"]]
end
The second one is declare configuration in config files (recommended way). For this way we add configurations to config file config.exs, dev.exs, prod.exs, test.exs & runtime.exs.
Example config.exs file.
import Config
config :super_backend,
media: "/public/media"
import_config "#{config_env()}.exs"
For more details for config files. We have config.exs for common/generic configurations, dev.exs for development environment, prod.exs for product environment, test.exs for test environment (remember config/dev/prod/test config file is executed in compile time then we cannot change it after build application (but can override it, not recommended) and another file is runtime.exs for all environments & support change config at runtime.
For declare config in config files we need to understand two things. The first is when the configurations are load to code (compile time for config.exs, dev.exs, prod.exs & test.exs and runtime for runtime.exs). The second is environment of configurations (dev/prod/test env).
To test config in prod env we can run with MIX_ENV like:
MIX_ENV=prod mix phx.server
or put to env file:
export MIX_ENV=prod
export MEDIA_PATH="/public/media"
then run:
source .env.prod && mix phx.server
Another thing we need to know in here is all configurations can be delete/override in runtime (depends the way we load config to our code) then it can give a little of bit flexibility.
For this way, configuration is live inside Erlang VM (not related with OS environment) and config of an application can be override by other application (usually, and Elixir application like Phoenix will run several Elixir application in same Erlang VM).
OS Environment Variable
Some time we need get config from OS environment variables. For this case, we can get by function System.get_env
, for better code clean & easy to track we can push code to get config from OS env to runtime.exs and in application get config like above way.
Example for get config from OS env
.env file:
set MEDIA_PATH=/public/media
runtime.exs file:
config :super_backend,
media: System.get_env("MEDIA_PATH", "/default_public/media")
in Elixir code:
media_path = Application.get_env(:super_backend, :media)
Or if we don't want to declare in config file we can call directly like:
media_path = System.get_env("MEDIA_PATH", "/default_public/media")
Conclusion
Now we got the way of Elixir configuration works, I think better way is got some full examples/use cases but time is limited. I will back and add more in the future.
...
🔧 Elixir Configuration & Environment variables
📈 47.78 Punkte
🔧 Programmierung
🔧 Handling Environment Variables in Vite
📈 21.28 Punkte
🔧 Programmierung
🐧 audit full command line AND environment variables
📈 21.28 Punkte
🐧 Linux Tipps
🐧 links vs environment variables
📈 21.28 Punkte
🐧 Linux Tipps
🔧 Managing environment variables in Angular apps
📈 21.28 Punkte
🔧 Programmierung
🐧 Hacking with Environment Variables
📈 21.28 Punkte
🐧 Linux Tipps