.nib guide std nib 0.3.1-pre

Effectors in another process

System.Remote binds an interface to a daemon over a socket — nib's foreign-function interface, typed by the interface, versioned by its declaration, journalled like any effect.

An interface is a contract; an effector is whatever answers it. System.Remote is an instance that answers any interface by sending each operation to another process over a Unix or TCP socket. The calling code does not change: it performs @Sql.Db.query(…) and receives a typed answer or a declared raise, whether the effector is in the same process or not. That makes it nib's foreign-function interface: the other side can be written in any language that speaks the protocol.

Binding

use "std/system.nib"

wire Main runs App.main where
  Sql.Db = System.Remote{ at: "unix:/run/nib/sqlite.sock" }

at is unix:/path or tcp:host:port. A remote instance can sit under a wrapper like any other (System.Remote{ … } |> Audit). The checker refuses an interface that cannot cross a process boundary: one with type parameters, or with an operation whose parameters or answer are not data — Int, Float, String, Bool, Json, Bytes, (), a Vec, Set or Map of data, a record, or a data type built from those. A function, an interface or a handle cannot be sent.

The protocol

Each message is a frame — u8 version (1) | u32 big-endian length | UTF-8 JSON — at most 64 MiB.

  1. nib connects on the first operation and sends hello: the interface name, its version, and each operation's name, parameter types, answer type and raises. The version is the first 16 hex digits of a SHA-256 of the interface's declaration as nib prints it, so a daemon pinned to a version refuses a program written against any other declaration.
  2. The daemon answers welcome, or refuse with a reason.
  3. Each operation is a request {op, args}, answered by ok {value}, raise {name, args} — which must be one of the operation's declared raises, and arrives at the program's rescue arm with its payload typed — or refuse.

Values are JSON: () is null, a record an object, a Vec an array, a sum {"tag": …, "args": […]}, a Map {"map": [[k, v], …]}, a Float {"float": "2.5"} (exact), Bytes {"bytes": "<hex>"}. Answers are decoded against the declared types, and an answer that does not fit is an error.

Writing a daemon

The reference implementation is the Rust crate system/tools/nib-remote: implement its Service trait for your interface and call serve, which handles the framing, the handshake and one thread per connection. The crate ships nib-sqlite DB unix:/path, a daemon answering Sql.Db (exec, query) over SQLite, and nib/example.nib beside it uses it, including a rescue of SqlError.

Failure, and the journal

You start the daemon; nib never spawns one. A missing daemon, a closed socket, a malformed frame, a refused hello or an answer that does not decode is a transport error: it ends the run with a message naming the address, interface and operation, and no rescue can catch it — the program cannot mistake a broken FFI for an answer. There is no timeout on the nib side, and a call blocks the run's one lane, so a slow daemon stalls every fibre.

Every remote call is journalled with its decoded arguments and answer, so a recorded run replays without the daemon running at all. A Secret argument is sent with its value but journalled only by its label.