Precompiled.MixShim (Precompiled v0.10.6)

Copy Markdown View Source

Defines a Mix module on a runtime that has none, such as Nerves Livebook's, so a notebook's Mix.install/2 installs with Precompiled.mix_install/2.

Call define/0 once at boot, for example in the firmware's Application.start/2:

def start(_type, _args) do
  _ = Precompiled.MixShim.define()
  ...
end

The Mix it defines has three functions:

  • install(deps, opts \\ []) is Precompiled.mix_install/2: it takes the deps and options that function takes, returns :ok or raises Precompiled.InstallError;
  • install_project_dir/0 always returns a path, where Elixir's returns nil until Mix.install/2 has run, so code that joins onto it works: the cache home Precompiled.Config names (config :precompiled, home:, PRECOMPILED_HOME, else the user cache dir), or /data/precompiled when there is none. It never raises. Livebook's runtime calls it after an evaluation while a Mix module is loaded and keeps it for a later runtime, whose Mix.install/2 starts from it only when that is Elixir's Mix;
  • installed?/0 says whether a Precompiled.mix_install/2 call in this VM returned :ok, as Mix.installed?/0 says whether Mix.install/2 ran.

Nothing else of Mix is there: a call to another Mix function raises UndefinedFunctionError. Code.ensure_loaded?(Mix) is then true in the whole VM, so another library that checks for Mix may change what it does, or call a Mix function the shim does not have. This library's own host functions tell the shim, and any other Mix, from Elixir's.

Nothing calls define/0 on its own, and it never replaces a Mix:

  • :ok when it defined Mix, or this function had already defined it;
  • {:error, :mix_present} when a Mix is loaded from, or found on the code path as, a beam file: Elixir's Mix on a host and in tests, or one compiled into the firmware. Nothing is defined, so the call needs no Mix.env() or target check around it;
  • {:error, :mix_defined} when another Mix module defined at run time, with no beam file, is loaded: it is left as it is.

It raises when Mix does not compile, as when another process defines a Mix at the same moment.

Summary

Functions

Defines Mix when this runtime has none. See the module documentation.

Functions

define()

@spec define() :: :ok | {:error, :mix_present | :mix_defined}

Defines Mix when this runtime has none. See the module documentation.