Files
SpacetimeDB/flake.nix
Kris Jenkins 5895dbd0b9 Re-enable the Nix flake on aarch64-darwin (#5173)
Re-enable the Nix flake on aarch64-darwin.

PR #3422 added the flake but bailed out on Darwin pending a fix for
"could not find native static library `rusty_v8`". With v8 now on
145.0.0 (PR #4073) the build itself works on aarch64-darwin, so this
removes the `builtins.abort` guard and fills in the real sha256 for the
v145.0.0 rusty_v8 archive on aarch64-darwin.

The underlying bug — v8's build.rs writing `librusty_v8.a` outside the
locations cargo and crane treat as authoritative — already has a known
workaround in PR #3921, but that fix only lives in
`.github/workflows/ci.yml` and so does not protect the Nix build. This
ports the equivalent guard into the flake as a `preBuild` on
`commonArgs`: if the v8 build directory exists but `librusty_v8.a` is
missing, clean and rebuild just the v8 crate. With current
nixpkgs/crane the file does in fact survive the `buildDepsOnly` →
`buildPackage` handoff on aarch64-darwin, so the guard no-ops on the
happy path; it is defence-in-depth for the next time crane or nixpkgs
shifts.

x86_64-darwin and aarch64-linux still use placeholder hashes; users on
those platforms will continue to hit the existing fail-then-paste-hash
loop documented in `librusty_v8.nix`.

# API and ABI breaking changes

None. Build-system only, no runtime change.

# Expected complexity level and risk

1.

# Testing

- [x] `nix flake check --no-build` passes on aarch64-darwin.
- [x] `nix build .#default` produces working `spacetime`,
  `spacetimedb-cli`, and `spacetimedb-standalone` binaries; all three
  report `spacetimedb tool version 2.3.0`.
- [x] `nix build .#checks.aarch64-darwin.workspace-fmt` passes.
- [x] `nix develop --command rustc --version` succeeds (the command
  originally reported failing before this change).
- [x] Confirmation from a reviewer with x86_64-linux that the change
  has not regressed the previously working platform.

Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
2026-06-29 21:21:53 +00:00

196 lines
8.5 KiB
Nix

{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
# crane is a framework for building Rust projects in Nix,
# which we prefer over alternatives because it better handles multi-crate workspaces.
crane.url = "github:ipetkov/crane";
flake-utils.url = "github:numtide/flake-utils";
# rust-overlay provides more and more recent builds of the rust toolchain
# than are available in nixpkgs.
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
};
outputs = { self, nixpkgs, crane, flake-utils, rust-overlay, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
inherit (pkgs) lib;
# Inject git commit in an env var around the build so that we can embed it in the binary
# without calling into `git` during our build.
# Note that `self.rev` is not set for builds with a dirty worktree, in which case we instead use `self.dirtyRev`.
gitCommit = if (self ? rev) then self.rev else self.dirtyRev;
# We fetch a precompiled v8 binary.
# The rusty_v8 build.rs normally tries to download v8 artifacts during compilation,
# but the Nix build sandbox doesn't give it network access.
# Instead, download the archive in a Nix-friendly way with a recorded sha.
librusty_v8 = pkgs.callPackage ./librusty_v8.nix {};
# The Rust toolchain that we actually build with.
rustStable = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
# An additional Rust toolchain we put in our devShell for rust-analyzer.
rustNightly = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.rust-analyzer);
version = (craneLib.crateNameFromCargoToml { inherit src; }).version;
craneLib = (crane.mkLib pkgs).overrideToolchain rustStable;
# We don't use craneLib.cleanCargoSource here because we have a lot of non-Rust files in our repo.
# I (pgoldman 2025-10-17) am too lazy to properly compose together source cleaners appropriately.
src = lib.cleanSource ./.;
# Arguments we'll pass to all of our derivations.
commonArgs = {
inherit src;
strictDeps = true;
# nativeBuildInputs are tools that are required to run during the build.
# Usually this is stuff like programming language interpreters for build scripts.
# In cross-compilation, these will be packages for the host machine's architecture.
nativeBuildInputs = [
pkgs.perl
pkgs.python3
pkgs.cmake
pkgs.pkg-config
];
# buildInputs are libraries that wind up in the target build.
# In cross-compilation, these will be packages for the target machine's architecture.
buildInputs = [
pkgs.openssl
];
# Add MacOS specific dependencies to either nativeBuildInputs or buildInputs with the following snippet:
# ++ lib.optionals pkgs.stdenv.isDarwin [
# pkgs.whateverPackage
# ];
# Include our precompiled V8.
RUSTY_V8_ARCHIVE = librusty_v8;
SPACETIMEDB_NIX_BUILD_GIT_COMMIT = gitCommit;
# Workaround for https://github.com/clockworklabs/SpacetimeDB/issues/3882
# (fixed for CI in https://github.com/clockworklabs/SpacetimeDB/pull/3921).
# The v8 crate's build.rs writes `librusty_v8.a` to
# `$CARGO_TARGET_DIR/<profile>/gn_out/obj/`, which is outside the
# directories cargo and crane treat as authoritative artifacts.
# Mixed cargo invocations on the same target dir — including crane's
# buildDepsOnly -> buildPackage handoff — can leave that file missing
# while cargo still believes v8 is up to date, producing
# "could not find native static library `rusty_v8`" at link time.
# If the v8 build directory exists (so v8 has been built before) but
# the static lib is gone, clean and rebuild just the v8 crate.
preBuild = ''
for profile in release debug; do
target_dir="''${CARGO_TARGET_DIR:-target}"
lib="$target_dir/$profile/gn_out/obj/librusty_v8.a"
if compgen -G "$target_dir/$profile/build/v8-*" > /dev/null \
&& [ ! -f "$lib" ]; then
echo "librusty_v8.a missing at $lib; cleaning and rebuilding v8."
cargo clean -p v8 || true
if [ "$profile" = release ]; then
cargo build --release -p v8
else
cargo build -p v8
fi
fi
done
'';
};
# Build a separate derivation containing our dependencies,
# which can be cached and shared between `-cli` and `-standalone`.
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
individualCrateArgs = commonArgs // {
inherit cargoArtifacts version;
# We disable tests since we'll run them all in the checks target
doCheck = false;
};
makeSpacetimePackage = name: craneLib.buildPackage (individualCrateArgs // {
pname = name;
cargoExtraArgs = "-p ${name}";
});
spacetimedb-cli = makeSpacetimePackage "spacetimedb-cli";
spacetimedb-standalone = makeSpacetimePackage "spacetimedb-standalone";
# I've chosen not to package spacetimedb-update, since it won't work on Nix systems anyways.
# Combine -standalone and -cli into a single derivation, with -cli named as spacetime.
# It would be nice to use `symlinkJoin` here, but our re-exec machinery to have -cli call into -standalone
# misbehaves when the two binaries are neighboring symlinks to real files in different directories.
# So we just copy them.
spacetime = pkgs.runCommand "spacetime-${version}" {} ''
mkdir -p $out/bin
cp ${spacetimedb-cli}/bin/spacetimedb-cli $out/bin/spacetime
cp ${spacetimedb-standalone}/bin/spacetimedb-standalone $out/bin/spacetimedb-standalone
'';
in
{
checks = {
inherit spacetimedb-cli spacetimedb-standalone;
workspace-clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
workspace-fmt = craneLib.cargoFmt {
inherit src;
};
workspace-test = craneLib.cargoTest (commonArgs // {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
# I (pgoldman 2025-10-17) have not figured out a sensible packaging of Unreal or of the .NET WASI SDK.
# The SDK reauth tests attempt to create files in the home directory, which the nix sandbox disallows.
cargoTestExtraArgs = "--workspace -- --skip unreal --skip csharp --skip reauth";
});
# TODO: Also run smoketests.
};
packages = {
inherit spacetimedb-cli spacetimedb-standalone spacetime;
default = spacetime;
};
devShells.default = craneLib.devShell {
checks = self.checks.${system};
inputsFrom = [ spacetimedb-standalone spacetimedb-cli ];
# Required to make jemalloc_tikv_sys build in local development, otherwise you get:
# /nix/store/0zv32kh0zb4s1v4ld6mc99vmzydj9nm9-glibc-2.40-66-dev/include/features.h:422:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
# 422 | # warning _FORTIFY_SOURCE requires compiling with optimization (-O)
# | ^~~~~~~
# In file included from /nix/store/0zv32kh0zb4s1v4ld6mc99vmzydj9nm9-glibc-2.40-66-dev/include/bits/libc-header-start.h:33,
# from /nix/store/0zv32kh0zb4s1v4ld6mc99vmzydj9nm9-glibc-2.40-66-dev/include/math.h:27,
# from include/jemalloc/internal/jemalloc_internal_decls.h:4,
# from include/jemalloc/internal/jemalloc_preamble.h:5,
# from src/pac.c:1:
CFLAGS = "-O";
packages = [
rustStable
rustNightly
pkgs.cargo-insta
];
};
}
);
}