Docs

Linux

NSH apps also run on Linux. The Linux backend renders them with GTK 4 widgets and lays out text with Pango; the core, the reactive graph, the renderer, Taffy layout and the native modules are the same as on iOS and Android, so one app runs everywhere. On Linux, os() is Os::Linux (platform.os is "linux" in TSX) and TSX’s desktop={{…}} props apply. The window’s title, a title bar of the app’s own, the menu bar and context menus come from nsh_core::element::desktop in Rust (Desktop title bars) and nsh/desktop in TSX.

#Requirements

  • GTK 4.14 or later. To build a Rust app’s runner or NSH’s Linux runtime (from a checkout, or cargo xtask dist on Linux) you need their development packages and clang: libgtk-4-dev clang on Debian and Ubuntu (24.04 or later), gtk4 clang on Arch, gtk4-devel clang on Fedora.
  • WebKitGTK 6.0 (2.42 or later) for web views and download decisions: libwebkitgtk-6.0-dev on Debian and Ubuntu, webkitgtk-6.0 on Arch, webkitgtk6.0-devel on Fedora to build the runtime. The development host and the release runner use it by default, so packaged apps need WebKitGTK 6.0 where they run (libwebkitgtk-6.0-4 on Debian and Ubuntu; GNOME desktops have it). cargo xtask dist on a machine without its development files builds a runtime whose web views are placeholders, and whose apps need only GTK. An app with its own Rust crates builds its development host and runner with WebKitGTK only when it makes a WebView, so nsh run of an app without one compiles none of it.
  • GStreamer 1.14 or later, which GTK plays video through and NSH plays sound with. Nothing of it is needed to build: NSH loads GStreamer when an app first plays a sound. Where apps run, GStreamer and its base plug-ins (desktops have them), and the plug-ins for the video and audio formats your app plays. Without GStreamer an app still starts; loading a sound then fails with an error naming the missing library. nsh package leaves sounds (the runtime’s Cargo feature sound) out of a TSX app’s runner when the app imports no Sound; video and AudioPlayer play through GTK either way.

#Running an app

On Linux, nsh run opens the app in a GTK window. A Rust app runs from its directory:

nsh run                        # --target linux is the default on Linux

nsh builds a debug runner with your crate and opens the app, with its logs (log::info! and the rest) in the terminal; a change to the crate’s Rust code builds the runner again and restarts the app. cargo run starts the same root from the crate’s src/main.rs, which nsh init --template rust writes: run_app with AppSource::Rust.

A TSX app names its entry, and opens in a 390×844 window by default, with hot reload, logs in the terminal and the error overlay as on the other targets:

nsh run App.tsx

In a checkout, nsh run builds the development host (nsh-linux-host) first; the distribution made on Linux ships it in runtime/linux/. Ctrl-C closes the window, and closing the window ends nsh run. The host also runs a built bundle on its own:

nsh build my-app --release -o build
dist/nsh/runtime/linux/nsh-linux-host --bundle build/bundle.nshc --window-size 1024x768   # in a checkout: target/debug/nsh-linux-host

--window-size is the window’s default size (TSX apps); tiling window managers may give the window another one.

#Packaging

nsh package --target linux [--run]           # a Rust app, from its directory
nsh package App.tsx --target linux [--run]   # a TSX app

Writes a directory and the same as an archive:

.nsh/out/<Name>/
  <Name>                 the executable: a Rust app's own runner, built with its crate; nsh's release runner for TSX
  nsh/bundle.nshc        a TSX app, compiled
  nsh/assets/…           the assets it imports (a Rust crate's `assets!()` too)
  nsh/app.json           the window title, the application id and nsh.json's window options
  <id>.desktop           a desktop entry (StartupWMClass is the id)
  <id>.png               the icon at 256×256, when nsh.json names one
  <id>.metainfo.xml      AppStream metainfo: what software centres and Flathub list
.nsh/out/<Name>.tar.gz

The executable finds nsh/ next to itself (a symbolic link to it, in ~/.local/bin say, works too). GTK 4, Pango and WebKitGTK 6.0 must be installed where the app runs. To show the app in a desktop’s launcher, put the executable (or a link) on the PATH, the .desktop file in ~/.local/share/applications and the icon in an icon theme directory. --run launches the app after packaging it.

A TSX app’s executable has only the parts of nsh/base the app imports. On x86_64, nsh package links nsh’s release runner again from its prebuilt objects (runtime/linux/nsh-linux-app.link/) and leaves out the other parts. This needs no Rust, C compiler or -dev packages: the linker is the linux-linker component, which the first package downloads (a system ld.lld is used without it). If the link fails, the app gets the prebuilt runner, with every part.

AppImage and Flatpak packages are in Packaging; releases on Flathub in Publishing.

#Embedding in a GTK app

The nsh-platform-linux crate (from the NSH repository) gives a Rust GTK app AppView: one NSH engine as a GTK widget. Each AppView is independent; several can run at once. It runs a Rust root, the root component of your crate:

use std::rc::Rc;
use nsh_platform_linux::{gtk4 as gtk, set_ui_sets, AppOptions, AppView, Root};
use gtk::prelude::*;

fn main() -> gtk::glib::ExitCode {
    // Once, before the first view: the UI sets the apps import (NSH's and your crate's).
    set_ui_sets(vec![nsh_base::registry(), my_app::registry()]);
    let app = gtk::Application::builder().application_id("com.example.host").build();
    app.connect_activate(|app| {
        let window = gtk::ApplicationWindow::builder().application(app).default_width(390).default_height(844).build();
        let view = AppView::new(AppOptions::default());
        // register native components here, before loading
        view.load(Root::of::<my_app::App>().into());
        window.set_child(Some(&view.widget()));
        // The window keeps the view; closing it stops the app.
        let keep = std::cell::RefCell::new(Some(Rc::clone(&view)));
        window.connect_destroy(move |_| if let Some(view) = keep.take() { view.stop() });
        window.present();
    });
    app.run()
}

Root::tokens gives the root its theme and Root::app_id the id its data is kept under. A TSX bundle that nsh build wrote loads into the same view, with asset_base set to its assets directory:

use nsh_platform_linux::{AppOptions, AppView, AssetBase, Bundle};

let view = AppView::new(AppOptions {
    asset_base: Some(AssetBase::Directory("app/assets".into())),
    ..AppOptions::default()
});
let code = std::fs::read_to_string("app/bundle.js").expect("bundle");
view.load(Bundle::source(code).into());

AppView::load replaces whatever ran; stop ends the app. asset_base tells it where asset:/ URIs resolve (AssetBase::Directory or AssetBase::Http). Bytecode bundles (bundle.nshc) load with unsafe { Bundle::bytecode(bytes) }: QuickJS bytecode is trusted input, so only load it from your own app’s files. A standalone window is nsh_platform_linux::run_app(source, options), with AppSource::Rust(root) or AppSource::Bundle(bundle).

AppOptions::login_item gives the app run_app runs its login item: an XDG autostart entry, ~/.config/autostart/<app_id>.desktop, starting this executable. It is off by default and read by run_app alone, so an embedded app never launches at login; the runner of the apps nsh package builds sets it.

AppOptions::cache (or AppView::set_cache, for the apps loaded next) sets where the apps keep their caches, the images they download, and how much each keeps (CacheOptions: the system’s cache directory and 256 MiB of images by default).

AppView::set_locale(Some(locale)) and AppView::set_font_scale(Some(scale)) give the app your app’s language and text size in place of the system’s (None follows the system’s again): locale(), Intl, fontScale() and text follow at once (Device and environment).

AppView::set_theme_overrides(Some(json)) puts your app’s brand over the app’s theme: a theme file in the format of nsh.json’s "theme", whose tokens win over the app’s of the same names in light and dark mode while the app’s others stay; classes such as bg-primary restyle at once, and apps loaded later start with it (None gives the app its own theme back; a file that does not parse is an error and changes nothing).

#Several windows sharing data

A Rust host opens as many windows as it likes, an AppView in each, and shares its data between them as any Rust program does: one process, an Arc the roots’ props carry. There is nothing to send between the windows; each root reads the shared value and listens for its changes the way it listens to any other source (a channel, a watch, a callback the store calls):

use std::sync::{Arc, Mutex};
use nsh_platform_linux::{gtk4 as gtk, AppOptions, AppView, Root};
use gtk::prelude::*;

#[derive(Default)]
struct Store { count: Mutex<u32> }

fn open(app: &gtk::Application, title: &str, root: Root) {
    let window = gtk::ApplicationWindow::builder().application(app).title(title).build();
    let view = AppView::new(AppOptions::default());
    view.load(root.into());
    window.set_child(Some(&view.widget()));
    let keep = std::cell::RefCell::new(Some(view));
    window.connect_destroy(move |_| if let Some(view) = keep.take() { view.stop() });
    window.present();
}

app.connect_activate(|app| {
    let store = Arc::new(Store::default());
    let main = Arc::clone(&store);
    open(app, "Main", Root::new::<my_app::Main>(move || my_app::MainProps { store: Arc::clone(&main) }));
    let chat = Arc::clone(&store);
    open(app, "Chat", Root::new::<my_app::Chat>(move || my_app::ChatProps { store: Arc::clone(&chat) }));
});

A TSX app opens its windows with <Window> and Window.open: they run in the app’s one JS runtime, so they share its signals and modules directly. Dialogs, panels and popups are transient for their parent window, and the window manager places every window.

Native components are registered with AppView::register_component; see Native components. set_ui_sets(sets) sets the UI sets every engine serves: NSH’s (nsh_base::registry()) and your crates’ (my_app::registry(), which #[native_module] makes), so the TSX a view loads imports your Rust components and modules too.

#Presenting from Rust

Rust code that shows system UI (a popover, a dialog, a file chooser) shows it from the surface whose code asked. In a main-thread task (run_on_main_thread), nsh_platform_linux::presenting_window() is the window to make a dialog transient for: the surface’s visible window the user is in; None when the surface shows in no visible window. native_view(node) is the GTK widget of an element of that surface (the node of its ref, a NodeRef argument of a module function; ROOT_ID for the surface’s root widget), in whatever window of the surface it is: the parent of a popover that belongs to the element (unparent the popover once it closes, as NSH’s own menus do). Take both while the task runs. They are for anchoring and presenting system UI only: a module never changes, re-parents, restyles or removes a widget NSH owns.

#What differs on Linux

The environment is a desktop’s: the safe area and the keyboard height are 0, idiom() is the desktop (platform.idiom is "desktop"), os_version() (platform.version) is the kernel’s major and minor version, and dark mode follows the desktop’s setting (through the XDG settings portal) live. See Device and environment.

#Keyboard

Keys Do
Return In an Input, what submitBehavior says: submit (onSubmit), submit and leave the field, or a new line (multiline inputs)
Shift+Return, Alt+Return A new line in a multiline input whose return submits (Ctrl+Return submits)
Tab, Shift+Tab The next / previous field in screen order, within the newest dialog
Escape Leaves the focused field; else clears a text selection, then closes the newest dialog or goes back
Alt+Left, Back key, mouse back button Go back (also from a field)
Ctrl+arrows, Home / End, with Shift; Ctrl+Backspace / Delete Move, select and delete by word and line in a field, as in every GTK app
Ctrl+Z, Ctrl+Shift+Z, Ctrl+X / C / V / A Undo, redo, cut, copy, paste, select all (a masked field refuses cut and copy); Ctrl+A / Ctrl+C select and copy selectable text
Arrows; Page Up / Down, Space, Shift+Space; Home / End Scroll the scroll view around the focused view or under the mouse by a line, a page, to the start / end (not while a field has the focus)
Ctrl+W, F11, Ctrl+Q Close the window, full screen, quit (nsh package and nsh run apps)
Ctrl+R Reloads the latest build (nsh run)

The app’s own shortcuts (keymap) are a window-wide shortcut controller; Ctrl+Q and F11 cannot be bound.

Shortcuts work with any keyboard layout (on a Cyrillic or Greek one, Ctrl+C is the key that types C on a US layout). A return that commits an input method’s composition (Chinese, Japanese, …) never submits. onKeyPress reports what React Native reports: the typed text, "Enter", "Backspace", and "Escape"; pasted text is not reported.

Web views are WebKitGTK pages, drawn inside NSH’s views: sheets, dialogs, toasts and views after them draw over a page, and parents clip it and fade it. Their website data (cookies, storage, the cache) is kept per app, under ~/.local/share/<app id>/webkit and ~/.cache/<app id>/webkit. The webview capability also applies to the pages of frames: an iframe of a host your app was not granted stays empty. Escape in a page leaves it, as in a text field (unless the page uses it); Alt+Left and the mouse’s back button go back in the page while it has a history.

Not supported:

  • Video rate: GTK plays at normal speed only. onEnd only comes for videos that do not loop.
  • Zooming scroll views (zoomTo, the zoom props).
  • mixBlendMode and backdropFilter (the background color still draws). Backdrop blur is not implemented; experiments using GTK 4.22’s copy and paste nodes are not part of the renderer.
  • Input accessory views and the status bar (no desktop equivalent).
  • Accessibility: accessible grouping, custom accessibilityActions; live regions are announced instead.

#See also