Skip to content

stdenv.generic: Self rpath prefix cannot handle spaces in $PWD #415892

@amethys7dev

Description

@amethys7dev

Nixpkgs version

  • Unstable (25.11)

Describe the bug

The _addRpathPrefix() in stdenv setup.sh does not properly handle spaces in directory names. I'd love to help fix this but I'm not super familiar with how to escape things in Bash properly and I'd love some guidance <3

This causes build failures with anything that has a space in the directory name. I was getting very vague errors from cargo when trying to build several apps of mine. I ended up narrowing it down to the NIX_LDFLAGS environment variable, and I realized that if I take the output and export it as a bash array instead of a string, all of my problems go away.

This is the function in question:
https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh#L510-L514

_addRpathPrefix() {
    if [ "${NIX_NO_SELF_RPATH:-0}" != 1 ]; then
        export NIX_LDFLAGS="-rpath $1/lib ${NIX_LDFLAGS-}"
    fi
}

This is what my NIX_LDFLAGS looks like:

NIX_LDFLAGS=-rpath /home/v/Projects/VueFusion/Desktop App/outputs/out/lib  -L/nix/store/ky8yghlr8r6i1yj14pixk6acwxxskr53-rust-mixed/lib -L/nix/store/6nw[...]

This is the error it's causing when running cargo builds that call any linker (not just mold, rust-lld and others that grab from NIX_LDFLAGS):
mold: fatal: cannot open App/outputs/out/lib: No such file or directory

So far, I have tried a couple approaches, but I'm not entirely sure how to best do it. Maybe you could help me review these and offer some suggestions? :D

This does what it says on the tin, but the escaping doesn't make it into the environment variable. I think it may need to be double escaped, but this doesn't feel ideal as any future packages that re-assign NIX_LDFLAGS again may take out the escapes & I feel like the use of printf isn't ideal. I assume it could also be because the linker does not support shell escaped strings and just sees <space> as part of the arguments list

printf -v selfrpath "%q/lib" "$1"
export NIX_LDFLAGS="-rpath $selfrpath ${NIX_LDFLAGS-}"

These two approaches seem like they would theoretically work, but I am getting an error during the build process and I'm not entirely sure what it means or how this script is being used by other packages:
setup: line 1807: pop_var_context: head of shell_variables not a function context

export NIX_LDFLAGS="-rpath ${1@Q}/lib ${NIX_LDFLAGS-}"
export NIX_LDFLAGS="-rpath \"$1/lib\" ${NIX_LDFLAGS-}" # single quote (') does same thing

This approach works by manually executing it in bash to fix up the command, however I'm not sure if it's appropriate/intended to set NIX_LDFLAGS as a bash array in Nixpkgs as I don't know how other packages are handling it.

export NIX_LDFLAGS=("-rpath" "$1/lib" "${NIX_LDFLAGS[@]}")

I'm working to get a Nixpkgs built with this ^^ and test it but I'm waiting on terribly slow T-mobile internet for another couple hours D: - will update when it finishes

I know the nix build system was designed without spaces in mind, but seeing as NIX_LDFLAGS gets applied in most development environments and this uses $PWD, it's essentially broken for any path someone would want to use a flake in that contains a space, not just the /nix store, and it manifests in very unclear / hard to troubleshoot errors for end users. For instance, RustRover fails to build even a fresh created project in a directory with a space because of the linking errors.

Steps to reproduce

  1. Create a nix shell in a folder that has a space in the name, like so:
    flake.nix
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };
  outputs = { self, nixpkgs } @ inputs:
  let
    system = "x86_64-linux";
    pkgs = import nixpkgs { localSystem = system; };
  in {
    devShells."x86_64-linux".default = pkgs.mkShell {
      nativeBuildInputs = [];
    };
  };
}
  1. Observe that NIX_LDFLAGS is not in a state where it can be passed to other apps:
$ nix develop
$ echo $NIX_LDFLAGS
-rpath /home/v/Downloads/Test Folder - Space In Name/outputs/out/lib

Expected behaviour

NIX_LDFLAGS should ideally be an array so that packages picking it up can deal with spaces in the folders. Something interesting to note is that nix-shell, the old one, does not have this problem when making a shell.nix:

$ echo $NIX_LDFLAGS
-rpath /nix/store/gv5n1pr9c82p66r3ad56w04dz9pqbd07-nix-shell/lib

Screenshots

No response

Relevant log output

error: linking with `cc` failed: exit status: 1
  |
  = note:  "cc" "-m64" "/tmp/rustc6afGoI/symbols.o" "<3 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,libcfg_if-*,liblibc-*,liballoc-*,librustc_std_workspace_core-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/tmp/rustc6afGoI/raw-dylibs" "-B<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "-Wl,-znostart-stop-gc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/v/Projects/VueFusion/Desktop App/target/debug/build/getrandom-107550282e9aeb70/build_script_build-107550282e9aeb70" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs" "-fuse-ld=mold"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments #(--verbose does add NIX_LDFLAGS to this, I checked with `RUSTFLAGS=--verbose`)
  = note: mold: warning: unknown command line option: -znostart-stop-gc
          mold: fatal: cannot open App/outputs/out/lib: No such file or directory
          collect2: error: ld returned 1 exit status
          

error: could not compile `getrandom` (build script) due to 1 previous error

Additional context

No response

System metadata

  • system: "x86_64-linux"
  • host os: Linux 6.14.7, NixOS, 25.05 (Warbler), 25.05.20250521.cd2812d
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.28.3
  • channels(root): "nixos-25.05"
  • nixpkgs: /nix/store/4kbgcrj2lq7xhsii0gsyn401669bfm71-source

Notify maintainers


Note for maintainers: Please tag this issue in your pull request description. (i.e. Resolves #ISSUE.)

I assert that this issue is relevant for Nixpkgs

Is this issue important to you?

Add a 👍 reaction to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bugSomething is broken2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions