Docs

Platform overview

NSH runs on phones, desktops, terminals and the web: iOS, Android, macOS, Windows, Linux, GPUI, the terminal and web pages. Each has a backend of its own that draws with the platform’s own UI, so an app is UIKit views on an iPhone, AppKit views on a Mac, GTK 4 widgets on Linux, GPUI elements in a GPUI app, character Cells in a terminal and HTML in a browser, with the platform’s text, input, accessibility and dark mode. Everything above the backend is shared: the reactive graph, the renderer, Taffy layout, the components and the native modules are the same Rust code everywhere, so one app runs on all of them. An app written in Rust is that code and your crate, with no JavaScript VM; a TSX app runs its modules on QuickJS on top of it (Building with Rust).

On every platform, an app is used the same two ways: packaged as an installable app of that platform (nsh package), or embedded in a native app of your own, whether the host links a Rust root or loads TSX at run time. You develop it with nsh run (Rust edits rebuild and restart the app, TSX edits are hot updates).

#At a glance

Platform Backend; text Runs on Develop Package
iOS UIKit; CoreText iOS 15.0+, devices and the simulator nsh run --target ios .app (simulator), signed .ipa
Android Android views; StaticLayout Android 8 (API 26)+, arm64-v8a, armeabi-v7a and x86_64 nsh run --target android signed .apk
macOS AppKit and Core Animation; CoreText macOS 14+ nsh run --target macos ad hoc signed .app
Windows Win32 windows drawn with Direct2D; DirectWrite Windows 10 1809+, Windows 11 nsh run (the default on Windows) a directory, a .zip and an installer
Linux GTK 4 widgets; Pango GTK 4.14+ nsh run (the default on Linux) a directory and a .tar.gz
GPUI GPUI; GPUI’s text system macOS, Windows, Linux (Vulkan) in your GPUI app; TSX also with nsh run --target gpui inside the GPUI app that embeds it
Web HTML and CSS; the browser’s fonts modern browsers, desktop and phone nsh run --target web a static site
TUI ratatui on the terminal’s Cell grid; the terminal’s font terminals on macOS, Windows and Linux nsh run --target tui inside the terminal app that embeds it
  • GPUI draws apps with GPUI, the framework of the sibling project GPUI Kit, on macOS, Windows and Linux alike: it is the backend for embedding in GPUI and GPUI Kit apps, whose AppView they place in their own element tree. Standalone desktop apps run and are packaged with each OS’s own backend (AppKit, Win32, GTK 4).
  • TUI draws apps on the terminal’s grid of character Cells, in the terminal’s own colors, keyboard first and with the mouse: nsh run --target tui runs an app in the current terminal, and a ratatui app embeds one with AppView.
  • Web renders apps as HTML in a browser page: nsh package --target web publishes a static site for any web server or CDN, with a Rust app’s engine and crate compiled to WebAssembly, and a page of your own embeds an app with createPlayer or an iframe. It also runs the live demos and the playground on this site.
  • Mac Catalyst runs the iOS backend in an iPhone-sized window (390×844) on a Mac, for development. Select it with --target catalyst to see how an app looks on a phone.

More platforms are built by the community on the same engine, each in a repository of its own, and the nsh command line runs them by name: nsh-platform-miniprogram runs apps in WeChat Mini Programs (--target miniprogram). Building a platform shows how one is made.

#Develop and package

nsh run picks the machine’s own platform when you give no --target: AppKit on macOS, the GTK window on Linux and the Win32 window on Windows. Phones are reached from a computer: iOS from a Mac with Xcode, Android from macOS, Linux or Windows with the Android SDK (Installation).

A Rust app (its nsh.json names the "root" component) runs and packages from its directory: nsh builds the app’s own runner for the target, with your crate and no QuickJS. A TSX app names its entry:

# A Rust app, from its directory
nsh run --target macos             # or ios, android, windows, linux, tui, web, catalyst
nsh package --target macos         # or ios, ios-simulator, android, windows, linux, web

# A TSX app
nsh run App.tsx --target macos     # or ios, android, windows, linux, gpui, tui, web, catalyst
nsh package App.tsx --target macos

Each desktop OS packages apps with its own backend, built for the machine it runs on: package a Mac app on a Mac, a Windows app on Windows and a Linux app on Linux. Packaging covers every target’s output, signing and icons.

#Embed in a native app

Platform Host view Runs Ships as Host language
macOS AppView (an NSView) a Rust root, TSX bundles the nsh-platform-macos crate Rust
Windows AppView (a child window) a Rust root, TSX bundles the nsh-platform-windows crate Rust
Linux AppView (a GTK widget) a Rust root, TSX bundles the nsh-platform-linux crate Rust
GPUI (macOS, Windows, Linux) AppView (a GPUI view) a Rust root, TSX bundles, signed packages the nsh-platform-gpui crate Rust
iOS AppView (a UIView) a Rust root, TSX bundles the nsh-platform-ios crate Rust
iOS NshAppView (a UIView) TSX bundles, signed packages NativeShell.xcframework Swift
Android NshView (a FrameLayout) TSX bundles, signed packages nsh.aar Java or Kotlin
Web createPlayer (a page element) or an iframe TSX bundles; a Rust app as its published site the runtime files from nsh package --target web JavaScript or HTML
TUI AppView (an area of a ratatui frame) a Rust root, TSX bundles the nsh-platform-tui crate Rust

A Rust host mounts a Rust root (Root::of::<App>()) as AppSource::Rust or App::Rust, and gives its engines the app’s UI sets with set_ui_sets, so TSX it loads imports the app’s own components and modules. Each view is one engine, and an app can show several. The host registers its own native views for apps to use on every platform (Native components).

#Share a mobile host’s Tokio runtime

A Rust-linked mobile host can run NSH’s async tasks on the Tokio runtime it already has. Each view keeps a clone of the runtime’s Handle; the app owns the runtime and keeps it running (with IO and time enabled) until its views stop. None uses NSH’s shared runtime.

On iOS, set the runtime in the options of AppView (or Surface); every load of the view uses it:

let mut options = SurfaceOptions::default();
options.runtime = Some(runtime.handle().clone());
let view = AppView::new(mtm, frame, options);
view.load(bundle);

On Android, call nsh::set_runtime on the UI thread with the NshView’s native handle (its handle field), before the view loads an app or connects to a dev server. The setting belongs to that view and applies from its next load; a running engine keeps the runtime it started with. A JNI function in the app’s own library reads the handle:

#[unsafe(no_mangle)]
extern "system" fn Java_com_example_app_RuntimeBridge_configure(
    mut env: JNIEnv<'_>,
    _: JClass<'_>,
    view: JObject<'_>,
) {
    let Ok(handle) = env.get_field(&view, "handle", "J").and_then(|v| v.j()) else {
        return;
    };
    // SAFETY: called on the UI thread with a live NshView of this library.
    unsafe { nsh::set_runtime(handle, Some(app_runtime().clone())) };
}

In minified builds, keep the bridge class’s names (-keep class com.example.app.RuntimeBridge { *; }); the distributed AARs already keep the com.longbridge.nsh package, NshView.handle included.

#Writing for every platform

Most of an app needs no platform checks: elements, styles, layout and modules behave the same everywhere. Where platforms differ, NSH gives the difference one place (Platform-specific code):

  • The environment says where the app runs: in Rust os(), idiom() and is_desktop() (nsh_core::prelude), in TSX platform.os, platform.idiom, platform.desktop and platform.select from nsh. The OS is iOS, Android, macOS, Windows, Linux or the web; in a terminal, the idiom is "terminal" and the OS the machine’s own.
  • Platform-only props go in ios={{…}}, android={{…}}, macos={{…}}, web={{…}} or desktop={{…}} in TSX (desktop: macOS, Windows, Linux, desktop browsers and terminals).
  • Input follows the platform. Phones get touches and the software keyboard; desktops get the mouse, the trackpad and the keyboard.

An app’s components are drawn with NSH’s primitives, so they run on desktops as well as on phones, looking the same on both. A desktop layout is built from the primitives (view, text, scroll, virtual_list, …; View, Text, Scroll, List, … in TSX) and a check for the desktop, as the gallery’s desktop layout is:

use nsh_core::prelude::*;

#[component]
pub fn Shell() -> impl IntoElement {
    // macOS, Windows, Linux and desktop browsers get a sidebar; phones get tabs.
    view()
        .flex_1()
        .child(if is_desktop() { sidebar_layout() } else { tab_layout() })
}

fn sidebar_layout() -> AnyElement {
    view().flex_1().flex_row().into_element()
}

fn tab_layout() -> AnyElement {
    view().flex_1().flex_col().into_element()
}

Each backend lists what it does its own way: macOS, Windows, Linux, GPUI, TUI.

#See also