clockworklabs/SpacetimeDB · error
Injected commit hash is not valid unicode: {gross:?}
Error message
Injected commit hash is not valid unicode: {gross:?} What it means
Build-time panic in the SpacetimeDB CLI's build.rs: the environment variable `SPACETIMEDB_NIX_BUILD_GIT_COMMIT` is set (marking a Nix build) but its bytes are not valid UTF-8. The flake.nix injects the git commit hash through this variable because git metadata is unavailable in the Nix sandbox; a non-UTF-8 value means the injection is corrupted, so the build aborts immediately rather than embedding a garbage hash.
Source
Thrown at crates/cli/build.rs:32
fn nix_injected_commit_hash() -> Option<String> {
use std::env::VarError;
// Our flake.nix sets this environment variable to be our git commit hash during the build.
// This is important because git metadata is otherwise not available within the nix build sandbox,
// and we don't install the git command-line tool in our build.
match std::env::var("SPACETIMEDB_NIX_BUILD_GIT_COMMIT") {
Ok(commit_sha) => {
// Var is set, we're building under Nix.
Some(commit_sha)
}
Err(VarError::NotPresent) => {
// Var is not set, we're not in Nix.
None
}
Err(VarError::NotUnicode(gross)) => {
// Var is set but is invalid unicode, something is very wrong.
panic!("Injected commit hash is not valid unicode: {gross:?}")
}
}
}
fn is_nix_build() -> bool {
nix_injected_commit_hash().is_some()
}
fn find_git_hash() -> String {
nix_injected_commit_hash().unwrap_or_else(|| {
// When we're *not* building in Nix, we can assume that git metadata is still present in the filesystem,
// and that the git command-line tool is installed.
let output = Command::new("git").args(["rev-parse", "HEAD"]).output().unwrap();
String::from_utf8(output.stdout).unwrap().trim().to_string()
})
}
fn get_manifest_dir() -> PathBuf {View on GitHub (pinned to 6dee26c6ef)
Solutions
- Inspect how the variable is produced: `printenv SPACETIMEDB_NIX_BUILD_GIT_COMMIT | xxd` — it must be pure ASCII hex (a 40/64-char commit sha).
- Fix the injection site (flake.nix / wrapper) to export a plain UTF-8 hex string, e.g. `export SPACETIMEDB_NIX_BUILD_GIT_COMMIT=$(git rev-parse HEAD)`.
- If you did not intend a Nix-style build, unset the variable so build.rs falls back to `git rev-parse`.
- Re-run the Nix build after correcting the environment.
Example fix
# before (broken: injects raw bytes) SPACETIMEDB_NIX_BUILD_GIT_COMMIT=$someBinaryRef nix build # after export SPACETIMEDB_NIX_BUILD_GIT_COMMIT="$(git rev-parse HEAD)" nix build
Defensive patterns
Strategy: validation
Validate before calling
#!/usr/bin/env bash
# Run before invoking the Nix build:
if [[ -n "${SPACETIMEDB_NIX_BUILD_GIT_COMMIT:-}" ]]; then
if ! [[ "$SPACETIMEDB_NIX_BUILD_GIT_COMMIT" =~ ^[0-9a-fA-F]{7,64}$ ]]; then
echo "SPACETIMEDB_NIX_BUILD_GIT_COMMIT is not a valid hex sha" >&2
exit 1
fi
fi Prevention
- Always derive SPACETIMEDB_NIX_BUILD_GIT_COMMIT via `git rev-parse HEAD` inside the flake/wrapper.
- Never hand-type or pipe binary data into the variable.
- Unset the variable entirely when building outside Nix.
When it happens
Trigger: Building `spacetimedb-standalone`/CLI inside Nix where `SPACETIMEDB_NIX_BUILD_GIT_COMMIT` was set from a non-UTF-8 source, e.g. a misquoted shell interpolation, a binary blob, or a wrapper script exporting the variable with stray bytes.
Common situations: Custom Nix overlays or dev shells that hand-set the variable; CI pipelines that pass the hash through a tool that mangles encoding; copy-pasted env setup exporting a truncated hash.
Related errors
- Failed to canonicalize manifest_dir path {}: {err:#?}
- Template '{}' has no git-tracked files! Check that the direc
- Failed to read_dir from template directory {}: {err:#?}
- Got error during read_dir from template directory {}: {err:#
- Failed to get file_type for template file {}: {err:#?}
AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20).
Data as JSON: /api/errors/7f7499470a88f433.
Report an issue: GitHub.