NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.89k stars 13.95k forks source link

stdenv improper escaped -rpath in NIX_LDFLAGS causes linking failure in flake devShell #324108

Open pfzetto opened 3 months ago

pfzetto commented 3 months ago

Describe the bug

A devshell adds -rpath $out/lib to NIX_LDFLAGS when entered using nix develop. If the file path of $out includes spaces, the second part of the path is interpreted as a new argument by a Linker, causing it to fail.

I think that this bug is caused by this function.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Create a new flake in a path that includes spaces: /tmp/Hello\ World/flake.nix and enter it using nix develop

    {
    inputs = {
        nixpkgs.url = "github:NixOS/nixpkgs";
    };
    
    outputs = { self, nixpkgs, ... }:
    let
        lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101";
        version = builtins.substring 0 8 lastModifiedDate;
    
        nixpkgsFor = system: import nixpkgs { inherit system; overlays = [ self.overlay ]; };
    in
    {
        overlay = final: prev: with final; {
            test = stdenv.mkDerivation {
                pname = "test";
                inherit version;
    
                buildScript = ''
                    make
                '';
    
                installScript = ''
                    mkdir -p $out/bin
                    cp app $out/bin
                '';
    
                nativeBuildInputs = with pkgs; [
                    clang
                    glibc
                ];
            };
        };
    
        defaultPackage."x86_64-linux" = (nixpkgsFor "x86_64-linux").test;
    };
    }
  2. build a empty c file to an object: clang -c test.c -o test.o
    int main(){
    return 0;
    }
  3. link the object: clang -o test test.o
  4. observe linker error:
    /nix/store/7v7g86ml0ri171gfcrs1d442px5bi1p3-binutils-2.41/bin/ld: cannot find World: No such file or directory
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

Expected behavior

The linker exits successfully and test is a working executable.

Notify maintainers

@Ericson2314

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.9.1, NixOS, 24.05 (Uakari), 24.05.20240524.d12251e`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.2`
 - channels(root): `"nixos-23.11"`
 - nixpkgs: `/nix/store/hp43s4p11vbq7qfw6v8w32vlfa9z9mry-source`

Add a :+1: reaction to issues you find important.

trofi commented 3 months ago

https://github.com/NixOS/nixpkgs/issues/177952 looks related? Tl;DR: spaces in paths are hard to handle on UNIX without breaking separator assumptions.

Herschenglime commented 2 weeks ago

Were you ever able to come up with a workaround? I'm trying to use a devshell in a remotely mounted directory that has commas in the name (based on how GNOME mounts the files).

trofi commented 2 weeks ago

Commas are probably even worse as -Wl, compiler option uses it as a separator to pass spaces to the linker.

pfzetto commented 1 week ago

Were you ever able to come up with a workaround? I'm trying to use a devshell in a remotely mounted directory that has commas in the name (based on how GNOME mounts the files).

Not really. I've worked around it by creating a bind-mount to a good path. A hardlink would propably also work.