Installs signed precompiled libraries into a running Nerves device, with no Mix, no Hex and no compiler.
Precompiled.install(:recon)
Precompiled.install({:circuits_gpio, "2.3.1"})
Precompiled.install([:recon, {:circuits_gpio, "~> 2.3"}])
Precompiled.install(:recon, dry_run: true)
Precompiled.install(:recon, source: "file:///media/usb/precompiled")
Precompiled.mix_install([:recon, {:circuits_gpio, "~> 2.3"}])
Precompiled.list()
Precompiled.uninstall(:recon)
Precompiled.report()A call selects the build that matches the device (its Nerves system and version, OTP, Elixir, CPU and the apps it already has, by name and exact version), verifies every byte through the signed root, batch, manifest and file hashes, stages the files under the store directory, and loads and starts them. Absent runtime dependencies are installed with it, as one unit for the running VM: anything that fails leaves the code path and the loaded apps as they were. The store is a cache and may change before a later step fails: least recently used entries can be evicted for room, and builds that verified and were committed stay in it, unloaded, for a later call. Nothing is compiled, nothing unsigned is loaded, and nothing already present is replaced.
It is available by default. Nothing runs on its own: only someone with a shell on the device calls it. The files stay in the store across reboots as a cache, but nothing is loaded at boot.
Cache
Installed builds stay in dir across reboots, but nothing is loaded at
boot. Installing the same build again loads it from there without
downloading, after checking every cached file against the signed hashes
of the current batch. The cache is bounded by max_bytes and evicts the
least recently used builds that are not loaded. Precompiled.clean/0
empties it.
A network install also keeps the documents it fetches by sha256 (each
manifest and the batch's systems table) in dir/.documents, and the
next call reads them from there: a copy is used only when its bytes
hash to the sha256 the verified batch names, so it is byte for byte
what the source would send, and any other copy is removed and the
document downloaded. The batch and the root are never kept there.
max_document_bytes bounds it, apart from max_bytes, least recently
used first, so the store can take up to both (96 MB by default). When
staging finds the disk full, the call empties the document cache and
stages once more. A dry run only reads it; a file:// bundle install
never reads or writes it. Each network install that asked the source
for the batch prints one line with what it downloaded and what the
store gave it instead, documents and files apart.
Options
| Option | Default | Meaning |
|---|---|---|
:source | config :precompiled, :device, source:, else the official API | Per call: file://DIR for a bundle only. A network source (https://..., or http:// on loopback only) is set in the firmware's config, never per call: a call naming one is refused with {:source_not_allowed, source} (see the Trust guide). The default is https://compatibility.nerves-project.org/api/precompiled, checked against the root this library ships. |
:deadline_seconds | as on the host | Same meaning as the host config key. |
:cacerts_path | none | A PEM CA bundle to verify an https source with (a path). |
:dir | /data/precompiled | The store. Its trust state lives in DIR/.trust: a call with another :dir gets its own trust state (root chain and sequence floors), starting again from the shipped root. |
:start | true | Start the installed apps (:temporary) after loading them. |
:closure | true | Install absent runtime dependencies. An optional one (in the .app's optional_applications or marked optional in the build's declared) is installed only when it is in the request, as in install([:axon, :kino]); otherwise the build installs without it and a line names it, and a build locked without it is preferred. false refuses with {:needs, [...]} instead. |
:max_closure, :max_bytes | 25, 64 MB | Bounds on the apps of one call and on the store's size. |
:max_document_bytes | 32 MB | Bound on the store's document cache (see Cache), counted apart from :max_bytes, so the store can take both; 0 keeps no documents. |
:dry_run | false | Select and plan, verify the trust chain, fetch the manifests and .app files only. Returns the plan, whose transfer says what it fetched and reused. It writes no entry, staging dir, document or eviction: only the trust state in DIR/.trust, and the store dir and its marker when they do not exist yet. |
:report | none | With dry_run: true, write the device facts and the plan as JSON to this path (for mix precompiled.bundle --device-report). |
:quiet | false | Print nothing. |
:lock_retries | 60 | For tests: how often to retry a store another call holds. |
:fetch_retry_waits_ms | [500, 2000] | For tests: the waits, in ms, before each retry of a download that failed transiently (a 500, 502, 503 or 504, a refused, closed or timed-out connection); [] never retries. |
Another trust anchor than the shipped priv/precompiled/root.json is
set in the firmware's config only (config :precompiled, :device, root: path), never per call. So are content origins
(content: [url], mirrors or CDNs): a call passing :content gets
{:error, {:content_not_allowed, origins}}.
The device facts, the clock check, the trust anchor and the network
source have test seams (:env, :clock_synchronized?, :root, an
https or loopback :source), and so do the directories the device's
libraries are looked up in (:lib_roots, /lib and /usr/lib
otherwise). A call may pass them only when this library is built in its
own test env and its application env has test_seams: true, as its own
test suite sets; otherwise, as in every build a firmware or project
makes, they are refused as unknown options, and the firmware's
:device config never sets them.
config :precompiled, :device, enabled: false in the firmware
disables install/2 ({:error, :disabled}); list/1, uninstall/2
and clean/1 keep working, so what is installed can still be removed.
allow: [app] limits installs to those apps, closure members included.
Both are read from the firmware's config only: a call can neither pass,
widen nor clear them. The same config may set the defaults of source,
content, root, cacerts_path, deadline_seconds, dir,
max_bytes, max_document_bytes, max_closure, start, closure and
quiet; any other key there is ignored.
An online call does not wait for NTP: it goes ahead with the clock as it
is, and the batch's own time checks (expiry, a future issue time, a
clock before this library's build) decide. When one of them refuses the
batch and the clock is not synchronized yet (NervesTime.synchronized?/0,
when nerves_time is in the firmware), the call says to wait for NTP
and returns {:error, {:clock_unsynchronized, now}}; with a
synchronized clock it is the batch refusal. A file:// bundle with a signed
offline.json needs no clock and no network at all; one without it is
bounded by its batch's own expiry, judged against the device clock (no
NTP needed, but a clock before this library's build is refused).
Return values
{:ok, [%Precompiled.Installed{}]}, the apps this call installed (empty when everything was already there);- with
dry_run: true,{:ok, %Precompiled.Plan{}}; {:error, reason}, wherereasonis one of:{:bad_request, term},{:bad_option, key},{:bad_source, s};{:source_not_allowed, s}, a per-call source that is notfile://;{:content_not_allowed, origins}, per-call content origins (set them in the firmware's:deviceconfig);{:clock_unsynchronized, now}, the batch's times do not hold and the clock is not NTP-synchronized yet;{:missing_app, app}, a network app the firmware leaves out;{:batch, why}, the trust chain refused (whyis the client's batch refusal text);{:no_build, pkg, why}, the first failed rule of the build that got furthest;{:needs, [{app, vsn}]}, withclosure: false;{:not_allowed, app};{:already_present, app, vsn};{:module_loaded | :module_conflict, mod, path};{:noexec, mount};{:conflict, app, [vsn]}, two versions of one app in one call;{:too_big, needed, free}(in bytes,freeundermax_bytes; the line printed names the store and the largest build),{:too_many, n, max};{:enospc, path}, a full disk while staging or committing;{:unsafe_store, why};{:rename, posix},{:bad_staging, path}, or a posix atom such as:eacces, when the store refuses a commit;{:fetch, pkg, why}, a file that did not verify;{:elf, why}, a native library built for another CPU;{:libs, app, reason}, a native build needs a library the device does not have, or a symbol version its library does not define.reason.codeis:needed_lib_missingor:symbol_version_missing(:malformed_entryfor needs that do not read),reason.argsits soname and version, andreason.textthe line;{:on_load_failed, mod, reason};{:start_failed, app, reason};{:in_use, mod, pids},{:in_use_by, [app]},{:not_installed, app};- from
uninstall/2:{:stop_failed, app, why}, the app would not stop or unload, and{:in_use, app}, withremove: true, when its files are still in use; {:report, path, why}, the dry run's report could not be written;:busy, another call holds the store;:disabled;{:internal, message}.
install/2 never raises on the network or on bad input.
mix_install/2, Mix.install/2 for a device (Nerves Livebook), returns
:ok or raises Precompiled.InstallError with one of these reasons, or
one of its own.
The store's lock serializes calls on this node. An installed app whose
start/2 itself calls install/2 waits out the retries and gets
{:error, :busy}.
Summary
Types
What report/1 did. {:sent, id, false} is a new report and its id (what
retracting it needs); {:sent, nil, true} is one the source had already
received. The rest: dumped, cancelled (a no, or no terminal to ask on),
no install to report, an invalid contact:, reporting turned off in the
firmware, or an error.
Functions
Empties the cache: removes every store entry that is not loaded in this
VM, and every document the document cache keeps. Keeps .trust and
whatever this VM is running from. Runs under the store's lock. Prints
one summary line unless quiet: true. bytes counts the documents too.
Installs, loads and starts request and its absent runtime
dependencies. See the module documentation for the options and reasons.
The apps in the store, each with whether it is loaded in this VM ([]
when there is no store yet), or {:error, reason}: a bad option, an
unsafe store, {:internal, message}. Prints one line per app unless
quiet: true.
Mix.install/2 on a running device: installs deps with one
install/2 call, skipping what the device already has. Returns :ok,
or raises Precompiled.InstallError, whose message says why in words.
For a runtime with no Mix, such as Nerves Livebook's
Sends what the last install/2 on this device hit, missed and failed to
the configured source's /reports, after showing it and asking
Send this report to <host>? [y/N]. Only y or yes sends.
Unloads an installed app: stops it, unloads it, purges its modules and
takes its directory off the code path. Its files stay in the store as a
cache unless remove: true.
Types
@type report_outcome() :: {:sent, String.t() | nil, boolean()} | :dumped | :cancelled | :no_report | :invalid | :off | {:error, term()}
What report/1 did. {:sent, id, false} is a new report and its id (what
retracting it needs); {:sent, nil, true} is one the source had already
received. The rest: dumped, cancelled (a no, or no terminal to ask on),
no install to report, an invalid contact:, reporting turned off in the
firmware, or an error.
Functions
@spec clean(keyword()) :: {:ok, %{removed: [atom()], bytes: non_neg_integer(), kept: [atom()]}} | {:error, term()}
Empties the cache: removes every store entry that is not loaded in this
VM, and every document the document cache keeps. Keeps .trust and
whatever this VM is running from. Runs under the store's lock. Prints
one summary line unless quiet: true. bytes counts the documents too.
@spec install(request() | [request()], keyword()) :: {:ok, [Precompiled.Installed.t()]} | {:ok, Precompiled.Plan.t()} | {:error, term()}
Installs, loads and starts request and its absent runtime
dependencies. See the module documentation for the options and reasons.
@spec list(keyword()) :: [Precompiled.Installed.t()] | {:error, term()}
The apps in the store, each with whether it is loaded in this VM ([]
when there is no store yet), or {:error, reason}: a bad option, an
unsafe store, {:internal, message}. Prints one line per app unless
quiet: true.
Mix.install/2 on a running device: installs deps with one
install/2 call, skipping what the device already has. Returns :ok,
or raises Precompiled.InstallError, whose message says why in words.
For a runtime with no Mix, such as Nerves Livebook's:
defmodule Mix do
def install(deps, opts \\ []), do: Precompiled.mix_install(deps, opts)
end
Precompiled.mix_install([:recon, {:circuits_gpio, "~> 2.3"}])
Precompiled.mix_install([:demo], config: [demo: [mode: :fast]])deps takes every shape Mix.install/2 takes: :app,
{:app, requirement}, {:app, opts} and {:app, requirement, opts},
for Hex packages.
- A dep the device has, in its firmware or installed earlier in this
VM, is skipped when its version meets the requirement (any version
when none is given). Another version raises: nothing present is
replaced. OTP's versions read as OTP shortens them (
10.4is10.4.0); a four-part one (11.7.8.1) is checked against no requirement. - Everything else installs in one
install/2call, so its closure, optional dependency and conflict rules see the whole list. - The same deps again (a notebook re-run) install nothing and ask the
source nothing. A call with more deps installs the new ones, where
Mix.install/2would raise: a device's VM outlives a notebook.
A dependency option a device cannot honour raises, naming the dep and the option:
| Option | Here |
|---|---|
:git, :github, :path, :in_umbrella and the git options, :organization | Refused: only signed builds of public Hex packages exist. |
:repo, :hex | Taken when they name hex.pm ("hexpm") and the app's own package; refused otherwise. |
:env | :prod taken; refused otherwise: builds are compiled as :prod. |
:compile, :manager, a non-empty :system_env, app: false | Refused: Mix would build the dependency another way. |
:only, :targets | Refused: a device has no Mix env or target to pick deps by. |
:runtime | false: installed and loaded, not started. The others are then started after the install, and one that fails to start raises with the apps left loaded. |
:override, :optional, :warn_if_outdated | Ignored: a device pins every dependency to the exact version the build that needs it was compiled with, and refuses a conflict rather than overriding it. |
| anything else | Refused. |
The options of Mix.install/2:
| Option | Here |
|---|---|
:config | Put into the application env (persistent: true) before anything is planned, as Mix.install/2 does before it compiles: builds are compared with these values. It raises, changing nothing, when it names an app of OTP or Elixir, or one this library runs on or reads (itself, its runtime closure, castore, nerves_runtime, nerves_time): those decide how this call and every later one verifies, connects and reads the device. It raises too when it changes a key that an app the device has read at compile time, as far as that is known: for an app installed earlier, from any store dir, every read its build was matched on, which this VM records when it activates the build; for a firmware app, only its .app's, which Mix writes for the compile_env/3 reads of Application only. While any app the device has has reads that cannot be told, every config: is refused, naming that app: its reads may be of any app's keys. The checks, the put and the install run under the store's lock; nothing is put when the call is refused, an unsafe store included. It stays put when the install then fails. |
:start_applications | false loads without starting. |
:elixir | Raises when this Elixir misses it. |
:verbose | true prints install/2's lines whatever the firmware's quiet says, and one line per dep skipped. |
:config_path, :lockfile, a non-empty :system_env | Refused: a device cannot check a build against a config file's env, a lock's versions or variables set for the compile. |
:force, :consolidate_protocols, :compilers, :stop_unused_applications, :runtime_config | Ignored: they change no build here. A cached build is checked against the current batch on every install, so force: true would fetch the same bytes. |
Any other option is install/2's (:source as a file:// bundle,
:dir, :quiet, ...), except :dry_run, :report and :start, which
raise. The firmware's :device config applies as for install/2, but
for its start default: start_applications: decides, as in
Mix.install/2. A call refused before it installs records that no
install ran, as install/2 does, for report/1.
@spec report(keyword()) :: report_outcome()
Sends what the last install/2 on this device hit, missed and failed to
the configured source's /reports, after showing it and asking
Send this report to <host>? [y/N]. Only y or yes sends.
Precompiled.install(:circuits_gpio)
Precompiled.report() # show, ask, send
Precompiled.report(yes: true) # no question
Precompiled.report(contact: "@me") # a handle for follow-up questions
Precompiled.report(dump: true) # print the exact JSON, send nothingOptions: yes: true skips the question; contact: adds a handle (at
most 100 characters); dump: true prints the JSON and dump: path
writes it, sending nothing; :dir is the store, as for install/2.
The question is asked only in an interactive shell (IEx on the console
or over ssh). Without one (an rpc, a script) it shows the summary and
cancels, unless yes: true. The source is the firmware's config (or the
official API), never a per-call option, and never a file:// bundle.
Sent: each candidate's name, version, HIT/MISS/FAIL, reason code and
arguments; the Elixir and OTP versions, OS family and architecture; the
target, Nerves system and its version when the source knows that system
(otherwise custom); and the version and checksum of each package the
install planned and of its build's dependencies. Never sent: paths,
host name, serial number, config values, environment variables or the
printed details. A new report prints its id, the one thing needed to
retract it (DELETE <source>/reports/<id>). Never raises.
Unloads an installed app: stops it, unloads it, purges its modules and
takes its directory off the code path. Its files stay in the store as a
cache unless remove: true.
Refuses {:in_use_by, apps} while other installed apps that are loaded
need it, unless cascade: true (which unloads them first). Refuses
{:in_use, mod, pids} while other processes run its code, unless
force: true.
"In use" means code: processes running the app's modules, as
:code.soft_purge/1 sees them. Data is not detected: an open NIF
resource or a reference into the app (a GPIO handle from
Circuits.GPIO.open/2, say) held by a shell or another process does not
stop the uninstall. Close such handles (Circuits.GPIO.close/1) before
uninstalling.