# Build the desktop app

This guide runs the Tauri desktop shell from a checkout, builds an installer,
and sets up code signing for releases. How the shell works is described in
[About the desktop shell](https://mirli.app/docs/desktop-shell/index.md).

## Before you begin

- Install Node 22.12 or later, the same as for
  [running from source](https://mirli.app/docs/install/index.md).
- Install Rust (stable) with [rustup](https://rustup.rs).
- Install your system's toolchain from the
  [Tauri 2 prerequisites](https://v2.tauri.app/start/prerequisites/):

  | System | Toolchain |
  |---|---|
  | Windows | Microsoft C++ Build Tools, and WebView2 (preinstalled on Windows 11) |
  | macOS | Xcode Command Line Tools |
  | Linux | `webkit2gtk` 4.1 and the other packages on Tauri's page |

- Run `npm install` in the repository root.

## Run the shell from a checkout

1. Build the web app and the server, then compile and open the shell:

   ```bash
   npm run desktop
   ```

   The first compile pulls the whole Tauri dependency tree and takes several
   minutes. Later ones take seconds. The window opens on the same app the
   server serves in a browser.

If a server is already running on port 17494, for example from
`npm run dev`, the shell uses it instead of starting another.

## Build an installer

1. Build the installer for the system you are on:

   ```bash
   npm run desktop:build
   ```

   This runs `scripts/desktop-stage.mjs` first, which builds the app and
   server, installs the server's production dependencies, and downloads a
   pinned Node checked against nodejs.org's SHA-256.

2. Find the installer in `src-tauri/target/release/bundle/`. With
   `--target`, it is in `src-tauri/target/<TRIPLE>/release/bundle/`.

Cross-building works only where Rust and the bundler support it. To build
for every system, push a `v*` tag: the release workflow in
`.github/workflows/release.yml` builds Windows (x64), macOS (Apple silicon
and Intel) and Linux (x64) installers on their own runners and attaches them,
with `SHA256SUMS.txt`, to a draft GitHub Release. Running the workflow by hand
builds the same installers as artifacts without touching Releases. See
[RELEASING.md](https://github.com/HamSanders/soundbox/blob/main/docs/RELEASING.md).

## Sign the installers

The release workflow signs whatever it has secrets for, and nothing else.

### macOS

You need an Apple Developer account ($99 a year).

1. In the repository's GitHub settings, add these secrets:

   | Secret | Value |
   |---|---|
   | `APPLE_CERTIFICATE` | The base64 of your exported `.p12` |
   | `APPLE_CERTIFICATE_PASSWORD` | Its password |
   | `APPLE_SIGNING_IDENTITY` | Your signing identity |
   | `APPLE_ID` | Your Apple ID |
   | `APPLE_PASSWORD` | An app-specific password |
   | `APPLE_TEAM_ID` | Your team ID |

2. Push a release tag. The build is signed with your Developer ID and
   notarized.

3. Before publishing, install the signed build and check that it starts its
   server.

A Developer ID build adds `src-tauri/tauri.signed.conf.json`, which turns on
the hardened runtime that notarization requires. Under it, Node's JIT and its
loading of `better-sqlite3` need the entitlements in
`src-tauri/Entitlements.plist` (`allow-jit`,
`allow-unsigned-executable-memory`, `disable-library-validation`), which every
build embeds.

An ad-hoc build keeps the hardened runtime off, because with it on, an ad-hoc
signature stops the bundled Node at its first JIT allocation: a `trace trap`,
and the app reports that the server did not start in time. To check which
kind a build is, inspect its Node:

```bash
codesign -dv Soundbox.app/Contents/MacOS/node
```

An ad-hoc build shows `flags=0x2(adhoc)`, without `runtime`.

### Windows

An unsigned installer works, behind the SmartScreen prompt. To remove the
prompt, get a code-signing certificate (Azure Trusted Signing is the cheapest
current option) and configure it in Tauri's `bundle.windows.signCommand`.

## What's next

- [About the desktop shell](https://mirli.app/docs/desktop-shell/index.md)
- [Desktop app](https://mirli.app/docs/desktop/index.md)
- [RELEASING.md](https://github.com/HamSanders/soundbox/blob/main/docs/RELEASING.md)
