rebar3-purifier full guide

Detailed setup, dependency management, build commands, and troubleshooting. Start with the overview for the initial commands; use the sections below for the complete setup and behavior. rebar3-purifier is classified at maturity tier 3 (see tier assessment for details).

Contents

Setup and activation

Use the overview’s Quickstart for the initial command sequence. The setup details below apply to that workflow.

Commit the lockfiles that the lock command writes, including the build tool’s own lockfile when it has one. Keep using the project’s usual build, test and editor commands with this setup. Nix provides the locked dependencies; the development commands above run outside the Nix sandbox. No project Nix files are needed for this workflow.

Sandboxed builds are an optional next step.

The shell provides the rebar3 wrapper. Use it for the locked development workflow. With no purifier lockfile, it warns on standard error and runs the ordinary tool with impure resolution.

Install these packages through your usual Nix configuration.

Version compatibility

Tool or language Packaged defaults Tested versions
Rebar3 3.28.0 —
Erlang/OTP 28.5.0.8 —

Operating systems and architectures

OS CPU architecture Packaged toolchain Recorded workflow
macOS ARM64 Yes Not recorded
Linux ARM64 Yes Not recorded
Linux x86-64 Yes Tested

Development loop

nix shell 'github:nix-purifiers/rebar3-purifier' gives a shell with rebar3, the Erlang tools, elp and rebar3-purifier. Run rebar3 as usual: rebar3 compile, rebar3 eunit, rebar3 ct, rebar3 shell, rebar3 release. In a project with a lockfile, rebar3 first builds the cache tree of the lockfile through Nix, and runs with:

A dependency that is not in the lockfile fails to fetch, with the message of rebar3. A project without a lockfile resolves the usual way. REBAR3_PURIFIER=0 turns interactive mode off.

The commands:

rebar3 has a plugin mechanism, and rebar3-purifier is a separate command that runs beside it. lock runs rebar3 itself in a copy of the project with empty caches, and the rebar3 of the toolchain sets the Hex and git environment before rebar3 starts. A plugin runs inside rebar3, after it has read its cache and configuration, so it cannot do either.

Using locked dependencies

It supports x86_64-linux and aarch64-linux.

Working offline

rebar3-purifier prefetch realizes in the Nix store everything the lockfile lists for the current system: the Hex tarballs and the git checkouts of every profile, and the toolchain, which holds Erlang, rebar3 and the Erlang Language Platform (elp). It keeps them alive with garbage-collector roots in .rebar3-purifier-gcroots beside the lockfile, which .gitignore should list. After a prefetch, rebar3 compile, rebar3 eunit, rebar3 release and the editor work with the network cut off.

Language tooling integration

The toolchain has elp, the Erlang Language Platform, which loads the project by running rebar3. Run your editor inside nix shell, or put the toolchain on its PATH, and elp finds the rebar3 of the toolchain, which reads the locked downloads. After a prefetch the editor needs no network.

Nix builds

Once the native development workflow is working, you can also build and test the whole project in the Nix sandbox. For this step, keep a flake or another Nix build definition in the project’s repository, or maintain an external packaging repository that points at its sources and lockfiles. The builder consumes the same lockfiles as the development workflow.

The overlay of the flake adds one attribute to a package set, the scope rebar3-purifier. The scope holds the builder buildRebar3Project beside the command and the toolchain. A project whose authors want a flake can package itself by calling the builder directly. The flake can live in the repository of the project or in another one:

{
  inputs.rebar3-purifier.url =
    "github:nix-purifiers/rebar3-purifier";
  inputs.nixpkgs.follows = "rebar3-purifier/nixpkgs";

  outputs =
    { nixpkgs, rebar3-purifier, ... }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;
        overlays = [ rebar3-purifier.overlays.default ];
      };
      project =
        pkgs.rebar3-purifier.buildRebar3Project {
        src = ./.;
      };
    in
    {
      packages.${system}.default =
        project.packages.release-my_app;
      checks.${system}.tests = project.tests;
    };
}

buildRebar3Project { src, lockFile ? ..., fetchers ? { }, ... } returns:

Every attribute name is known at evaluation time, with no import-from-derivation. fetchers replaces the fetchers of the library, for a company mirror or for credentials: fetchers.fetchurl is a fetchurl with them. buildRebar3Project.override replaces the packages the builder takes, and rebar3-purifier.overrideScope replaces what the packages of the scope share. lib.rebar3-purifier.readLock { src, lockFile ? ..., rebarLock ? ... } reads the lockfiles into the lock in memory that the nix-purifiers share.

The other arguments are beamPackages (the default is the one of the OTP release the lock was made with, when nixpkgs has it), cli, releaseProfile, buildInputs, nativeBuildInputs and srcFilter. nix/lib/build-rebar3-project.nix documents them.

We recommend committing both rebar3-purifier.lock.json and rebar.lock to the repository. The builder and the commands look for them there.

A build can instead keep the lockfiles outside the repository, for a project whose committed lockfiles are missing or stale. The flake that calls the builder passes lockFile, and rebarLock when it also keeps a copy of rebar.lock, and the build uses them in place of the copies in src. The commands take --lockfile <path> and --tool-lockfile <path>, or the variables REBAR3_PURIFIER_LOCKFILE and REBAR3_PURIFIER_TOOL_LOCKFILE. With either, lock and update write the lockfile at that path and leave the copy in the tree as it is, and lock starts from the copy of rebar.lock in the tree while the one outside does not exist.

One-off builds without a project Nix file

For an ad hoc build, the builder can also be evaluated directly from the project root. Replace the example package name with one from your project:

nix build --impure --expr 'let
  purifier = builtins.getFlake "github:nix-purifiers/rebar3-purifier";
  pkgs = import purifier.inputs.nixpkgs {
    system = builtins.currentSystem;
    overlays = [ purifier.overlays.default ];
  };
in
(pkgs.rebar3-purifier.buildRebar3Project { src = ./.; })
.packages.default'

This uses the purifier’s pinned nixpkgs, matching the lock command above. To build with ambient <nixpkgs> from your Nix search path instead, replace import purifier.inputs.nixpkgs with import <nixpkgs>. Both forms need no Nix files in your project. The build tool and language versions must match the lockfile; with ambient nixpkgs, lock with the purifier from that package set or pass a compatible toolchain to the builder.

Supported scope

Supported language repositories

The purifier fetches Hex packages from https://repo.hex.pm, from HEX_CDN or HEX_MIRROR, from hexpm:<organization>, and from a repository that rebar.config or your global rebar3 configuration names under hex. The checksum of a Hex package of rebar.lock is its outer checksum, the SHA-256 of the tarball, and the build checks the download against it.

It fetches git dependencies and git_subdir dependencies by commit, tag or branch, and git plugins. The lockfile records the hash of the checkout and the raw commit object, and the build rebuilds the repository from them. An SSH URL is recorded as the HTTPS URL of the same host, since the Nix sandbox has no SSH keys. Git submodules are not checked out, as rebar3 does not check them out.

Plugins, the dependencies of the test and prod profiles, and the dependencies of extra profiles added with --profile are locked in the same way.

A few limits apply. A plugin or a build hook that downloads files itself fails in the sandbox, and C libraries for NIFs and port drivers come from buildInputs. The lock run downloads tarballs from a Hex repository that needs credentials with your credentials and adds them to the Nix store, where the build finds them, so another machine needs a fetchurl with credentials in fetchers. Your global rebar3 configuration is in effect during the lock run but not in the sandbox, so the lock run downloads a plugin that only the global configuration names and the build does not load it.

rebar.lock cannot record everything the build downloads, so the purifier writes rebar3-purifier.lock.json for the rest. rebar.lock is the resolution of the default profile, and it holds the outer checksum of every Hex package in it, so the lockfile does not repeat them. The lockfile holds the Hex tarballs that rebar.lock does not list (plugins and the other profiles), the hash and commit object of each git checkout, the outputs of the build, and the hash of the files that define the dependencies. rebar.lock is an Erlang term, which Nix cannot parse, so Nix reads the shapes rebar3 writes with regular expressions, and the command reads it with file:consult.

Three more decisions:

  • The package index is generated. rebar3 reads the Hex registry for every Hex dependency before it takes the cached tarball. Registry records change with every release, so they cannot be locked by hash. Each Hex tarball carries the checksums and requirements the registry holds for its version, so the build writes the index rebar3 reads (hex/packages.idx, format 7) from the locked tarballs, and the etag file beside each.
  • Versions from git are pinned. An application whose .app.src takes its version from git ({vsn, git}) cannot compute it in the sandbox. The lock run records the version, and the build writes it into its copy of the .app.src.
  • Erlang and rebar3 come from nixpkgs. The lockfile records the OTP release and the rebar3 version of the lock for reference.

Target systems

  • x86_64-linux
  • aarch64-linux
  • aarch64-darwin

Platform limitations and validation

  • Apple Silicon macOS support is contributor-reported from unsandboxed builds; no macOS CI is configured
  • Sandboxed Darwin –check rebuilds have not been verified
  • Deterministic compilation also changes Linux stack traces: source filenames omit the build-directory path

Troubleshooting

Every lockfile problem stops the build and the commands with a message that names the command that fixes it, and the same messages appear at Nix evaluation.

Problem Message names
The lockfile does not exist rebar3-purifier lock
The lockfile is not valid JSON, or not a rebar3-purifier lockfile rebar3-purifier lock
The lockfile has another format version rebar3-purifier lock
The lockfile was changed after lock wrote it (its contentHash differs) rebar3-purifier lock
rebar.config, rebar.lock or the rebar.config of an application changed since the lock rebar3-purifier lock
rebar.lock names a dependency that has no download rebar3-purifier lock
A download has another hash than the lockfile records the build fails in the fetcher, which names the hash

To move dependencies to newer releases, run rebar3-purifier update.

Examples

For day-to-day work on these projects, use the native-tool setup and commands in Quickstart and Development loop. The Nix expressions below show optional sandboxed packaging of those projects.

erlang_ls

erlang_ls is a language server for Erlang, built as an escript. To build it with Nix, clone it, lock it, and build it from the checkout:

nix shell 'github:nix-purifiers/rebar3-purifier'
git clone https://github.com/erlang-ls/erlang_ls
cd erlang_ls
rebar3-purifier lock
nix build --impure --expr 'let
  purifier = builtins.getFlake "github:nix-purifiers/rebar3-purifier";
  pkgs = import purifier.inputs.nixpkgs {
    system = builtins.currentSystem;
    overlays = [ purifier.overlays.default ];
  };
in
(pkgs.rebar3-purifier.buildRebar3Project { src = ./.; })
.packages.escript-erlang_ls'
./result/bin/erlang_ls --version

elvis

elvis is a style checker for Erlang, also an escript, with Hex plugins. The steps are the same. Clone it, run the lock command in the checkout, and build it with packages.escript-elvis in place of packages.escript-erlang_ls. The result is result/bin/elvis.

Your own project

The same steps apply to a project of your own. The package to build is packages.release-<name> for a relx release, packages.escript-<name> for an escript, or packages.app-<name> for an application, and the tests attribute of the builder is the test run that goes into the checks of a flake.