Where can I learn how to release my own library as a hex package?

I’ve been learning about Elixir Nif’s for a while, and it looks like I will have my first library to release shortly and could use some help with learning how that process works. It is a fixed-row caching library, and IMHO it is a gamechanger for elixir apps that are planning to use or that are already using embedded node-local storage (i.e. sqlite, dets, cubdb, leveldb, etc). Details to follow.

I could use some help with understanding the process associated with releasing a library as a hex package and on github or a similar site (I don’t know what the best alternative is nowadays). Any pointers, info, insights would be greatly appreciated, especially if they are Nif related.

FWIW, I am currently doing my compilation like this, which is fine for testing but won’t fly for a release I know:

  def project do
    [
      app: :daphne,
      version: "0.1.0",
      elixir: "~> 1.14",
      start_permanent: Mix.env() == :prod,
      compilers: Mix.compilers ++ [:NifBuilder],     <<====  set custom compiler in mix.exs
      deps: deps()
    ]
  end
defmodule Mix.Tasks.Compile.NifBuilder do
  def run(_args) do
    {result, _errcode} = System.cmd("gmake", [], stderr_to_stdout: true)
    IO.binwrite(result)
  end
end
1 Like

I’d suggest starting with the official publishing guide. It’s pretty straight forward. You can test your build process with mix hex.build.

3 Likes

I guess I am unclear on how to turn my app into a dependency.

1 Like

In general, any mix-based app can be configured as a dependency. There are particular rules if you also want to publish to hex.pm

What have you tried and what issues have you found?

3 Likes

Since your library contains a nif you’ll probably also want to look at GitHub - elixir-lang/elixir_make: A Make compiler for Mix which helps compiling code without calling make/gmake directly (since which one you use might be platform dependent).

4 Likes

Unfortunately that didn’t work on my system (FreeBSD / C++ nif) - that’s why I had to write my own mix task for compilation.

Is this related to umbrella apps? I’m familiar with the concept but have never used them. TBH I don’t even know how to get started when it comes to code organization - the hex publishing docs tell me how to publish not what to publish. But maybe I can find a similar small project and the understanding will come.

I’d suggest take a look at:

  • Decimal for a simple package example
  • ex_cldr for a more complex packaging example but no NIF
  • vix for an example of a NIF-based package

When I’m testing a new application that is intended to be published, I find the following useful:

  • In the app (lets call it my_package) that will become a package: mix hex.build -o ../test_my_package --unpack
  • Create a dummy app, my_app
  • In my_app add to deps: {:my_package, path: "../test_my_package"}
  • mix deps.get && deps.compile && compile

And the deal with any errors along the way,

10 Likes

Thanks! That is very helpful.