Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust build support doesn't use NIX_LDFLAGS in link flags #326944

Closed
kira-bruneau opened this issue Jul 13, 2024 · 10 comments
Closed

Rust build support doesn't use NIX_LDFLAGS in link flags #326944

kira-bruneau opened this issue Jul 13, 2024 · 10 comments
Labels
0.kind: bug Something is broken 6.topic: rust significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc.

Comments

@kira-bruneau
Copy link
Contributor

Describe the bug

I was trying to use NIX_LDFLAGS instead of LD_LIBRARY_PATH in ukmm to link extra runtime libraries, but ukmm would silently fail to start: #325139 (comment). I noticed that a bunch of other packages were defining RUSTFLAGS to handle this, so I just ended up using that instead, but it was surprising to see that NIX_LDFLAGS wouldn't work.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Create a rust package with NIX_LDFLAGS set
  2. Notice that the libraries listed in NIX_LDFLAGS don't actually get linked in resulting binaries

Expected behavior

NIX_LDFLAGS should link extra libraries in rust like it does with packages built with c.

Notify maintainers

@zowoq @winterqt @figsoda

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.6, NixOS, 24.05 (Uakari), 24.05.20240623.e4509b3`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.2`
 - nixpkgs: `/nix/store/h5hhc58qp2rcv0f3d9di6569pxrpsi1j-source`
@kira-bruneau kira-bruneau added the 0.kind: bug Something is broken label Jul 13, 2024
@Qyriad Qyriad added 6.topic: rust significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc. labels Jul 23, 2024
@nbdd0121
Copy link
Contributor

I believe this is another case caused by rust-lang/rust#124129

@kira-bruneau
Copy link
Contributor Author

I don't think this is a regression, it just looks like it was something that wasn't considered when the rust builder was created.

It seems like all other packages that use the same workaround I used for ukmm were merged before rust-lang/rust#124129:

@nbdd0121
Copy link
Contributor

Cosmic ones use dlopen so they require the no-as-needed flag to have the libraries added even without any symbol reference.

What I have observed is that with Rust nightly even referenced symbols will not have rpath set properly. I guess what I see is a different bug with similar symptom.

@yrns
Copy link
Contributor

yrns commented Dec 24, 2024

FWIW, I use NIX_LDFLAGS = "-rpath ${lib.makeLibraryPath buildInputs}"; and it seems to work for Rust binaries.

@kira-bruneau
Copy link
Contributor Author

@yrns Oh weird, do you have an example? I just tried doing that with ukmm again, and it's still not working, but maybe there's some hook/other dependency that I'm missing?

Here's the derivation I tried for ukmm with NIX_LDFLAGS:

nix derivation show .#ukmm
{
  "/nix/store/509wijbz6njgpwjjy5n5m1frda1hqkrq-ukmm-0.15.0.drv": {
    "args": [
      "-e",
      "/nix/store/v6x3cs394jgqfbi0a42pam708flxaphh-default-builder.sh"
    ],
    "builder": "/nix/store/5mh7kaj2fyv8mk4sfq1brwxgc02884wi-bash-5.2p37/bin/bash",
    "env": {
      "NIX_LDFLAGS": "-rpath /nix/store/5mb70vg3kdzkyn0zqdgm4f87mdi0yi4i-libglvnd-1.7.0/lib:/nix/store/4fgxx1rc4x9vzzylyd192nkzb2ahva1p-libxkbcommon-1.7.0/lib:/nix/store/h1ydpxkw9qhjdxjpic1pdc2nirggyy6f-openssl-3.3.2/lib",
      "PKG_CONFIG_ALLOW_CROSS": "0",
      "__structuredAttrs": "",
      "buildInputs": "/nix/store/wjpikbgivasi62j96csmdf5w00kkwnf9-libglvnd-1.7.0-dev /nix/store/6dir2ya69y1lr1shf95wh8gr0d1svd90-libxkbcommon-1.7.0-dev /nix/store/pp2zf8bdgyz60ds8vcshk2603gcjgp72-openssl-3.3.2-dev",
      "builder": "/nix/store/5mh7kaj2fyv8mk4sfq1brwxgc02884wi-bash-5.2p37/bin/bash",
      "cargoBuildFeatures": "",
      "cargoBuildNoDefaultFeatures": "",
      "cargoBuildType": "release",
      "cargoCheckFeatures": "",
      "cargoCheckNoDefaultFeatures": "",
      "cargoCheckType": "release",
      "cargoDeps": "/nix/store/13nv3f5r4r6y1gijjwayc2lglls447wh-cargo-vendor-dir",
      "cargoTestFlags": "--all",
      "checkFlags": "--skip=gui::tasks::tests::remerge --skip=pack::tests::pack_mod --skip=project::tests::project_from_mod --skip=tests::read_meta --skip=unpack::tests::read_mod --skip=unpack::tests::unpack_mod --skip=unpack::tests::unzip_mod --skip=bnp::test_convert",
      "cmakeFlags": "",
      "configureFlags": "",
      "configurePhase": "runHook preConfigure\nrunHook postConfigure\n",
      "depsBuildBuild": "",
      "depsBuildBuildPropagated": "",
      "depsBuildTarget": "",
      "depsBuildTargetPropagated": "",
      "depsHostHost": "",
      "depsHostHostPropagated": "",
      "depsTargetTarget": "",
      "depsTargetTargetPropagated": "",
      "doCheck": "1",
      "doInstallCheck": "",
      "mesonFlags": "",
      "name": "ukmm-0.15.0",
      "nativeBuildInputs": "/nix/store/38cffsqqx823crf1i4bcf6zz1qz1hgpd-cmake-3.30.5 /nix/store/0d4m43yp69lrm8imxbqgl9zxjwwz52jw-pkg-config-wrapper-0.29.2 /nix/store/0nmsi40rfxgv5r2c7673c9lv42rxvpb3-wrap-gapps-hook /nix/store/vyyrhvjd30x2mkmvjy6qc25lrpsx3xh1-auditable-cargo-1.83.0 /nix/store/jaqg4a9kdp2szqnswxizn60zhbg7q22n-cargo-build-hook.sh /nix/store/m8f1hgzp2aa6rgkf82c3r2j03lvmapqc-cargo-check-hook.sh /nix/store/7p2gx5daa4vnqcl4276acl06099n83qg-cargo-install-hook.sh /nix/store/394bbrkpyr7viwp942pkr92cz7k64066-cargo-setup-hook.sh /nix/store/ss0yarz4m14gcskw73makm0glkm5aj9a-rustc-wrapper-1.83.0",
      "out": "/nix/store/mcqrl0b4py57a006mp7q2n2wcihigk8q-ukmm-0.15.0",
      "outputs": "out",
      "patches": "",
      "pname": "ukmm",
      "postUnpack": "eval \"$cargoDepsHook\"\n\nexport RUST_LOG=\n",
      "propagatedBuildInputs": "",
      "propagatedNativeBuildInputs": "",
      "src": "/nix/store/9icmhij8x8kq04pp2mis4i21098j1k1z-source",
      "stdenv": "/nix/store/d0gfdcag8bxzvg7ww4s7px4lf8sxisyx-stdenv-linux",
      "strictDeps": "1",
      "system": "x86_64-linux",
      "version": "0.15.0"
    },
    "inputDrvs": {
      "/nix/store/2qglsdxggm5033zb8nib4q07ng82yckv-cargo-check-hook.sh.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/30w5namr5bbg6zs0nfifw752s45ngd2b-auditable-cargo-1.83.0.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/5w1gxbwa8ixsvhfrgf9xilh3jg69dsd2-rustc-wrapper-1.83.0.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/asnfc1fbhcpnrhnk4kkrs5zrb5gdf5qs-stdenv-linux.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/cr5fwgmhs43bb4cn38dxq085k68iklwl-libglvnd-1.7.0.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "dev",
          "out"
        ]
      },
      "/nix/store/f90qwklsn9pgx3arlmizh60zdwx48c7l-source.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/fg5phv5vlsr5yjpzn8kx51r0pipfhh0w-cargo-setup-hook.sh.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/gcindcflcp4jway4igjy500yaccm9bxv-cmake-3.30.5.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/hgrb9180br5gxj1k3v6cm3w0s2k74yfy-cargo-install-hook.sh.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/mwf8p4j7j6zyii3khpvb7hpg0i9pxy3s-cargo-vendor-dir.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/nnpgwwk2h5b3j4zg2lafjkb4xra4gb7x-pkg-config-wrapper-0.29.2.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/nw8mwm6wb25n9dgq61klw5d3ymfz4gf4-openssl-3.3.2.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "dev",
          "out"
        ]
      },
      "/nix/store/q68v2ljr3cd4670yhssw7qf7z5nbax4n-wrap-gapps-hook.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/vlbyybhm6y33iw43l9swa9y1v13lyjvd-libxkbcommon-1.7.0.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "dev",
          "out"
        ]
      },
      "/nix/store/vrx8z3fnwbcfggqhjmj1hz4szfw59fwh-bash-5.2p37.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      },
      "/nix/store/xqpz37816780dr8syh2k40d1ickwn2cc-cargo-build-hook.sh.drv": {
        "dynamicOutputs": {},
        "outputs": [
          "out"
        ]
      }
    },
    "inputSrcs": [
      "/nix/store/v6x3cs394jgqfbi0a42pam708flxaphh-default-builder.sh"
    ],
    "name": "ukmm-0.15.0",
    "outputs": {
      "out": {
        "path": "/nix/store/mcqrl0b4py57a006mp7q2n2wcihigk8q-ukmm-0.15.0"
      }
    },
    "system": "x86_64-linux"
  }
}

@yrns
Copy link
Contributor

yrns commented Dec 26, 2024

You're right, it doesn't work. I was using dontPatchELF = true. Weirdly, it works in a dev shell when building manually.

@kira-bruneau
Copy link
Contributor Author

kira-bruneau commented Dec 27, 2024

Oh wait... I think it's because the rust build links with --as-needed by default, this works using NIX_LDFLAGS:

NIX_LDFLAGS = [
  "--push-state,--no-as-needed"
  "-lEGL"
  "-lwayland-client"
  "-lxkbcommon"
  "--pop-state"
];

I think this might be the culprit:

optional (
stdenv.hostPlatform.isLinux && !withBundledLLVM && !stdenv.targetPlatform.isFreeBSD && !useLLVM
) "--push-state --as-needed -lstdc++ --pop-state"
++
optional
(stdenv.hostPlatform.isLinux && !withBundledLLVM && !stdenv.targetPlatform.isFreeBSD && useLLVM)
"--push-state --as-needed -L${llvmPackages.libcxx}/lib -lc++ -lc++abi -lLLVM-${lib.versions.major llvmPackages.llvm.version} --pop-state"

I'm not 100% sure, but I think --push-state needs to be one arg, and as a seperate arg the --as-needed flag leaks out of the scope it was intended to be used in?

@kira-bruneau
Copy link
Contributor Author

kira-bruneau commented Dec 27, 2024

Oh sorry never mind. The NIX_LDFLAGS for rustc should only apply to rustc, not everything built with rustc. I'm still not quite sure why rust defaults to --as-needed though 🤔

@nbdd0121
Copy link
Contributor

@kira-bruneau
Copy link
Contributor Author

Oh thanks @nbdd0121! That clears it up! I'll close this then since it was just a misunderstanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: bug Something is broken 6.topic: rust significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc.
Projects
None yet
Development

No branches or pull requests

4 participants