How do I load an Elixir library into iex without adding it to a project's mix.exs deps?

前端 未结 3 1539
礼貌的吻别
礼貌的吻别 2021-02-01 17:19

I want to tryout the Poison json module without creating a mix project.

How do I install it and make it available in iex via import?

I have been able to add it

相关标签:
3条回答
  • 2021-02-01 17:29

    I can recommend you this blog post.


    1st Step: What do you want?

    There's more than a couple of libraries I want to use without a Mix project, like

    • Combine
    • CSV
    • Poison

    Get their sources from Github, git checkout to the last release and compile them.

    2nd Step: Where do you want them?

    One compilation was over, create ~/.mix/beam/ and move the .beam files into this directory.

    3rd Step: Now let's customize your Interactive EliXir shell!

    Thankfully, iex is just a shell script. If you happen to have a custom $PATH variable that points to ~/.local/bin, then copy iex to this directory and rename it to something like deviex. Then in your custom deviex, move to the last line and change it to…

    exec elixir --no-halt --erl "-user Elixir.IEx.CLI" -pa "$HOME/.mix/beam" +iex "$@"
    

    And now it will load the .beam files located at ~/.mix/beam at startup.

    The reason why we use a different script for IEx is to avoid name conflicts with installed libs in the projects you'll work on with regular iex.

    0 讨论(0)
  • 2021-02-01 17:47

    Not a direct answer, but another way to maybe achieve what you want:

    You could have a playground project that you generate once (e.g. mix new playground) and that you can then relatively easily add new dependencies to.

    If you do iex -S mix within this project, you'll get all its dependencies.

    If you want to quickly experiment with e.g. Poison at some later point in time, you can just go back into this project.

    0 讨论(0)
  • 2021-02-01 17:47

    I don't know if there is an official way to do this.

    One way would be to clone the library project locally, compile it, and then add it to the library path like this by creating a ~/.iex.exs script:

    IO.puts "Adding poison to path from ~/.iex.exs"
    true = Code.prepend_path("#{path_to_project}"/poison/_build/dev/lib/poison/ebin")
    
    0 讨论(0)
提交回复
热议问题