vrod

vrod

Forgive me for maybe a noob question. I am reading much about configuration – maybe this in Elixir is one of the most confusing things? I read this Configuring your Elixir Application at Runtime with Vapor | AppSignal Blog and it says “if it can be set at runtime, it should be set at runtime.” This makes much sense to me – this is how other languages work with .env files – but maybe the comparison is not fair with dynamic languages?

My question is what things can not be runtime config? I imagine a very simple setup from working with PHP where there was only 1 config and 2 .env files (regular .env and a .test.env) and the test suite would know to use the .test.env. But dynamic languages do not have the same concerns. I am thinking maybe a config for a module name like saying which HTTP client the app should use. A system ENV maybe has string for “HTTPoison”, but you must convert this to atom like Elixir.HTTPoison. It is ugly, but I think this works fine. What things will not work this way and must be set in a config file for compile time config?

Thank you for your replies!

Showing Posts 1 to 10

lud

lud

The first thing I can think off are modules names that macros will be used from, or will be modules generated by macros.

Basically, anything that changes the compilation result.

vrod

vrod OP

How to know what modules have macros?

lud

lud

Well if we are talking about your application then you already know it. And if we are talking about a library then it is either documented in the configuration section of the documentation, if it exists, or you have to dive in the code:

Module that define macros call defmacro. Module that use macros call use SomeModule or require SomeModule.

I am not sure if import SomeModule also “requires” the module to be able to call its macros.

Unfortunately this only gets you so far…

That is why, when they say " if it can be set at runtime, it should be set at runtime", then I would say “yes, if it is environment dependent (a hostname, a port, a database connection string, anything that you would put in your .env files)”. Otherwise, if it is something that doesn’t change, the compile-time config is fine.

vrod

vrod OP

I am wondering how to move most configs into runtime.exs. This works simple for local running or on production server when real values are required. runtime.exs looks something like

import Config

config :myapp,
  x: System.get_env("X"),
  y: System.get_env("Y"),
  z: System.get_env("Z"),
  # ... etc...

But how to override this for testing?

RudManusachi

RudManusachi

You can use Config.config_env/0 to differentiate between envs

import Config
... 
if config_env() == :test do
  config :myapp, foo: "bar"
end
lud

lud

You can use Application.put_env in your tests directly.

vrod

vrod OP

Forgive me for my struggle to understand a good solution. I think my options are to do maybe these:

Option 1: fill runtime.exs:

if config_env() == :test do
  config :myapp, 
      x: "test-x",
      y: "test-y",
      z: System.get_env("TEST_Z", "test-z"),
     # ... etc...
else
  config :myapp, 
      x: System.get_env("X"),
      y: System.get_env("Y"),
      z: System.get_env("Z"),
     # ... etc...
end

Pro: declarative, clear
Con: double long runtime.exs. Also if-statements in configs are always for me a smell.

Option 2: use test_helper.exs

Use simple runtime.exs and add test values to test_helper.exs like maybe this:

  config :myapp, 
      x: System.get_env("X"),
      y: System.get_env("Y"),
      z: System.get_env("Z"),
     # ... etc...

and then in test_helper.exs I can done Application.put_env like

Application.put_env(:myapp, :x, "test-x")
Application.put_env(:myapp, :y, "test-y")
Application.put_env(:myapp, :z, "test-z")

Pro: simple runtime.exs (no double, no if-statement)
Con: cannot enforce System ENV vars using System.fetch_env! – only get_env would work so that execution does not stop before test_helper.exs runs and a value is supplied.

Option 3: Vapor

I looked also at Vapor. I like this: it could replace runtime.exs 100% I think. But it is still a problem when you must manually take a value and use Application.put_env to put it into the correct place so your app and deps can find it. For example, imagine a dependency with config like this:

config :some_dep, 
   deep: %{
      thing1: "a",
      thing2: System.get_env("THING2")
   }

With vapor, I can get THING2 ENV var from .env but I must be careful with any deep config like this example dependancy. Where to put it once I have it? I think I must merge map and then put inside carefully into Application process dictionary.

Pro: support for .env and .env.{ENV} files for each override. Errors when config is not correct.
Con: difficult for complex config that rely on Application.get_env.

I think there must be other ways. Sorry for my confusion. I admit I thought this was a easy topic but I am understanding more that config has subtle!

RudManusachi

RudManusachi

What about having in config/test.exs

  config :myapp, 
      x: "test-x",
      y: "test-y",
      z: System.get_env("TEST_Z", "test-z"),

and in config/runtime.exs

if config_env() == :prod do
  config :myapp, 
      x: System.get_env("X"),
      y: System.get_env("Y"),
      z: System.get_env("Z"),
     # ... etc...
end

[/quote]

if-statements in configs are always for me a smell.

For me if-statement is smell almost everywhere but configs :grinning_face_with_smiling_eyes:

Take a look at the example how configs are organized in hexpm/config at main · hexpm/hexpm · GitHub

Nicd

Nicd

This suffers from being long and containing ifs, but I’m posting it anyway for reference. I went 100% on runtime.exs in my project and only have a couple of things that need to be set at compile time in the config.exs.

This has worked well for me. Even though the runtime config file is big, it’s not really that big once you get to know it, and I can have unified config for both development and release time (just using .env in development for convenience).

vrod

vrod OP

This is a beautiful little package! Thank you for sharing your approach – it works well!

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews