Skip to main content
This guide continues from the end state of Basic Nerves device setup. Work through that guide first. At the end of this one you have a production-ready device that:
  • authenticates with its own certificate, signed by a CA you control,
  • decides when to apply an update, downloads it in verified parts, and validates the new firmware,
  • reports its own location, metrics, alarms and network identities, and
  • uses the connection options and extras that fit your product.

Before you start

You need the basic guide’s end state:
  • The my_app product in the acme organization, and a device that connects with a shared secret or a local certificate.
  • The config :nerves_hub_link block from the basic guide’s closing section in config/target.exs, with fwup_public_keys, remote_iex: true and five extensions.
  • The signing key my_app_key.
  • The bench deployment group, turned on, with your device in it. This guide releases through bench. If you have already replaced it with the groups from Basic fleet operations, use qa wherever this guide says bench.
The basic guide ended at 1.4.0, and each part here ships one version, 1.5.0 to 1.8.0. If you skipped the delta part of the basic guide your device is on a lower version, and these numbers still work. Build and release every version the same way, from the project directory with MIX_TARGET=rpi4 exported. The examples keep using a Raspberry Pi 4; any of the Nerves supported targets works, with its name in place of rpi4:
Every config snippet adds to the one config :nerves_hub_link block in config/target.exs. The closing section shows the whole block.

The parts

  1. Move to certificate authentication
  2. Control when and how updates are applied
  3. Turn on the rest of the extensions
  4. Tune the connection and add the extras
  5. Where you are now: the complete configuration and what to do next

Part 1: Move to certificate authentication

A product’s shared secret is compiled into every firmware image, and whoever holds it can connect as any device in the product. For production, give each device its own X.509 certificate. Security: PKI and the chain of trust explains the model. You sign device certificates with a certificate authority (CA) that you control and register with the acme organization. A just-in-time provisioning profile on the CA lets new devices create themselves in my_app, tagged prod, the first time they connect. Registering a CA needs the manage role or higher. If you chose the Local certificate tab in the basic guide, your firmware already uses LocalCertKey with a self-signed certificate. Do the first three steps of the Local certificate tab below to replace that certificate with one your CA signed. You have no shared secret to retire. Choose where the device keeps its private key.
The certificate and key are files on the device. See Device certificates.
1

Create and register the CA

This writes the CA key and certificate to ~/.nh/ca/acme/, named by the generation time, for example 2026-06-11-16-41-10-cert.pem. That timestamp is the CA’s name in the next step. Only the certificate is uploaded. Keep the key secret.To bring a CA you already have:
The --jitp-* flags give the CA its JITP profile, and need NervesHub 2.5.0 or later. nh sets the profile only when it registers the CA. To add or change it later, open the organization’s Certificates page, click the certificate authority, and click Edit. Check Enable Just In Time Provisioning, choose my_app as the JITP Product, enter prod in JITP Tags, and click Update Certificate.
2

Sign a device certificate

The certificate’s common name must be the device identifier, which is the device’s serial number.
The key and certificate are written to ~/.nh/certificates/acme/ as <identifier>-<timestamp>-key.pem and <identifier>-<timestamp>-cert.pem.
3

Put the files on the device

The files go to /data/nerves-hub/cert.pem and /data/nerves-hub/key.pem. How you get them there depends on where the device is:
  • A device on your bench with SSH access. Use sftp, as in part 1 of the basic guide.
  • A device in the field that is connected to NervesHub. Write the files from the remote console, which needs no network path to the device:
4

Switch the firmware to LocalCertKey

In the config :nerves_hub_link block, replace the shared_secret option with the configurator:
LocalCertKey is never chosen automatically. For other file paths, see Configure NervesHubLink.

Release 1.5.0

Set the version to 1.5.0, then build and release it to bench.
Send this firmware over the air. Do not burn it. Burning the card erases /data, and the certificate files with it.
The shared secret and certificates work side by side, so you move devices that are already in the field with this ordinary update:
  1. Leave the product key active.
  2. Make sure each device has its certificate before it updates. A device running the new firmware has no other way to connect.
  3. Release the firmware. Each device reconnects with its certificate after it updates. Devices still on the old firmware keep connecting with the shared secret.
An existing device keeps its product, tags and history. NervesHub records the certificate for the device whose identifier matches the common name. The profile’s product and tags apply only to devices that do not exist yet.

Retire the shared secret

When every device has a certificate on file, open Product settings, and in the Shared secrets card click Deactivate next to the key, then Deactivate secret to confirm. New connections that use the key are refused. Connections that are already open stay open until the device reconnects. See Deactivate a key.
A device that still depends on the product key drops off the next time it reconnects. Check each device’s certificates before you deactivate the key.

Check it works

  1. Run nh ca show <serial>, with the serial from nh ca list. It prints the JITP product my_app and the tag prod. The organization’s Certificates page lists the CA with Adds new devices to my_app.
  2. Wait for your device to update to 1.5.0. It comes back Online. A device NervesHub has not seen before, booted with this firmware and a certificate from the CA, appears under Devices in my_app with the tag prod.
  3. Confirm the certificate is on file. It is also listed in the Certificates card on the device’s Settings tab. See Devices.
  4. After you deactivate the product key, force a fresh connection. The device comes back Online.
Go deeper: Device certificates, Just-in-time provisioning, NervesKey, TPM, Shared secret, Devices, Security: PKI and the chain of trust.

Part 2: Control when and how updates are applied

Add the client module

NervesHubLink.Client is the behaviour through which nerves_hub_link asks your application what to do with an update. Create lib/my_app/nerves_hub_link_client.ex:
MyApp.busy?/0, MyApp.Display and MyApp.shutdown_gracefully/0 stand for your own code.
  • {:reschedule, 15, reason} turns the update down for now and reports the reason. NervesHub offers the update again after 15 minutes. The other return values are in Applying updates.
  • reboot/0 must end in a reboot, or the new firmware never runs.
  • nerves_hub_link reports whether the running firmware is validated, but it does not validate it. Your application has to, and a device that reports unvalidated firmware is not offered updates by its deployment group. The connected/0 callback is one reasonable place to validate, and Applying updates covers the alternatives. If your application already validates somewhere else, leave connected/0 out. A project generated by mix nerves.new can already do so through the nerves_runtime startup guard: check config/target.exs for config :nerves_runtime, startup_guard_enabled: true.
An exception in update_available/1 results in the update being applied, and so does any return value that nerves_hub_link does not recognise. That is why the callback above has a rescue.

Select the client and the download strategy

Add two keys to the config :nerves_hub_link block:
PartsUpdater downloads the firmware in parts and verifies each one. It keeps the parts in /data/nerves_hub_link/firmware, so the device needs room for the firmware file there. It needs NervesHub 2.5.0 or later. The other strategies and the retry settings are in Download strategies. Set the version to 1.6.0, then build and release it to bench.

Optional: let the device ask for its updates

With the client module, NervesHub still picks the moment to offer an update and your code answers. For a device whose owner or state decides the moment, use device-managed updates: the deployment group stops pushing, and the device fetches the group’s firmware when it chooses.
Roll out firmware that supports device-managed updates before you set the mode. A device in device_managed mode that connects with older firmware is returned to automatic and receives pushes again.
1

Ship the three calls

Call this from your own code, for example when the owner presses “Update now” or a machine cycle ends:
update_now/0 returns :ok once NervesHub has sent the update, and the error when it is refused. The reasons are listed in Ask for an update from firmware.An update started this way still goes through MyApp.NervesHubLinkClient, so update_available/1 can still turn it down.
2

Roll that firmware out

Include the module in 1.6.0. Release it through bench while the device is still in automatic mode, and wait until the device runs it.
3

Grant the permission and set the mode

An operator has to allow a device to choose its mode. On the device’s Settings tab, in the Firmware updates card, switch on Let the device choose its update mode, or run:
Then call MyApp.Updates.take_control() on the device. To skip the grant, set the mode yourself with nh device update <identifier> --update-mode device_managed, or in the device page header, select the update mode and choose Device managed.
NervesHub does not pace device-requested updates. The group’s rollout controls do not apply to them, so if many devices could ask at the same moment, spread the requests in your firmware.

Check it works

  1. The device takes 1.6.0 at once, because the firmware it was running had no client module. The client and PartsUpdater act from the next update on.
  2. In the remote console on the updated device:
    The last line applies only if you set up device-managed updates. Otherwise it returns :automatic.
  3. Part 3 releases 1.7.0, the first update the client decides on. While MyApp.busy?/0 returns true, the device stays on 1.6.0. When it returns false and the update is offered again, the device’s Details tab shows download and install progress, and the device reboots into 1.7.0. If you set up device-managed updates, the group offers nothing: call MyApp.Updates.update_now() on the device instead.
Go deeper: How firmware updates work, Applying updates, Download strategies, Device-managed updates, Connection lifecycle, Rollout controls.

Part 3: Turn on the rest of the extensions

Each section is independent, so skip what your device does not need. The config for all of them is in one snippet, in Configure the extensions. Every extension also has to be on for the product, in Product settings on the General tab, in the Device extensions card. A product created through the API or nh starts with them all off. See the extensions overview.

Report your own location

Skip this unless the device has a better location source than its public IP address, such as GPS or a modem. If it does, write a resolver module. Use your own resolver has a complete one, which the config below names MyApp.GPSGeoResolver. Return errors from it as {:error, code, description}. That three-element shape is the one the extension matches on.

Add your own metrics and metadata

A metric set is a module with one callback that returns a map of names to numbers. See Health.
The config below adds it after :defaults, which keeps the default metric sets. Metadata is a map of strings beside the metrics, set under metadata in the same config.

Raise alarms

Alarms have no extension of their own in nerves_hub_link. The Health report carries every alarm currently raised, so NervesHub learns of a change at the next report. An alarm raised and cleared between two reports is never seen. Add alarmist, which the nerves_hub_link guide recommends, and the report reads alarms from it:
Raise and clear alarms through Erlang’s :alarm_handler:

Add context to error reports

Point context at a function that returns a map, and every error report carries it:

Report network identities

If the device is on iroh, NetBird, Tailscale or WireGuard, the Network identity extension shows its key on the device page. The extension has been in your firmware since the basic guide, but it reports nothing without a provider. Write one provider module per network. Enable it has a complete NetBird provider, which the config below names MyApp.NetBirdIdentity. Never put a secret in a provider’s details. Anyone who can view the device can read it.

Configure the extensions

In the config :nerves_hub_link block, replace extension_modules and logging from the basic guide with these keys. Drop the lines for any section you skipped.
extension_modules replaces the default list, so it names all five from the basic guide. Set the version to 1.7.0, then build and release it to bench.

Set a health profile

Custom metrics are charted, but they only affect a device’s health status once they are in a health profile. Profiles need ClickHouse on the instance. See Feature prerequisites. Open Product settings and select the Device health tab. Pick Default under Profiles, use Add a metric… to add battery_percent, and set Warning at and Unhealthy at. The metric is listed once a device has reported it. Click the ≥ button to flip it to ≤, because a low battery is the problem. Click the star in the Featured column to give the metric a tile on the device page, then click Save profile. Edit a profile covers each control, and How a threshold engages explains how to choose the period.

Check it works

On the device, after it has updated to 1.7.0 and reconnected:
Each extension shows attached?: true, the health report holds battery_percent, and your provider returns an identity. On the device page in NervesHub:
  • Details tab: the Location card names your source (the card needs a map token on the instance), the Health card has a Battery percent tile and an Alarms row, and the Network identities card lists the NetBird key. The Metadata list has a Placement row. Without a map token it also has a Location row, which shows the coordinates and the source.

Optional: add a system shell

The Local shell extension opens sh on the device’s operating system. It is optional, and mostly recommended for devices that are not Nerves devices, such as ones running the Linux agent.
A local shell lets a NervesHub user with the device:extensions:local_shell permission run commands on the device. Check who holds it in Roles.
To add it, add the expty library:
Then add the module to extension_modules:
Switch on Local shell for the product, release the firmware, and open the device’s System Shell tab. A terminal opens.
NervesHub also has standalone Metrics and Alarms extensions. nerves_hub_link 2.14.0 implements neither, so turning them on for the product does nothing for a Nerves device. See Metrics and Alarms extensions.
Go deeper: Extensions overview, Geo, Health, Health monitoring, Error reports, Logging, Local shell, Remote console, Network identity, Metrics and Alarms extensions.

Part 4: Tune the connection and add the extras

Nothing in this part is required. Each section says who it is for, so skip what does not apply. The first four change firmware, which you release as 1.8.0. Support scripts and connecting code need no firmware change.

TLS 1.3 session resumption

For devices on slow or metered links, such as LTE-M. A reconnect resumes the TLS session instead of repeating the full handshake, so it sends and verifies no certificates. The instance must accept TLS 1.3 on its device endpoint, which is off by default. See Feature prerequisites. You need NervesHub 2.5.0 or later, and OTP 25.1 or later on the device. Add ssl to the config :nerves_hub_link block:
Keep :"tlsv1.2" in the list so the device still connects to a server without TLS 1.3. If you already set ssl: for certificate file paths, add these two keys to that list. Tickets, firmware downloads and the server side are covered in Connection transport.

Msgpack

For the same metered or low bandwidth devices. Msgpack encodes the device’s messages in fewer bytes than JSON. It needs nerves_hub_link 2.13.0 or later and NervesHub 2.5.0 or later.
A device configured for Msgpack cannot connect to a server that does not support it. A device that cannot connect cannot receive the firmware that would change the setting back. Confirm your server is NervesHub 2.5.0 or later before you build this into firmware.
Add the dependency to mix.exs, then serializer to the config :nerves_hub_link block:
NervesHub needs no setting. See Msgpack serialization.

HTTP proxy

For devices that can only reach NervesHub through an HTTP proxy. You configure the proxy twice, once for the WebSocket and once for the firmware downloader. The snippet is in HTTP proxy.

Archives

For data that changes on a different schedule from firmware, such as calibration data or a model. An archive is a signed fwup file that travels with a release. nerves_hub_link downloads and verifies it, then hands your code the path. The default client ignores archives. Add both callbacks to MyApp.NervesHubLinkClient:
MyApp.Archives stands for your own code. It must remember the UUID, because NervesHub offers the archive again on every connect. Archives are verified with the keys in archive_public_keys. When that is not set, the device uses fwup_public_keys, which your firmware already has, so there is nothing to add. Build the archive as Set it up shows, sign and upload it with the key you use for firmware, and attach it to the group once the device runs firmware with the callbacks:
Set it up also shows the web UI path. The second command creates a release, and the archive travels with that release only. A release made with nh firmware upload --deploy has no archive, so attach the archive again after your next firmware release if devices still need it.

Support scripts and connecting code

For anyone who supports devices in the field. A support script is Elixir saved on the product that you run on one online device, with the output returned to you. It does not need remote_iex. Managing scripts with nh needs NervesHub 2.5.0 or later.
Connecting code is Elixir that NervesHub sends to a device when it connects. Set it on a device, a deployment group or a release. For the bench group, open the group, select the Settings tab, type the code into the Connecting code card and select Save.
Connecting code runs on every connect, including each reconnect after a network drop. Write code that is safe to run repeatedly. nerves_hub_link stops it after 10 seconds.

Devices that sleep

For battery devices that wake, connect, work and sleep again. A device that says it is going to sleep shows as asleep with a wake time instead of offline, and the sleep does not count as a disconnect.
This feature is on the main branch of nerves_hub_link and NervesHub but is in no tagged release as of nerves_hub_link 2.14.0 and NervesHub 2.5.4. You need a build from main of both to use it.
Call this just before the device sleeps:
The call closes the connection. A device that restarts on waking connects at boot as usual. One that keeps running calls NervesHubLink.reconnect() when it wakes. See Devices that sleep and, for reconnecting in general, Connection lifecycle.

Check it works

1

Release 1.8.0 to a device you can reach

Set the version to 1.8.0, then build and release it to bench only, so the first device to take it is one you can reach physically. A transport setting the server does not accept leaves a device unable to connect, and so unable to take a fix.
2

Confirm it connects

If you turned on Msgpack, check the device log does not contain using JSON instead. That line means the setting was not applied.
3

Run the support script

Run nh device run-script <identifier> "Memory" for that device. Output coming back shows the device is connected and can run scripts.
4

Trigger the connecting code

Run nh device reconnect <identifier>. If the code fails, NervesHub raises a product notification.
Go deeper: Connection transport, Connection lifecycle, Archives, Support scripts, Connecting code, Devices that sleep, Feature prerequisites.

Where you are now

Your device runs 1.8.0.

The complete configuration

This block holds everything from the basic guide’s block, with shared_secret replaced by certificate authentication in part 1, and everything this guide added. Lines for sections you skipped are not in yours.
The guide also added dependencies to mix.exs: nerves_key (NervesKey tab only), alarmist and msgpax, plus expty if you added the optional system shell.

Where to go next

Keep the bench group. Basic fleet operations uses it for one last release, then deletes it.
  • Basic fleet operations: firmware built and signed in CI, qa and production groups, and Slack notifications.
  • Complete fleet operations: a pipeline that rolls out through canaries with approval, webhooks into your own backend, and rollout guardrails.
For everything the guides did not cover, start from the page that maps each area: