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

  1. 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).
  2. 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)`.
  3. If you did not intend a Nix-style build, unset the variable so build.rs falls back to `git rev-parse`.
  4. 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

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


AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20). Data as JSON: /api/errors/7f7499470a88f433. Report an issue: GitHub.