xgeek116

xgeek116

I created a test elixir lib that uses a file config/config.exs to store env variables for example :

config.exs :

import Config

url_dev = "DEV"
url_prod = "PROD"

config :test_lib,
  dev: [
    url: url_dev,
  ],
  production: [
    url: url_prod,
  ]

and in the lib/test_lib_elixir_package.exs, I called tha variable :

defmodule TestLibElixirPackage do
  def testFunction do
    dev_url = Application.fetch_env!(:test_lib, :dev)
    dev_url[:url]
  end
end

When I test the lib with IEX i can obtain the result of the function testFunction

But When I installed the LIB in an elixir app and tried to call the function I got this error :

“** (ArgumentError) could not fetch application environment :dev for application :test_lib because the application was not loaded nor configured
(elixir 1.13.1) lib/application.ex:683: Application.fetch_env!/2
(test_lib_elixir_package 0.1.2) lib/test_lib_elixir_package.ex:8: TestLibElixirPackage.testFunction/0”

Here is my mix.exs from the library (I included the config directory) :

defp package do
    [
      files: ["lib", "config", "mix.exs", "README*"],
      description: "Testing build and publish elixir lib",
      maintainers: ["test"],
      licenses: ["MIT"],
      links: %{}
    ]
  end

Showing Posts 1 to 10

hauleth

hauleth

config/config.exs in library codebase is not loaded when used as a dependency.

xgeek116

xgeek116 OP

Hi, But I can find it in the installed lib files :

I am missing something?

LostKobrakai

LostKobrakai

It doesn’t matter if it’s there – config files of your dependencies are not used.

xgeek116

xgeek116 OP

Hi LostKobrakai, So how can I make it work ?

LostKobrakai

LostKobrakai

You can either move the values into your code or provide app env values (for your app only) via the mix.exs (see vintage_net/mix.exs at main · nerves-networking/vintage_net · GitHub)

xgeek116

xgeek116 OP

The idea is to provide one LIB, but the LIB provide 3 envs (dev, staging and prod) depending on the ENV of the app. How do you think it should be implemented on the LIB side ?

xgeek116

xgeek116 OP

@LostKobrakai how this approche " or provide app env values (for your app only) via the mix.exs "

Can help me get the LIB vars ? you mean I should define the enviromnent in each app that installes the LIB ?

hauleth

hauleth

Ideally you should not use the application environment in libraries at all. But answering your question - yes, you should define configuration each time you use dependency.

wolf4earth

wolf4earth

To add to that, if you decide to rely on the application environment and would still like to rely on some defaults you can do something like the following:

Application.get_env(:test_lib, :dev, your: "default config")

Or a tad more readable:

@default [your: "default config"]

def some_function do
  config = Application.get_env(:test_lib, :dev, @default_config)
  # ...
end

I do something similar in my etag_plug package.

xgeek116

xgeek116 OP

@wolf4earth : Sorry to ask, I’m still beginner but this line will be on the LIB side ?

Application.get_env(:test_lib, :dev, your: "default config")

And this code also ?

@default [your: "default config"]

def some_function do
etc..

When we send the config from the APP to the LIB ?

Where Next? Top

Trending in Questions Top

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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews