mbklein

mbklein

Importing custom functions for use in config.exs/releases.exs/runtime.exs

I have a complex app configuration that I’ve made much simpler via a custom module. I don’t want to implement it as a config provider because I have to use it during development as well. But trying to access it from config.exs leads to a bit of a chicken-and-egg situation because config.exs is loaded before the app is compiled.

I’ve tricked Elixir into making this work in both mix mode and release mode, but it’s really hacky and I kind of hate it. I’d love to know if there’s a better way to accomplish what I’m doing. Here’s how it goes:

lib/my_configurator.ex

Define the module if it’s not already defined.

if !function_exported?(:MyConfigurator, :__info__, 1) do
  defmodule MyConfigurator do
    def prefix, do: System.get_env("CONFIG_PREFIX", "")
    def prefix(str), do: prefix() <> "-" <> str
    ...
  end
end

mix.exs

At the top of mix.exs I added

Code.require_file("lib/my_configurator.ex")

Yeah. Like I said. Ugly. Anyway, All of that makes MyConfigurator available from within config.exs and all of the files it loads via import_config when run via mix. And it’s available from releases.exs in the release by virtue of having been compiled from lib. But you can see why I’d appreciate a more elegant solution.

Is there one?

First Post!

josevalim

josevalim

Creator of Elixir

You shouldn’t really read env vars from config.exs, because the var will be read when the code is compiled. Then if you change the var, the code won’t be recompiled, and it will behave as before.

My suggestion is then to move all runtime configuration (such as env vars) to runtime.exs, where your code already works as expected!

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement