6
votes

I have myerlib/src/myerlib.erl erlang library module and I need call its functions from Elixir modules. Too call myerlib module functions from elixir code I could write :myerlib.function(.....) but

If I put myerlib subrirectory under deps/ elixir directory and use mix.exs:

def deps do
  [
    {:myerlib, path: "deps/myerlib"}
    # ...
  ]
end

then when I do iex -S mix I get this error:

*** (Mix) :path option can only be used with mix projects, invalid path dependency for :myerlib

2

2 Answers

10
votes

If you have a src directory with .erl files in it then they will be compiled when you run mix.compile (either with mix compile or implicitly with something like iex -S mix).

You can see this in the mix compile.erlang task. This can be the default path src, but this can be changed by modifying the erlc_paths option in your mix.exs file.

def project do
  [app: :my_app,
   version: "0.0.1",
   elixir: "~> 1.0",
   erlc_paths: ["foo"], # ADD THIS OPTION
   build_embedded: Mix.env == :prod,
   start_permanent: Mix.env == :prod,
   deps: deps]
end
2
votes

I found early this morning a solution here:

https://github.com/alco/erlang-mix-project

Why this link answer the question is essentially:

1.- we have an elixir main project under rssutil/

2.- we have a myerlib.erl erlang library that we need to use from the elixir code we have under rssutil/lib/

3.- one solution is to create rssutil/src/ and copy myerlib.erl and compile, like first answer tell us before.

4.- But we want to manage our erlang libraries like deps of elixir proyects. For this we need that elixir see the myerlib erlang library like a elixir proyect.

5.- then add myerlib like a dep in rssutil/mix.exs

defp deps do
 [..........
       {:myerlib, path:deps/myerlib"}
 ]
end

6.- We need to create rssutil/deps/myliberl/ with the next mix.exs file:

defmodule Myerlib.Mixfile do
  use Mix.Project

  def project do
    [app: :myerlib,
     version: "0.0.1",
     language: :erlang,
     deps: deps]
  end

  def application do
    [applications: [], mod: {:myerlib, []}]
  end

  defp deps do
    [
      {:mix_erlang_tasks, "0.1.0"},
    ]
  end
end

Observe that the language is now erlang, and that we need like myerlib 's dep/

mix_erlang_tasks

7.- also create rssutil/deps/myerlib/src/myerlib.erl with your "old" erlang code

8.- In rssutil/deps/myerlib/ directory where you have the last mix.exs file, write

$ mix deps.get
$ mix compile

9.- Go up to rssutil/ directory and also

$ mix deps.get
$ iex -S mix

10.- And at end, you can call erlang s functions in myerlib.erl with:

iex> :myerlib.any_function_you_know_to_have_here(...)

that's all.

Anyway thank you very much.