# Making `codex-tui` Work on FreeBSD

This is the longer version of what it took to make `codex-tui` work on FreeBSD.

It is written for someone who may not already know:

- what `codex-tui` is
- what FreeBSD is
- what V8 is
- what `rusty_v8` is
- how Rust projects use native libraries during builds

The short version is simple: the final packaged binary now works.

```sh
/root/codex/releases/0.118.0/codex-tui --version
# codex-tui 0.118.0
```

The longer version is that getting there took much more than one patch or one package install.

## What the Pieces Are

Before the story makes sense, it helps to define the parts in plain language.

- `codex-tui` is the terminal interface for Codex. It is the program we wanted to build and run.
- FreeBSD is the operating system where we wanted that program to work.
- Rust is the main programming language used by this project.
- V8 is Google's JavaScript engine, the same core engine used by Chromium.
- `rusty_v8` is the Rust wrapper that lets Rust code talk to V8.

That last point matters a lot.

This was not just "compile a Rust app on FreeBSD". Part of the Rust workspace depends on V8 through `rusty_v8`, which means the build also has to supply a V8 library in exactly the form that `rusty_v8` expects.

## The First Real Lesson

The first important realization was this:

The real target was not "install V8 on FreeBSD".

The real target was:

- produce the exact `rusty_v8` build inputs this Rust project expects
- for the exact pinned version used by the project
- for the exact FreeBSD machine target

That changed the whole strategy.

A generic system V8 package was never going to be enough. The Rust side wanted a very specific artifact for:

- `v8 = 146.4.0`
- `x86_64-unknown-freebsd`

So from that point forward, the job was not "get some V8 working". It was "get the exact `rusty_v8` files this workspace is wired to consume".

## Step 1: Clear the Basic Native Dependency Problems

The build did have ordinary system-level problems at the beginning.

One of the Rust crates needed DBus support, and the build used `pkg-config` to discover where the DBus development files were installed.

If you have never seen this before:

- DBus is a system messaging service commonly used on Unix-like systems
- `pkg-config` is a helper tool that tells the compiler where installed libraries and headers are

That part failed because the DBus package metadata was not available where `pkg-config` expected it.

The practical fix was the normal FreeBSD one:

```sh
pkg install dbus pkgconf
export PKG_CONFIG_PATH=/usr/local/libdata/pkgconfig
```

That got the build past the first native dependency failure, but it did not solve the real V8 problem.

## Step 2: Discover That Upstream `rusty_v8` Was the Real Blocker

After the DBus issue, the build ran into `rusty_v8`.

By default, `rusty_v8` expects a prebuilt library archive with a name like this:

```text
librusty_v8_release_x86_64-unknown-freebsd.a.gz
```

On some platforms that is just a download-and-go step.

For this FreeBSD target, it was not.

So the next decision became:

- can we obtain a matching prebuilt `rusty_v8` archive?
- if not, can we manufacture one ourselves?

At that point, building V8 from source was not the preferred steady-state plan. It was just the only plausible way to manufacture the artifact we needed.

## Step 3: Try the Source Build and Discover That It Also Was Not Ready

The obvious fallback was:

```sh
V8_FROM_SOURCE=1 cargo build --release --bin codex-tui
```

That did not just work.

The V8 and Chromium build system assumed a world where the host platform and toolchain layout were already familiar. FreeBSD was not in that comfortable path.

In plain language, the source build failed because the V8 side did not really know how to behave on this host.

The failures included:

- build logic that rejected FreeBSD as a host platform
- helper scripts that expected Chromium-managed toolchains instead of host FreeBSD tools
- missing FreeBSD-specific V8 source selection, which caused later link failures

So the source-build path had to become a local patched path before it could do anything useful.

## Step 4: Patch V8 Enough to Manufacture What Rust Needed

That is why this repo now includes:

- `rusty_v8_freebsd.patch`

This patch was not about making all of Chromium beautifully support FreeBSD. It was a practical local patch set to get the V8 build far enough to emit the artifacts the Rust side needed.

In practice, that meant:

- teaching parts of the V8/Chromium build system to tolerate FreeBSD as a host
- using the host FreeBSD tools instead of unsupported upstream assumptions
- fixing FreeBSD-specific source selection inside V8 itself

Once that was in place, the source build finally produced the important outputs:

- `librusty_v8.a`
- a generated Rust binding file

That second file is easy to overlook, but it turned out to be critical.

## Step 5: Learn That the Archive Alone Was Not Enough

At first it looked like the success condition was just:

- obtain `librusty_v8_release_x86_64-unknown-freebsd.a.gz`

But the `v8` crate also expects a generated Rust source file that defines the binding interface. Without that file, the Rust workspace still fails even if the V8 archive itself exists.

So the real reusable pair was:

```text
~/.cache/codex/rusty_v8/146.4.0/librusty_v8_release_x86_64-unknown-freebsd.a.gz
~/.cache/codex/rusty_v8/146.4.0/src_binding_release_x86_64-unknown-freebsd.rs
```

That changed the wrapper design.

The build wrapper, `build_latest.sh`, was updated so FreeBSD would prefer:

1. an explicit `RUSTY_V8_ARCHIVE`
2. a versioned local cache
3. an optional `RUSTY_V8_ARCHIVE_URL`
4. early failure if nothing usable is present

It was also updated to resolve the matching generated binding file from the same versioned cache.

This was the real operational win.

The source-build path stopped being the normal build path. It became a manufacturing step that could create artifacts for later reuse.

## Step 6: Hit the Final Linker Problem

At that point, the workspace got much farther:

- the system dependency problems were out of the way
- the local V8 source build had produced a usable archive
- the archive and binding file were cached
- the Rust workspace could consume those cached files
- most of the project compiled successfully

Then the final executable link still failed.

The linker error looked like this:

```text
relocation R_X86_64_32 cannot be used ... recompile with -fPIC
relocation R_X86_64_32S cannot be used ... recompile with -fPIC
```

In plain language, the final executable was being linked in one format, but the cached V8 static library had been built in another.

More specifically:

- FreeBSD wanted a PIE executable by default
- the cached `rusty_v8` static archive was non-PIC

So even after "solving V8", the final executable still would not link in the default way.

The practical fix was to make the final `codex-tui` link non-PIE so it matched the form of the V8 archive we actually had.

That fix is now codified in `build_latest.sh`.

Instead of using a plain final `cargo build`, the wrapper uses:

```sh
cargo rustc -p codex-tui --release --bin codex-tui -- -C link-arg=-Wl,-no-pie
```

That makes the final FreeBSD link step match the real working setup.

## What Is Reusable Now

The expensive part of the problem is now reusable.

Future FreeBSD builds for the same pinned version can reuse:

```text
~/.cache/codex/rusty_v8/146.4.0/librusty_v8_release_x86_64-unknown-freebsd.a.gz
~/.cache/codex/rusty_v8/146.4.0/src_binding_release_x86_64-unknown-freebsd.rs
```

That means later builds do not need to rebuild V8 from source just to get started.

The important rule is exact matching:

- same `v8` crate version
- same target triple

This is not a generic "one V8 library for everything" cache. It is a versioned, target-specific cache.

## What the Wrapper Now Does

At this point the wrapper carries the whole working FreeBSD path:

- apply the no-Wayland patch where needed
- optionally use the experimental patched V8 source-build path
- otherwise prefer a cached `rusty_v8` archive
- require the matching cached Rust binding file
- perform the final `codex-tui` link as non-PIE on FreeBSD

That is what makes the current packaged binary possible:

```sh
/root/codex/releases/0.118.0/codex-tui --version
# codex-tui 0.118.0
```

## The Main Takeaways

If I had to reduce the whole effort to a few practical lessons, they would be these:

- The real target was not a generic system V8 install. It was the exact `rusty_v8` artifact expected by the Rust workspace.
- On FreeBSD, building V8 from source was useful as a manufacturing step, not as the desired steady-state workflow.
- The reusable unit was not only the V8 archive. The generated Rust binding file mattered too.
- Versioned caching was the only sane way to avoid paying the V8 cost repeatedly.
- The final executable link behavior on FreeBSD mattered just as much as the V8 build itself.

## Appendix

Useful paths from the working setup:

```text
/root/codex/build_latest.sh
/root/codex/rusty_v8_freebsd.patch
/root/codex/releases/0.118.0/codex-tui
~/.cache/codex/rusty_v8/146.4.0/librusty_v8_release_x86_64-unknown-freebsd.a.gz
~/.cache/codex/rusty_v8/146.4.0/src_binding_release_x86_64-unknown-freebsd.rs
```

And the final packaged binary currently reports:

```sh
/root/codex/releases/0.118.0/codex-tui --version
# codex-tui 0.118.0
```

That is the end state. The whole effort was really about turning an unsupported one-off V8 source build into a reusable FreeBSD `rusty_v8` supply chain, then making the final executable link match that reality.
