···
-
By reading this article I assume that you already have written some Elixir code.
-
Even if you haven't (yet) then I think that you can find it interesting as
-
a story how BEAM handles it's applications. But back on topic, when you start
-
your new Elixir project via `mix new my_awesome_project` you will end with
-
something like this in `mix.exs`:
defmodule MyAwesomeProject.Mixfile do
···
And in most cases you will focus on `deps/0`, sometimes on `config/0`, but the
-
`application/0` you will almost never touch, and if you do you probably only
need it to add new entry in `:extra_applications` or sometimes
-
`:included_applications`. Except that this function interns are almost terra
-
incognita, place where you never look unless you are forced to, just like "Read
-
it later" list in Safari. And that is sad, as this is quite powerful and useful
-
piece of code, that hides really useful stuff.
## What `application/0` is for?
-
In Erlang the running system is built from applications. These applications are
-
like processes in your system that are managed by your system supervisor (SysV,
-
systemd, OpenRC, launchd, etc.) and are launched either during VM startup or on
-
direct user request via `Application.start/1-2` and it's family. How to start
-
such application is described in `my_awesome_app.app` file which is commonly
generated by the build system from the template. In Rebar3 projects this
template is `src/appname.app.src` and in Elixir it's the return value of the
named `application/0` function. This generated file is known as [Application
-
Resource File][app file] and is just tuple in form of `{:application,
:my_awesome_app, opts}` where `opts` is a keyword list of additional properties
(to be exact it's output of the `application/0` function).
-
Two of these optional fields are quite known in the Elixir community:
-
- `:mod` which is a tuple `{module(), term()}` which contain the starting point
-
of our application, in most cases it's module that will return main
supervisor of the application.
-
- `:applications` which contain all applications that are required by our
-
application. Younger Elixir developers possibly never seen that as since 1.5
-
this field is automatically filled by parsing `:deps`, we still can add
-
entries there though via `:extra_applications`.
-
There are also few Elixir specific fields, that are sometimes used in bigger
-
- `:extra_applications` - applications that should be included in the release
-
and automatically started before running this application but aren't in the
dependencies list because, for example, are in default distribution, for
example `logger` or `inets`.
-
- `:included_applications` - applications that should be included in the
release, but not automatically started on boot.