<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nicola Bonsi on Medium]]></title>
        <description><![CDATA[Stories by Nicola Bonsi on Medium]]></description>
        <link>https://medium.com/@nicogoblin?source=rss-961a9ac97a9f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kVdLcA7ZMl7qFxu1CcUAuw.jpeg</url>
            <title>Stories by Nicola Bonsi on Medium</title>
            <link>https://medium.com/@nicogoblin?source=rss-961a9ac97a9f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:16:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nicogoblin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Passing configuration properties to library in Elixir]]></title>
            <link>https://medium.com/@nicogoblin/passing-configuration-properties-to-library-in-elixir-dca5a68dc08?source=rss-961a9ac97a9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/dca5a68dc08</guid>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Nicola Bonsi]]></dc:creator>
            <pubDate>Sun, 11 Sep 2022 14:46:37 GMT</pubDate>
            <atom:updated>2022-09-11T14:55:52.533Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yB2j4dInzZml3Yv7bfyKXA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@wesleyphotography">Wesley Tingey</a> on <a href="https://unsplash.com/photos/mvLyHPRGLCs">Unsplash</a></figcaption></figure><p>In this article, I examine two configuration mechanisms for libraries to be used by other developers and describe the advantages and disadvantages of each solution.<br>The methods described here are two possible ways to configure a library.<br>These are the two approaches we will examine:</p><ul><li>Configure library via configuration file</li><li>Passing configuration arguments when starting a library process</li></ul><pre><strong>Note:</strong> In this article, the focus is set on the libraries that use a supervisor tree and need an external configuration to work properly.</pre><h4>Configure library via configuration file</h4><p>Let’s start with the first method, which is perhaps the most commonly used library configuration mechanism in the Elixir ecosystem.</p><p>With this method, we write the configuration property that the library needs in the configuration file, usually called config.exs or env.exs (like test.exs, production.exs …).<br>Here’s an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*36AScdkK4dAafOxU-Wgo6w.png" /></figure><p>But what happens under the hood when we write a similar configuration?</p><p>These<a href="https://hexdocs.pm/elixir/main/Config.html#config/2">config/2</a> and <a href="https://hexdocs.pm/elixir/main/Config.html#config/3">config/3</a> functions of <a href="https://hexdocs.pm/elixir/main/Config.html">Confi</a>g used to insert key-value configurations into our <strong>application environment.</strong> This is a <strong>global store </strong>that contains all the configurations we entered in the configuration files or inserted at runtime using the Application.put_env/3 function.</p><p>We have seen so far how to insert configurations into the application environment, but how can a library read these values?<br>One way to read properties is the following snippet:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*U26Dh8RVq3DuHv00dW-25A.png" /></figure><p>We’ve learned about the basis for using configurations through the Config module. Now let’s look at a real-world case using the example of a library that uses this mechanism: <a href="https://github.com/thoughtbot/bamboo">Bamboo</a> is a library for sending emails.<br>(off-topic: I think it’s the best solution to send emails in elixir code at the time of writing)</p><p>This library uses the configuration file to get the adapter, the api-key, and some other properties. The adapter is a set of instructions for communicating with a particular email delivery service, while the api-key is a kind of password that allows you to authenticate with your email service and consequently send email. Here is an example of passing these configurations to the library:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*Ao7mrJs4T7y5GxS9FBfSew.png" /></figure><p>As we can see from this snippet, it’s very easy to load the configuration into the application environment and change the configuration properties based on MIX_ENV value. Let’s look at this snippet:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*qAnjSXgGtRSwqjbN1yEX_A.png" /></figure><p>Simply add these new configuration properties to the text.exs file and you will find that the application uses a module mock to send emails when running in the test environment (as MIX_ENV=test), while using the production email delivery service in all other environments.</p><p>This mechanism seems to be very useful for both the user of the library and the developer writing the library.<br>From the library’s point of view, it is easy to get the configuration; in particular, we can read the configuration value locally in the module where we need it. <br>The last advantage may not seem so important, but as we will explore other methods, we appreciate it!<br>This snippet shows the code for reading properties in the library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*so__7vec1xtbfnB2QHUcRA.png" /></figure><p>But not all that glitters is gold, as mentioned earlier when we add the configuration to the configuration file, we add it to an application environment. Also remember that the application environment is <strong>global</strong>, which means that it is impossible for two dependencies to use the library in two different ways.<br>I’ll try to explain this concept better with a new example: suppose we use a library called FileChecker to check some file properties, and the files are located in a certain directory specified in the configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*WQsOCDq9X_Fq-zQyMX4qGw.png" /></figure><p>We note that the FileChecker library is already used by our client HTTP dependency to check files before uploading them, and its documentation says to configure the :base_path to temporary_path/example/.<br>The problem arises here because we also want to use the FileCheck library for our purposes, but since the application environment is global, when we change the value of the :base_path property, we also change the value for our client HTTP library, resulting in unexpected behavior.</p><p>This problem is also described as an <a href="https://en.wikipedia.org/wiki/Anti-pattern">anti-pattern</a> in the<a href="https://hexdocs.pm/elixir/1.14.0/library-guidelines.html#avoid-application-configuration"> library guidelines</a> of Elixir documentation, which I would strongly recommend reading.</p><h4>Passing configuration arguments when starting a library process</h4><p>This approach is quite different from the previous one since users should be able to pass arguments when they start the process in their supervisor.<br>A good example of a library that uses this mechanism is Redix. For those who do not know this project, it is a Redis client and its main features are Speed, Correctness, and Resilience (it is able to automatically reconnect to a Redis in case of a network failure). So if you want to use Redis with Elixir code, this library is for you!</p><p>Let’s explore how this library manages the configuration given by users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*IgUMp6iBZl_iLDbT65qgMw.png" /></figure><p>With this code, the user starts a process that connects to Redis. The keyword list passed in the start_link function is interesting. This is the argument that the user passes to the Redix library for configuration.</p><p>This decision to start the main library process by the user is very powerful because it gives the user the freedom to decide how many Redix processes to start. Each started process can have a different configuration, and logic can be added based on MIX_ENV or an environment variable.</p><p>Let’s look at an example where we want to use Redix in our application and we want to start it through our supervisor, so when we start the application we are sure that the connection to the Redis client is up.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*kSLWrlNVT668IdOrpd09yg.png" /></figure><p><strong>Note:</strong> If you find the abbreviation for child_spec strange (or do not remember it), I recommend reading this. However, this way of writing child_spec is a shortcut to calling: <br>Redix.start_link(host: &quot;redix1.myapp.com&quot;, name: :primary).</p><p>From this code snippet, it is clear how a user of the library can benefit from this configuration mechanism. In this example, the user decides to start two connections to two different Redis clients (note that the host option is different) and give them two different names (using the name option). And if the user wants to make a third connection to another Redis client? No problem, just add another child_spec to the list of children.</p><p>Another advantage of this method is that it is quite easy to manage configuration properties based on a variable or MIX_ENV variable. Suppose we want to change the host when we run our test, this is a very common scenario. Because we do not want our application to connect to the production Redis client during testing. Let’s see how we can implement this behavior:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*WTfQcUP3sSNADsA96t7Hyw.png" /></figure><p>That’s it. The user can write a custom function to manage the various configuration properties based on MIX_ENV variables. If we also want a different host for the development environment, no problem, we can add another case to our if-else or use a cond or case expression.</p><p>Note that in this example I do not want to focus so much on this particular case of “using Redix correctly”, but rather show you the benefits of passing the configuration to the library process.</p><p>So far everything seems fine, but there is one aspect of this method that can be unpleasant. Let’s put ourselves in the shoes of the developer writing this library. The problem is to pass the configuration properties that the process gets as arguments to the function that needs them because if the function that needs these properties is nested, in the sense that this property needs to be passed for multiple functions, this can increase the complexity and we have many functions that need to get and manage these arguments.<br>Remember that I highlighted the locality of getting a configuration property in the previous method? This is the reason.</p><h4>Conclusion</h4><p>Like many other things, each approach has its advantages and disadvantages, as we’ve seen in this article.</p><p>In general, the first approach of configuring the library by configuration file is widely used, probably for historical reasons, because this pattern was very useful in the early days of Elixir, and also because of its simplicity. (It’s straightforward to write and read configuration properties.)<br>However, this simplicity comes at the price of storing the key-value properties in a global store. This is what the Elixir library guidelines say:</p><blockquote>The application environment should be reserved only for configurations that are truly global, for example, to control your application boot process and its supervision tree. And, generally speaking, it is best to avoid global configuration.</blockquote><p>And the same guidelines state:</p><blockquote>You should avoid using the application environment (see <a href="https://hexdocs.pm/elixir/1.14.0/Application.html#get_env/2">Application.get_env/2</a>) as the configuration mechanism for libraries. The application environment is <strong>global</strong> which means it becomes impossible for two dependencies to use your library in two different ways.</blockquote><p>I think the second approach is the best choice if it is possible and reasonable to use it.<br>Because I think if your library needs to use processes, then users should be able to pass arguments when they start the process in their supervisor, and also users should be able to start as many processes as they want. All of these things are possible with the second approach.<br>So my final advice is to pay attention to the method of passing configuration properties when writing libraries, as a poor choice may limit the use of the same library in some circumstances.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dca5a68dc08" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>