Quick note highlighting a currently under-documented flag supported when creating a new OTP Application.

When you are building a new “mix” and you are already plan on it being an OTP Application and you expect to build Supervision trees, you can use the --sup flag to start it off.

What is an “OTP Application”? That comes later. But my short explanation is:

It is an Elixir project that will build on top of the incredible features of the Erlang OTP Framework for building highly fault-tolerant and distributed applications.

Refer to the following for more in-depth information.

I wanted to document this for myself and others because I can’t find it officially documented anywhere. It isn’t in the Mix documentation online, and it isn’t in the mix help documentation. I’ve seen it in a few places online and in some presentations. So it is worth mentioning here too.

Using the --sup flag.

$ mix new sup_test --sup
* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/sup_test.ex
* creating test
* creating test/test_helper.exs
* creating test/sup_test_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd sup_test
    mix test

Run "mix help" for more commands.

The project looks the same on the surface. It doesn’t do anything that you can’t easily add to an existing Mix created without the --sup flag. However, there are two significant differences.

The mix.exs file has the OTP application configured with the module SupTest exported.

# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
  [applications: [:logger],
   mod: {SupTest, []}]
end

The lib/sup_test.ex file has the following contents. Without the --sup flag, it only has the module definition.

defmodule SupTest do
  use Application

  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      # Define workers and child supervisors to be supervised
      # worker(SupTest.Worker, [arg1, arg2, arg3]),
    ]

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: SupTest.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

It is a handy shortcut for creating new OTP Application pieces if you planned to do that anyway.