Requirements
- Nothing is needed in firmware config. A device needs a client that can run scripts sent by NervesHub.
- A device that reports device API version 2.1.0 or later receives the code as a script and reports how it went. For an older device, NervesHub types the code into the device’s remote console instead.
- Connecting code on a release needs NervesHub 2.5.0 or later.
- Notifications for failed connecting code need NervesHub 2.5.1 or later.
- Setting a device’s connecting code through the API or
nhneeds NervesHub 2.5.3 or later.
Set connecting code
On a deployment group
1
Open the group's settings
Open the deployment group and select the Settings tab.
2
Enter the code
Type your Elixir into the Connecting code card.
3
Save
Select Save in the bar at the foot of the card.
On a release
A release can carry its own code, which a device runs while it is running that release.- Web UI
- CLI
In the New release dialog, expand Connecting code, enter the code, and choose where it runs under Runs. To change it later, open the release on the Releases tab and select Edit release.
A release with no code of its own leaves the group’s code alone, whatever its mode.
The release that counts is the one the device is running: the newest release in its group whose firmware the device has. A device that has not yet installed a release does not run that release’s code.
On a device
Open the device, select the Settings tab, and enter the code in First connect code. From the CLI:nh cannot confirm what was saved.
What a device runs
When a device connects, NervesHub joins the code that applies to it into one script, in this order:- The release’s code, if its mode is
first. - The group’s code, unless the release’s mode is
override. - The release’s code, if its mode is
last. - The device’s own code.
nerves_hub_link evaluates the script with Code.eval_string/1 and stops it if it runs longer than 10 seconds.
Check that it works
Connect a device, or reconnect one that is already online:- the client reports an error, as
nerves_hub_linkdoes when the code times out or its task crashes, or - the output shows that the code raised an exception.
nil return value alone is not a failure.
Devices that fail on the same code share one notification. It names the first device that failed and what it printed. The notification reaches Slack, Microsoft Teams, Discord and webhooks like any other product notification.
A device on a nerves_hub_link release that does not report a result is only caught when the code raises. Code that hangs on such a device is never reported.

