Writing and Publishing Elixir Libraries
Thursday, 28 April 2016 · 13 min read · elixirLet’s walk through the process of writing and publishing an Elixir
library from start to finish. For illustrative purposes, we’ll create a pretend simple_statistics
statistics toolkit for Elixir.
The full source code is available on Github.
📬 Get updates straight to your inbox.
Subscribe to my newsletter so you don't miss new content.
Create an Elixir Project
Mix will generate the following directory structure:
Write Code
You can create a new folder in lib/
with the name of your package (simple_statistics
) to place other modules. It’s a good idea to split code to different modules for modularity. We’ll create a new lib/simple_statistics/mean.ex
module.
Write Documentation
Use @moduledoc
and @doc
to specify module and function docstrings. You should ideally document every major public function in your modules.
Add Doctests
You can add doctests
in your docstrings like so:
Let’s add these doctests to our main test suite.
We can now run our tests with mix test
.
Add Type Annotations
We can use Typespecs to utilize static type checking for our functions.
Typespecs helps ensure that there’s no discrepancy between differing types of our program’s constants, variables, and functions.
You can then perform static analysis by using dialyzer
. First, update your mix.exs
to add dialyxir as a dependency:
Then, run dialyzer
- Erlang’s static analysis tool:
Generate Documentation
Update your mix.exs
to add the following dependencies:
Run mix deps.get
and mix deps.compile
to retrieve and install those dependencies. Finally, we can generate our documentation using mix docs
:
Here is the live documentation, hosted on Github Pages. Our documentation also displays docstring examples as well as any type annotations. Cool!
Publishing your Library
We’re now ready to start publishing our library.
Register with Hex
First, set up an account on Hex, Erlang and Elixir’s package manager if you haven’t already.
Click on the email confirmation link to activate your Hex.pm acccount.
Set your project’s metadata
Update your mix.exs
with description
and package
:
After the package metadata and dependencies have been added to mix.exs
, we are ready to publish the package with the mix hex.publish
command:
Note that our library will be published as the version specified in mix.exs
.
A published version can be amended or reverted with
--revert
up to one hour after its publication. If you want to revert a publication that is more than one hour old you need to contact an administrator.
Publishing Documentation
You can publish your documentation to Hex Docs. The documentation will be generated by running the mix docs
task.
See the live documentation.
This documentation will be accessible at https://hexdocs.pm/my_package/1.0.0
. In addition, https://hexdocs.pm/my_package
will always redirect to the latest published version. Instead of Hex.pm, you can alternatively host the documentation at docs/
yourself or on Github Pages.
Note that
mix hex.publish
will by default build and publish your library’s documentation as well.
Appendix: Versioning
You can publish a new version by updating the version
value in mix.exs
and running mix hex.publish
.
Also, remember to use Git tags to annotate version changes!
In Closing
We’ve succesfully written and published an Elixir library! Writing and publishing libraries in Elixir is easy and straightforward. Other developers can now add your library into their deps
and start using it in their projects.
Additional Reading
📬 Get updates straight to your inbox.
Subscribe to my newsletter so you don't miss new content.