Skip to content
Permalink
Browse files

## Incorporating Mix

This is great, but manually managing our `*.beam` files and bootstrap expressions is a little cumbersome. Not to mention the fact that we haven’t even started working with dependencies yet.

Let’s make our lives easier by incorporating [the Mix build tool](https://hexdocs.pm/mix/Mix.html) into our application development process.

We can do that by creating a `mix.exs` Elixir script file in the root of our project that defines a module that uses `Mix.Project` and describes our application. We write a `project/0` callback in our new `MixProject` module who’s only requirement is to return our application’s name (`:minimal`) and version (`"0.1.0"`).

	def project do
	  [
	    app: :minimal,
	    version: "0.1.0"
	  ]
	end

While Mix only requires that we return the `:app` and `:version` configuration values, it’s worth taking a look at the other configuration options available to us, especially [`:elixir` and `:start_permanent`](https://hexdocs.pm/mix/Mix.html), [`:build_path`](https://hexdocs.pm/mix/Mix.Tasks.Compile.html#module-configuration), [`:elixirc_paths`](https://hexdocs.pm/mix/Mix.Tasks.Compile.Elixir.html#module-configuration), and others.

Next, we need to specify an `application/0` callback in our `MixProject` module that tells Mix which module we want to run when our application fires up.

	def application do
	  [
	    mod: {Minimal.Application, []}
	  ]
	end

Here we’re pointing it to the `Minimal.Application` module we wrote previously.

During the normal application startup process, Elixir will call the `start/2` function of the module we specify with `:normal` as the first argument, and whatever we specify (`[]` in this case) as the second. With that in mind, let’s modify our `Minimal.Application.start/2` function to accept those parameters:

	def start(:normal, []) do
	  IO.puts("Hello.")
	  {:ok, self()}
	end

Notice that we also changed the return value of `start/2` to be an `:ok` tuple who’s second value is a PID. Normally, an application would spin up a supervisor process as its first act of life and return its PID. We’re not doing that yet, so we simply return the current process’ PID.

Once these changes are done, we can run our application with `mix` or `mix run`, or fire up an interactive elixir shell with `iex -S mix`. No bootstrap expression required!
  • Loading branch information...
pcorey committed May 6, 2019
1 parent f367037 commit 7c2fcd8300e13f04dd0c20a9ae2153b9c21ecebe
Showing with 20 additions and 2 deletions.
  1. +2 −1 .gitignore
  2. +2 −1 lib/minimal/application.ex
  3. +16 −0 mix.exs
@@ -1,2 +1,3 @@
*.beam
.DS_Store
.DS_Store
/_build/
@@ -1,5 +1,6 @@
defmodule Minimal.Application do
def start do
def start(:normal, []) do
IO.puts("Hello.")
{:ok, self()}
end
end
16 mix.exs
@@ -0,0 +1,16 @@
defmodule Minimal.MixProject do
use Mix.Project

def project do
[
app: :minimal,
version: "0.1.0"
]
end

def application do
[
mod: {Minimal.Application, []}
]
end
end

0 comments on commit 7c2fcd8

Please sign in to comment.
You can’t perform that action at this time.