hex/nix/package.nix
Dax 92bcf45aa3
verify the hex microphone driver's signature before installing it (#2767)
* verify the Hex Microphone driver's signature before installing it

The driver install copied whatever bundle validated: the app's own copy,
or, when that was missing, scripts/build-hex-microphone-driver.sh's
output in ~/Library/Caches/hex-mic, a user-writable path. Nothing checked
the code signature, so an admin-approved install could plant a driver
from outside Hex's trust anchor into the HAL folder.

A published build (stable or dev channel) now checks the chosen bundle
with codesign --verify --deep --strict -R (src/codesign.rs) against
app/driver-requirement.txt: anchor apple generic, identifier
anomaly.hex.microphone.driver, Developer ID, and Anomaly's team
5NZ4Q7NXJ4. The check runs in the install worker before the admin prompt
and reports through DriverActionState::Failed.

dev_build::is_local_build is the gate: debug, test, HEX_LOCAL_BUILD,
HEX_TEST_BUILD, or an unmarked 0.0.0-dev checkout. It is narrower than
is_dev_build, whose dev-channel builds are published and signed with the
same Developer ID, so a dev-channel driver is verified too.

The candidate selection is a pure function: a bundle copy that is present
but broken fails the install instead of falling through to the cache, and
only local builds consider ~/Library/Caches/hex-mic at all, so an ad-hoc
or other-team signed driver still installs there.

The updater is untouched.

* read the bundled driver version without checking its signature

status() asks for the bundled driver's version on the UI thread (the macOS
host's platform poll), and the version read went through
bundled_driver_path(), which spawns codesign in a shipped build. The
version read now shares the install's candidate order and cache policy but
never verifies; only the install worker, before the admin prompt, checks
the signature.

Also says exactly what the requirement test pins (the driver's identifier
and the team ID the updater's requirement uses), and lists every kind of
local build the cache fallback covers in the feature map.

* driver install: re-check the signature as root and refuse symlinked copies

Addresses the second review of #2767.

The user-side codesign check ran before the admin prompt, so a bundle
swapped while the prompt was up was copied anyway. The requirement now
travels to osascript as its own argv item (empty for local builds) and
the root shell checks the staged copy with codesign --verify before it
replaces the driver or restarts coreaudiod. The copy refuses a symlinked
or non-directory source, drops ACLs and quarantine flags, and leaves the
staging copy root:wheel with no group or other write.

Candidate inspection uses symlink_metadata: a symlink (dangling included)
or a non-directory fails the install instead of reading as missing, and
only a real Hex.app's Contents folder counts as the running bundle. The
user home comes from app_paths.

is_local_build no longer treats a hand-stamped 0.0.0-dev-<N> build (what
scripts/release-app.sh makes, signed and notarized) as local; only
build.rs's -dev-local marker is local. is_dev_build is unchanged.

nix/package.nix's fileset now carries the two app/*-requirement.txt files
the Linux build and tests include, and the codesign helper compiles on
macOS only.
2026-11-24 16:58:01 -05:00

306 lines
9.7 KiB
Nix

{
lib,
rustPlatform,
pkg-config,
cmake,
python3,
shaderc,
vulkan-headers,
vulkan-loader,
spirv-headers,
openblas,
alsa-lib,
alsa-plugins,
pipewire,
libva,
ffmpeg-headless,
openssl,
libxkbcommon,
wayland,
fontconfig,
freetype,
curl,
util-linux,
wl-clipboard,
wtype,
makeWrapper,
autoAddDriverRunpath,
symlinkJoin,
libx11,
libxcb,
libxcursor,
libxrandr,
libxi,
mkShell,
rust-analyzer,
rustfmt,
clippy,
}:
let
buildEnv = {
OPENSSL_NO_VENDOR = "1";
BLA_VENDOR = "OpenBLAS";
};
runtimeEnv = {
LD_LIBRARY_PATH = lib.makeLibraryPath [
vulkan-loader
openblas
alsa-lib
wayland
libxkbcommon
fontconfig
# The X11 window and its XWayland overlay dlopen their libraries, so no
# DT_NEEDED entry finds them: x11rb's `dl-libxcb` loads libxcb.so.1, and
# winit's X11 backend loads libX11.so.6, libXcursor.so.1, libXi.so.6 and
# libX11-xcb.so.1 (in libx11), plus libxkbcommon-x11 (in libxkbcommon).
libxcb
libx11
libxcursor
libxi
# Linux Annotate dlopens libpipewire-0.3, libva and libva-drm. libva finds VA-API
# drivers through its built-in /run/opengl-driver path or LIBVA_DRIVERS_PATH.
pipewire
libva
];
ALSA_PLUGIN_DIR = symlinkJoin {
name = "hex-alsa-plugins";
paths = [
"${pipewire}/lib/alsa-lib"
"${alsa-plugins}/lib/alsa-lib"
];
};
};
runtimePackages = [
curl
wl-clipboard
wtype
# Annotate runs ffmpeg and ffprobe; the headless build includes libx264.
ffmpeg-headless
];
# Every Git dependency Cargo.lock pins, with the revision its output hash was
# computed from. scripts/check-nix-git-pins.py fails a pull request that bumps
# one without the other, before Nix reports a hash mismatch.
cargoGitDependencies = {
"transcribe-rs-0.3.11" = {
rev = "e37c66b6df43af51fde8dec34f98a07aaf09564c";
hash = "sha256-MJJiug3LfbKsuHcL2+LJk95+j5kk/MWzb5ihdBlHLmE=";
};
"rune-0.0.1" = {
rev = "9d4b4636f1f7e6d172053a0b6469a334c0bfc227";
hash = "sha256-h7g5m6MAQ+7SjmZjLfXbKaTvbCyBLcEGwop3oFzL4qg=";
};
"transcribe-cpp-sys-0.4.1" = {
rev = "60bed89dab84f7c1fddc3a17ad7fa8a27ddfba19";
hash = "sha256-zyvuCkvze16s9ve2FI2Q6m80h61FuBwdD4HVAc4EnUA=";
};
};
# The vendor tree names every Git crate `<name>-<version>`, so the rune
# package's directory follows its key in the table above.
runeVendor = builtins.head (
builtins.filter (name: lib.hasPrefix "rune-" name) (builtins.attrNames cargoGitDependencies)
);
in
rustPlatform.buildRustPackage (finalAttrs: {
pname = "hex";
version = (builtins.fromTOML (builtins.readFile ../Cargo.toml)).package.version;
src = lib.fileset.toSource {
root = ../.;
fileset = lib.fileset.unions [
../Cargo.toml
../Cargo.lock
../.cargo
../build.rs
../crates
../src
../tests
../native
../resources
../assets
../packaging
# The Annotate skill the CLI reads into its instructions.
../skills
# The iOS cleaner's instructions, compared by a test.
../ios/HEXMobile/TranscriptCleanup.swift
# The Labs registry's published copy: a Rust test compares it to the registry.
../site/src/labs.json
../macos/HexMicrophoneDriver/Info.plist
# The updater's and driver install's code-signature requirements, read at
# compile time by src/updater/macos.rs and src/audio/hex_microphone.rs.
../app/driver-requirement.txt
../app/update-requirement.txt
# Built in: a 2.3.1 install moves itself into this installer's layout.
../.github/release/install-linux.sh
];
};
# nixpkgs' vendor directory keeps every crate in one place, keyed by name and
# version, so it cannot hold two copies from different sources. Rune's
# `vendor/tiny-skia-path` is a path dependency inside Rune (a Git crate here)
# at 0.11.4, the same name and version as resvg's from crates.io, so this
# vendors crates.io's crates in `registry/` and every Git source's crates in
# `git/`, and points each source at its own directory: Cargo tells them apart
# by source. The script is importCargoLock's, rewritten; each fragment below
# must still be in it (the guard says which is gone), so a nixpkgs change
# fails loudly here instead of vendoring something wrong.
cargoDeps =
(rustPlatform.importCargoLock {
lockFile = ../Cargo.lock;
outputHashes = builtins.mapAttrs (_: dependency: dependency.hash) cargoGitDependencies;
}).overrideAttrs
(old: {
buildCommand =
let
# importCargoLock's script, and what this override turns it into.
fragments = [
{
what = "the single vendored-sources config";
from = ''
[source.crates-io]
replace-with = "vendored-sources"
[source.vendored-sources]
directory = "cargo-vendor-dir"'';
to = ''
[source.crates-io]
replace-with = "registry-vendor-dir"
[source.registry-vendor-dir]
directory = "cargo-vendor-dir/registry"
[source.git-vendor-dir]
directory = "cargo-vendor-dir/git"'';
}
{
what = "the one-directory crate link";
from = ''ln -s "$crate" $out/$(basename "$crate" | cut -c 34-)'';
to = ''
dest=$(basename "$crate" | cut -c 34-)
if [ -e "$crate/.cargo-config" ]; then
mkdir -p $out/git
ln -s "$crate" $out/git/$dest
else
mkdir -p $out/registry
ln -s "$crate" $out/registry/$dest
fi'';
}
{
what = "the Git source config append";
from = ''cat "$crate/.cargo-config" >> $out/.cargo/config.toml'';
to = ''sed 's/replace-with = "vendored-sources"/replace-with = "git-vendor-dir"/' "$crate/.cargo-config" >> $out/.cargo/config.toml'';
}
];
missing = builtins.filter (fragment: !(lib.hasInfix fragment.from old.buildCommand)) fragments;
in
if missing != [ ] then
throw (
"nix/package.nix: importCargoLock's vendor script changed; re-apply the split into "
+ "a registry and a Git vendor directory. Missing: "
+ lib.concatMapStringsSep ", " (fragment: fragment.what) missing
)
else
lib.replaceStrings (map (fragment: fragment.from) fragments) (map (
fragment: fragment.to
) fragments) old.buildCommand
+ "\n"
+ ''
# Rune's `crates/rune-text` loads its default fonts from
# `crates/rune/assets` through a path relative to Rune's
# workspace root (`../../../rune/assets/…`). A vendored crate
# sits in `git/` beside the rune package, not inside that
# workspace, so point that path at the rune package's own
# assets. Drop this once Rune moves the fonts into rune-text.
mkdir -p $out/git
rune_vendor=${runeVendor}
if [ ! -d "$out/git/$rune_vendor/assets" ]; then
echo "nix/package.nix: $out/git/$rune_vendor/assets is missing; rune-text's font include needs it" >&2
exit 1
fi
ln -sfn "$rune_vendor" $out/git/rune
'';
});
nativeBuildInputs = [
pkg-config
cmake
python3
shaderc
makeWrapper
autoAddDriverRunpath
];
buildInputs = [
vulkan-headers
vulkan-loader
spirv-headers
openblas
alsa-lib
openssl
libxkbcommon
wayland
fontconfig
freetype
libx11
libxcb
libxcursor
libxrandr
libxi
];
# OpenCode HTTP requests run through curl, including in the tests; the
# 2.3.1 adoption test runs the Linux installer, which needs flock.
nativeCheckInputs = [
curl
util-linux
];
dontUseCmakeConfigure = true;
env = buildEnv;
# Desktop and installed-model tests are #[ignore]; run the remaining suite.
doCheck = true;
# Hex's suite keeps process-global state (the lab switches, a model download
# in flight), while CI and `scripts/check` run nextest, which isolates each
# test in its own process. Plain `cargo test` shares one process, so let
# cargoCheckHook run it one test at a time (it sets RUST_TEST_THREADS=1).
dontUseCargoParallelTests = true;
preCheck = ''
export HOME="$TMPDIR/hex-test-home"
mkdir -p "$HOME"
'';
postInstall = ''
install -Dm644 ${../packaging/anomaly.hex.desktop} $out/share/applications/anomaly.hex.desktop
mkdir -p $out/share/icons
cp -R ${../packaging/linux/icons/hicolor} $out/share/icons/hicolor
substituteInPlace $out/share/applications/anomaly.hex.desktop \
--replace-fail '@HEX_BIN@' "$out/bin/hex"
'';
postFixup = ''
wrapProgram $out/bin/hex \
--prefix PATH : ${lib.makeBinPath runtimePackages} \
--prefix LD_LIBRARY_PATH : ${runtimeEnv.LD_LIBRARY_PATH} \
--set ALSA_PLUGIN_DIR ${runtimeEnv.ALSA_PLUGIN_DIR}
'';
passthru.devShell = mkShell {
inputsFrom = [ finalAttrs.finalPackage ];
env = buildEnv // runtimeEnv;
packages = runtimePackages ++ [
rust-analyzer
rustfmt
clippy
];
};
meta = {
description = "Local-first voice dictation (Linux beta)";
homepage = "https://anoma.ly/hex/";
license = lib.licenses.mit;
mainProgram = "hex";
platforms = [
"x86_64-linux"
"aarch64-linux"
];
};
})