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()
...
endThe Mix it defines has three functions:
install(deps, opts \\ [])isPrecompiled.mix_install/2: it takes the deps and options that function takes, returns:okor raisesPrecompiled.InstallError;install_project_dir/0always returns a path, where Elixir's returnsniluntilMix.install/2has run, so code that joins onto it works: the cache homePrecompiled.Confignames (config :precompiled, home:,PRECOMPILED_HOME, else the user cache dir), or/data/precompiledwhen there is none. It never raises. Livebook's runtime calls it after an evaluation while aMixmodule is loaded and keeps it for a later runtime, whoseMix.install/2starts from it only when that is Elixir's Mix;installed?/0says whether aPrecompiled.mix_install/2call in this VM returned:ok, asMix.installed?/0says whetherMix.install/2ran.
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:
:okwhen it definedMix, or this function had already defined it;{:error, :mix_present}when aMixis 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 noMix.env()or target check around it;{:error, :mix_defined}when anotherMixmodule 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
@spec define() :: :ok | {:error, :mix_present | :mix_defined}
Defines Mix when this runtime has none. See the module documentation.