NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
16.46k stars 12.95k forks source link

Swift target (x86_64-pc-linux-gnu) and compiled output (x86_64-unknown-linux-gnu) does not align #311565

Open chuangzhu opened 1 month ago

chuangzhu commented 1 month ago

Describe the bug

Reposted from https://discourse.nixos.org/t/swift-target-x86-64-pc-linux-gnu-and-compiled-output-x86-64-unknown-linux-gnu-does-not-align/39393:

When I run swift --version, I get:

<unknown>:0: warning: glibc not found for 'x86_64-pc-linux-gnu'; C stdlib may be unavailable
Swift version 5.8 (swift-5.8-RELEASE)
Target: x86_64-pc-linux-gnu

and when trying to compile my program, get:

<unknown>:0: error: could not find module '_Concurrency' for target 'x86_64-pc-linux

Steps To Reproduce

Steps to reproduce the behavior:

  1. Follow the documentation and run nix-shell -p swift --run 'swiftc -' <<< 'print("Hello world!")'
<unknown>:0: error: could not find module '_Concurrency' for target 'x86_64-pc-linux-gnu'; found: x86_64-unknown-linux-gnu, at: /nix/store/hfbzr5nzd2h90fs7ymbxvk505bcgynsi-swift-5.8-lib/lib/swift/linux/_Concurrency.swiftmodule

Expected behavior

Swift builds an executable ./main, which, when run, prints "Hello world!".

Additional context

Adding swift to enviroment.systemPackages in NixOS configuration seems to be a workaround.

Notify maintainers

@dtzWill @trepetti @dduan @trundle @stephank

Metadata

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

[chuang@hawthorn:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.6.30, NixOS, 24.05 (Uakari), 24.05.20240507.b211b39`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.2`
 - nixpkgs: `/nix/store/f0ssy3p6898x9k880l23mhd0fl6rvrb2-source`

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

nixos-discourse commented 1 month ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/swift-target-x86-64-pc-linux-gnu-and-compiled-output-x86-64-unknown-linux-gnu-does-not-align/39393/4

chuangzhu commented 1 month ago

Cc @Samasaur1 @annalee @Ma27 @trofi for the recent changes in swiftc

trofi commented 1 month ago

Do you know when it was working last time? We can try to bisect to get the idea what change caused it.

chuangzhu commented 1 month ago

Using swift.stdenv seems to be the trick, as described in https://github.com/NixOS/nixpkgs/issues/242779#issuecomment-1734561750 (although he missed the c in swiftc):

nix-shell -E "with import <nixpkgs> {}; pkgs.mkShell.override { inherit (pkgs.swift) stdenv; } { buildInputs = [ swift swiftPackages.Foundation ]; }" --run "swiftc -" <<< 'import Foundation; print("hello")'

The libdispatch.so problem can also be worked around by adding ${swiftPackages.Dispatch}/lib to LD_LIBRARY_PATH, because sometimes it is not trivial to add import Foundation to every Swift file:

$ nix-shell -E 'with import <nixpkgs> {}; pkgs.mkShell.override { inherit (pkgs.swift) stdenv; } { buildInputs = [ swift swiftpm ]; LD_LIBRARY_PATH = "${swiftPackages.Dispatch}/lib"; }'
$ swiftc - <<< 'print("Hello")'
$ ./main
Hello