Precompiled (Precompiled v0.10.2)

Copy Markdown View Source

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.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.

Options

OptionDefaultMeaning
:sourceconfig :precompiled, :device, source:, else the official APIPer 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_secondsas on the hostSame meaning as the host config key.
:cacerts_pathnoneA PEM CA bundle to verify an https source with (a path).
:dir/data/precompiledThe 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.
:starttrueStart the installed apps (:temporary) after loading them.
:closuretrueInstall 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_bytes25, 64 MBBounds on the apps of one call and on the store's size.
:dry_runfalseSelect and plan, verify the trust chain, fetch the manifests and .app files only. Returns the plan. It writes no entry, staging dir or eviction: only the trust state in DIR/.trust, and the store dir and its marker when they do not exist yet.
:reportnoneWith dry_run: true, write the device facts and the plan as JSON to this path (for mix precompiled.bundle --device-report).
:quietfalsePrint nothing.
:lock_retries60For 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_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}, where reason is one of:
    • {:bad_request, term}, {:bad_option, key}, {:bad_source, s};
    • {:source_not_allowed, s}, a per-call source that is not file://;
    • {:content_not_allowed, origins}, per-call content origins (set them in the firmware's :device config);
    • {: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 (why is the client's batch refusal text);
    • {:no_build, pkg, why}, the first failed rule of the build that got furthest;
    • {:needs, [{app, vsn}]}, with closure: 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, free under max_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.code is :needed_lib_missing or :symbol_version_missing (:malformed_entry for needs that do not read), reason.args its soname and version, and reason.text the 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}, with remove: 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.

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. Keeps .trust and whatever this VM is running from. Runs under the store's lock. Prints one summary line unless quiet: true.

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.

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

report_outcome()

@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.

request()

@type request() :: atom() | {atom(), String.t()}

Functions

clean(opts \\ [])

@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. Keeps .trust and whatever this VM is running from. Runs under the store's lock. Prints one summary line unless quiet: true.

install(request, opts \\ [])

@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.

list(opts \\ [])

@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.

report(opts \\ [])

@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 nothing

Options: 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.

uninstall(app, opts \\ [])

@spec uninstall(atom(), keyword()) :: :ok | {:error, term()}

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.