1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-24 11:19:35 +01:00

fix(FreeBSD): remove null terminator from executable path

On FreeBSD, sysctl(KERN_PROC_PATHNAME) returns a null-terminated
string with pathLen including the terminator. This causes Nix to
fail during manual generation with:

  error:
         … while calling the 'concatStringsSep' builtin
           at /nix/var/nix/builds/nix-63232-402489527/source/doc/manual/generate-settings.nix:99:1:
             98| in
             99| concatStrings (attrValues (mapAttrs (showSetting prefix) settingsInfo))
               | ^
            100|

         error: input string '/nix/store/gq89cj02b5zs67cbd85vzg5cgsgnd8mj-nix-2.31.2/bin/nix␀'
                cannot be represented as Nix string because it contains null bytes

The issue occurs because generate-settings.nix reads the nix binary
path from JSON and evaluates it as a Nix string, which cannot contain
null bytes. Normal C++ string operations don't trigger this since they
handle null-terminated strings correctly.

Strip the null terminator on FreeBSD to match other platforms (Linux
uses /proc/self/exe, macOS uses _NSGetExecutablePath).

Credit: @wahjava (FreeBSD ports and Nixpkgs contributor)
This commit is contained in:
Jon Hermansen 2025-11-22 03:59:29 -05:00
parent c9fe290b30
commit 3ff8d0ece4

View file

@ -134,6 +134,11 @@ std::optional<Path> getSelfExe()
return std::nullopt; return std::nullopt;
} }
// FreeBSD's sysctl(KERN_PROC_PATHNAME) includes the null terminator in
// pathLen. Strip it to prevent Nix evaluation errors when the path is
// serialized to JSON and evaluated as a Nix string.
path.pop_back();
return Path(path.begin(), path.end()); return Path(path.begin(), path.end());
#else #else
return std::nullopt; return std::nullopt;