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!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
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
How to know what modules have macros?
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 calluse SomeModuleorrequire SomeModule.I am not sure if
import SomeModulealso “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
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.exslooks something likeBut how to override this for testing?
RudManusachi
You can use
Config.config_env/0to differentiate between envslud
You can use Application.put_env in your tests directly.
vrod
Forgive me for my struggle to understand a good solution. I think my options are to do maybe these:
Option 1: fill
runtime.exs: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.exsand add test values totest_helper.exslike maybe this:and then in
test_helper.exsI can doneApplication.put_envlikePro: 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 beforetest_helper.exsruns and a value is supplied.Option 3: Vapor
I looked also at Vapor. I like this: it could replace
runtime.exs100% I think. But it is still a problem when you must manually take a value and useApplication.put_envto put it into the correct place so your app and deps can find it. For example, imagine a dependency with config like this:With vapor, I can get
THING2ENV var from.envbut 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
.envand.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
What about having in
config/test.exsand in
config/runtime.exs[/quote]
For me if-statement is smell almost everywhere but configs
Take a look at the example how configs are organized in hexpm/config at main · hexpm/hexpm · GitHub
Nicd
This suffers from being long and containing
ifs, but I’m posting it anyway for reference. I went 100% onruntime.exsin my project and only have a couple of things that need to be set at compile time in theconfig.exs..envwhen developing locallyThis 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
.envin development for convenience).vrod
This is a beautiful little package! Thank you for sharing your approach – it works well!