# Zed remote server on FreeBSD

MacOS Zed client + FreeBSD host, using the `editors/zed` port to build
`zed-remote-server`.

## Port changes

Two changes to `/usr/ports/editors/zed` are required for `zed-editor-1.22.0`.

### 1. Remove `files/patch-crates_gpui_src_text__system.rs`

Upstream 1.22.0 defines the constant unconditionally:

```rust
pub const SUBPIXEL_VARIANTS_Y: u8 = 1;
```

The patch still expected the old conditional form
(`cfg!(target_os = "windows") || cfg!(target_os = "linux")`), so its single hunk
failed and the build stopped at the patch phase. Upstream already gives FreeBSD
the value the patch asked for, so the patch is dead. Delete it. It was untracked
in the ports git tree, so no upstream commit is involved.

### 2. Add `files/patch-Cargo.toml`

```diff
--- Cargo.toml.orig
+++ Cargo.toml
@@ -1105,10 +1105,10 @@
 ui_input = { codegen-units = 1 }
 zed_actions = { codegen-units = 1 }

 [profile.release]
-debug = "limited"
-lto = "thin"
-codegen-units = 1
+debug = 0
+lto = false
+codegen-units = 16

 [profile.release.package]
 zed = { codegen-units = 16 }
```

Without this the build dies at the final links. `[profile.release]` in
`Cargo.toml` overrides every `CARGO_PROFILE_RELEASE_*` environment variable, so
the profile cannot be changed from the build command. ThinLTO plus
`linker-plugin-lto` on the `zed` and `remote_server` binaries exceeded memory on
a 32 GB host with 4 GB swap and both were killed with `signal: 9, SIGKILL`.

### Build command

```sh
cd /usr/ports/editors/zed
sudo env ALLOW_UNSUPPORTED_SYSTEM=yes WITHOUT_LTO=yes MAKE_JOBS_NUMBER=2 make package
```

- `MAKE_JOBS_NUMBER=2` is the working jobs knob. `Mk/Uses/cargo.mk:139` forces
  `CARGO_BUILD_JOBS=${MAKE_JOBS_NUMBER}`, so passing `CARGO_BUILD_JOBS` directly
  is overridden.
- `WITHOUT_LTO=yes` disables the extra `-C linker-plugin-lto` that ports adds at
  `Mk/Uses/cargo.mk:149`.
- `ALLOW_UNSUPPORTED_SYSTEM=yes` is required because the host release is past
  the ports support window.
- After a failed build, run `make clean` first. A stale `work/` tree re-applies
  patches that are already applied and fails with `7 out of 7 hunks ignored`.

Result: `/usr/ports/editors/zed/work/pkg/zed-editor-1.22.0.pkg` (112.9 MB).

Install it:

```sh
sudo pkg install /usr/ports/editors/zed/work/pkg/zed-editor-1.22.0.pkg
```

This installs `zed-remote-server-stable-1.22.0+stable` to `/usr/local/bin/`.

## Enabling the remote server

### 1. Match the versions

The client looks for `~/.zed_server/zed-remote-server-stable-<version>+stable`
on the remote host and requires `<version>` to equal its own version exactly. A
mismatch makes it download a prebuilt Linux server, which does not run on
FreeBSD.

Check the macOS client version:

```sh
/Applications/Zed.app/Contents/MacOS/zed --version
```

Install the port build whose `DISTVERSION` matches it (currently 1.20.2 is
installed, 1.22.0 is built).

### 2. Work around client-side OS detection

The client parses `uname -sm` over SSH in `crates/remote/src/transport.rs` and
only accepts `Darwin` and `Linux`. On `FreeBSD` it fails with
`Prebuilt remote servers are not yet available for "FreeBSD"` **before** it
checks whether the binary is already present. The FreeBSD port patches this in,
but an official macOS binary does not have the patch.

Shadow `uname` on the FreeBSD host so it reports Linux only for `-sm`:

```sh
mkdir -p ~/.bin
cat > ~/.bin/uname <<'EOF'
#!/bin/sh
case "$1" in
-sm) echo "Linux x86_64" ;;
*) exec /usr/bin/uname "$@" ;;
esac
EOF
chmod +x ~/.bin/uname
printf 'PATH=$HOME/.bin:$PATH\n' > ~/.ssh/environment
```

The client runs `uname` through a non-interactive shell, so shell rc files are
never sourced. `~/.ssh/environment` requires this in `/etc/ssh/sshd_config`:

```
PermitUserEnvironment yes
```

then `sudo service sshd restart`.

Check it:

```sh
ssh user@freebsd-host uname -sm
# Linux x86_64
```

### 3. Upload the binary

```sh
ssh user@freebsd-host mkdir -p ~/.zed_server
scp /usr/local/bin/zed-remote-server-stable-<version>+stable user@freebsd-host:~/.zed_server/
```

If you did not install the port on the remote host, copy it from
`work/stage/usr/local/bin/` instead.

### 4. Connect from the client

1. `ctrl-cmd-shift-o` (Remote Projects).
2. Connect New Server.
3. Enter the SSH command, for example `ssh user@freebsd-host`.

Set this for the server so a client upgrade never replaces the FreeBSD binary
with a Linux one:

```json
{ "upload_binary_over_ssh": true }
```

## Limitations

- No realtime collaboration: `webrtc-sys` has no FreeBSD build, so audio calls
  and screen sharing are disabled.
- The integrated terminal is laggy on FreeBSD.
- `uname` must keep reporting Linux until the macOS client carries FreeBSD in
  `RemoteOs`.
- Only x86_64 is known to work.

## References

- https://zed.dev/docs/remote-development
- https://zed.dev/docs/development/freebsd
- https://github.com/zed-industries/zed/issues/15309
- https://github.com/zed-industries/zed/discussions/29550
- https://github.com/zed-industries/zed/discussions/25601
