Lädt...


🔧 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

🔧 Elixir Task, Task.Supervisor - Another way to work with Elixir process


📈 32.88 Punkte
🔧 Programmierung

🔧 Blocker: How to load environment variables on broswer environment using webpack.config.js


📈 29.84 Punkte
🔧 Programmierung

🔧 Normal variables and bind variables in ORACLE PLSQL


📈 25.45 Punkte
🔧 Programmierung

🔧 Refactoring instance variables to local variables in Rails controllers


📈 25.45 Punkte
🔧 Programmierung

🐧 Shells & Setting Environment Variables Permanently in Linux [Noobs]


📈 23.3 Punkte
🐧 Linux Tipps

🐧 Configuring permanent environment variables in Ubuntu


📈 21.28 Punkte
🐧 Linux Tipps

🔧 Automate testing with Postman using environment variables


📈 21.28 Punkte
🔧 Programmierung

🔧 How to validate environment variables in Node.js?


📈 21.28 Punkte
🔧 Programmierung

🐧 Environment Variables that I put in ~/.profile don't work.


📈 21.28 Punkte
🐧 Linux Tipps

🔧 Handling Environment Variables in Vite


📈 21.28 Punkte
🔧 Programmierung

🔧 Scan your codebase for leaked environment variables instantly


📈 21.28 Punkte
🔧 Programmierung

🔧 Validating Environment Variables with Joi in NodeJS


📈 21.28 Punkte
🔧 Programmierung

🔧 vite build accessing system environment variables


📈 21.28 Punkte
🔧 Programmierung

🐧 audit full command line AND environment variables


📈 21.28 Punkte
🐧 Linux Tipps

🔧 Data Source Environment Variables in Power Automate actions


📈 21.28 Punkte
🔧 Programmierung

🔧 How to Pass Environment Variables to a YAML File in a Node.js Application using EJS Templating Engine


📈 21.28 Punkte
🔧 Programmierung

🔧 vite build accessing system environment variables


📈 21.28 Punkte
🔧 Programmierung

🐧 links vs environment variables


📈 21.28 Punkte
🐧 Linux Tipps

🔧 Environment Variables: Naming Conventions and Runtime Access in C# .NET


📈 21.28 Punkte
🔧 Programmierung

🔧 AWS Lambda Cookbook — Elevate your handler’s code — Part 4 — Environment Variables


📈 21.28 Punkte
🔧 Programmierung

🔧 How to use multiple files for configuring environment variables in React


📈 21.28 Punkte
🔧 Programmierung

🔧 React Environment Variables: A Complete Step-By-Step Guide


📈 21.28 Punkte
🔧 Programmierung

🔧 Managing environment variables in Angular apps


📈 21.28 Punkte
🔧 Programmierung

🐧 How to Replace Environment Variables Using the envsubst Command


📈 21.28 Punkte
🐧 Linux Tipps

🔧 A Complete Guide: How to Use Environment Variables in Postman


📈 21.28 Punkte
🔧 Programmierung

🔧 Optimize DX with Typesafe Environment Variables and code Autocomplete


📈 21.28 Punkte
🔧 Programmierung

🐧 How to Set and Use Environment Variables In Bash Script


📈 21.28 Punkte
🐧 Linux Tipps

🐧 Hacking with Environment Variables


📈 21.28 Punkte
🐧 Linux Tipps

🔧 Why do I need to store environment variables in a separate file when going to production?


📈 21.28 Punkte
🔧 Programmierung

matomo