Mix and project dependencies in Elixir

Like many other languages, there’s a lot of third party packages and shared code for the Elixir language.

If we use mix to create a new project you’ll get a mix.exs script file generated. Let’s take a look at one created for one of my projects

defmodule TestLang.MixProject do
  use Mix.Project

  def project do
    [
      app: :test_lang,
      version: "0.1.0",
      elixir: "~> 1.17",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

The project section is where we set the application name, version etc. The deps section is where we add dependencies that we want to pull into our project.

We can pull in code from git or from https://hex.pm/.

Let’s start by adding a dependency on a package from hex.pm, I’m going to add the UUID package https://hex.pm/packages/uuid, so amend the deps section to look like this

defp deps do
  [
    {:uuid, "~> 1.1"}
  ]
end

The code was copied from the packages hex.pm page where it has code for various configurations.

We can check the state of our dependencies by typing mix deps. In my case it tells me the dependency is not available, I need to run mix deps.get.

We don’t need to compiler the dependency as it’ll automatically compile when we need it, but you can compile it using mix deps.compile if you prefer.

When you bring dependencies into your project you’ll see a deps folder created and similar to node_modules in web/node development, you’ll see the dependency code in this folder.

Let’s try UUID out (as per it’s documentation https://hexdocs.pm/uuid/readme.html), I created a lib/try_uuid.ex file and added the following code

defmodule TryUuid do

  def create() do
    UUID.uuid1()
  end
end

Run up iex using iex -S mix to allow access to the project and it’s dependencies. Finally run the code TryUuid.create() and if all went well you’ll get a UUI created.

Let’s add a dependency from GitHub, back to the mix.exs and change the deps section to like this

defp deps do
  [
    {:uuid, "~> 1.1"},
    {:better_weighted_random, git: "https://github.com/JohnJocoo/weighted_random.git" }
  ]
end

I basically picked the package from GitHub by searching for packages and looking for something fairly simple to try – this one seemed as good as any. There were no tags so I’ve not included a version with the dependency.

Now go through the process of mix get.deps now add a file, mine’s try_random.ex with the following code

defmodule TryRandom do
  def create() do
    WeightedRandom.take_one([{:'1', 0.5}, {:'2', 1.0}, {:'3', 2.0}])
  end
end

Compile using mix compile and then run up iex -S mix and finally execute the command TryRandom.create() and if all went well you’ll get some randomly selected value from the list give within the code.