How to import config in Elixir

Elixir is a dynamically typed, functional language used for building scalable and maintainable applications. It runs on the Erlang virtual machine, which is known for its scalable, low-latency, and fault-tolerant systems.

Elixir applications typically have configuration (config) files that are used to configure the application environment. The files are stored in a config/ directory. Elixir provides two forms of creating a config file depending on how they are read, which is itemized below:

  • Read at build time: This type of configuration is read before the application codes are compiled. The configs are stored in config/config.exs.

  • Read at runtime: This type of configuration is read after the application and dependencies have been compiled. It allows users to use external configs and system environment variables. The configs are stored in config/runtime.exs.

Configuration files are an important part of any software application. They allow us to customize, manage and set the preferred parameters and initial settings for the application. In this answer, we’ll see how to import configurations into an Elixir application.

Prerequisites

Before learning how to import config in Elixir, it is essential to know the following:

  • Elixir fundamentals (comfortable in writing and executing Elixir code).
  • Mix build tool.

Creating a configuration file

When building applications in Elixir, we need to write configs for them to store environmental variables such as usernames, passwords and other information for the database, keys for APIs, etc. As such, it is important to learn how to write the configurations.

In Elixir, configuration files are typically written using the Elixir syntax. Here’s a step-by-step guide on how to write configs in Elixir:

  • Create a config.exs or runtime.exs file inside a config directory in the root of the project.
  • Import the Config module by using the keyword import Config.
  • Define the configuration values by using the syntax config :application_name, key: value, where config is a constant and application_name is what we choose to name our application. This is followed by the comma-separated key-value pairs we intend to declare.
# config/runtime.exs
import Config
config :my_app,
username: "my_username",
password: "my_passwd",
database_url: "mongo://localhost:27017",
key4: "value4"

  • We can choose to separate the configs into namespaces. This is done by adding the namespace after the application_name.
import Config
config :my_app, myApp.Database
username: "admin",
database_url: "postgres://localhost/my_app_dev"

We can also import other configurations into the main configuration file. To import other config files based on the current configuration environment, add the line import_config "#{config_env()}.exs" to the config. Alternatively, we can import using the file name like such:

import_config "other_config.exs"

How to import configs into the application

To use the configuration values in the code, make use of the Application.fetch_env!() function. It allows us to specify the application_name, a key, and an optional third parameter that serves as the default value if the value of the key has not been defined. Here is an example of how to access the username key from the config:

username = Application.fetch_env!(:my_app, :username, manager)

This will return the value of username associated with my_app. If the key has not been assigned a value or it returns nil, the username is set to manager by default.

Conclusion

In conclusion, importing a config into an Elixir application involves creating a configuration file, defining the configuration value using the Config module, and organizing them within namespaces to aid retrieval. By following these steps, we can effectively import and utilize configuration values in our Elixir applications, enabling flexibility and customization.

Free Resources