darwin67

darwin67

Detecting Mix.env in library

Hi folks,

What’s the current best practice when it comes to detecting env, something like Mix.env() within a library?

I’m currently building a library, and I’m trying to figure a good way for config to return different values based on environment it’s running on.
Based on library guidelines, config.exs is not recommended (and seems to be ignored when lib is used anyways), so I add a module like Library.Config. for returning config related values.

I tried something like this

defmodule Library.Config do
  def url() do
    if Mix.env() == :dev do
      @dev_url
    else
      @event_url
    end
  end
end

which seem to work for local dev, but now I’m getting

Function Mix.env/0 does not exist.

from Dialyzer.

Would appreciate any recommendations or just pointing me to a direction.
Searching on this topic gets different results from the pass and seems like guidelines have been changing.
Also most questions seem to be more on usage within an application like Phoenix, and I don’t seem to be able to find much for library authors, so it’s not clear what’s the best way to proceed.

I’m fine with using something like Application.get_env, but unclear how the config values can be derived over to applications using the lib.
e.g.

# my lib config.exs
config :mylib, base_url: "https://someapp.com"

if Mix.env() == :dev do
  config :mylib, base_url: "http://127.0.0.1:8288"
end

## application using mylib
iex -S mix> Application.get_env(:mylib, :base_url)
# => nil

Thanks

Marked As Solved

dimitarvp

dimitarvp

As a corollary, you can use NimbleOptions to help you with having solid configuration values.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Libraries are always compiled with Mix.env == :prod, so I think toggling based on that isn’t going to work. Libraries should not change themselves implicitly, the user of the library should always pass in configuration. What changes are you trying to do?

trisolaran

trisolaran

If you need base_url to be set when you’re developing your library locally, then you already figured out how to do it:

# my lib config.exs
config :mylib, base_url: "https://someapp.com"

But if you need applications using the library to pass configuration to it, they will have to do so in their own config files. You should also consider whether it’s a good idea for your library to avoid using the application configuration altogether.

trisolaran

trisolaran

I wouldn’t do it for at least 2 reasons:

  • libraries can also run outside of Mix, if your application is running in a release for example then Mix isn’t even available so the notion of an environment at runtime doesn’t even make sense
  • environment names are completely defined by the application using your lib. Most people use standard names like dev, test, prod etc. but they could use whatever. For all you know, they could use development instead of dev

If your library needs to consume a configuration, then have your library’s user pass that in. Only that should matter, not the name of the environment

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The convention in this case would be that your install instructions would tell users to do this in their runtime.exs

config :your_lib, endpoint: "127.0.0.1:8288"

if config_env() == :prod do
  config :your_lib, endpoint: "[127.0.0.1:8288](https://inn.gs)"
end

Using the mix.env as a global variable to change logic in. your library both won’t work for the reasons I mentioned earlier, and is not a best practice.

Where Next?

Popular in Questions Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
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
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement