Unable to run simple Swift app compiled on NixOS

I’m seemingly unable to get a very simple Swift binary to run on NixOS (NixOS 25.11, nix (Nix) 2.31.5), even with just using the basic test at the top of this page. When running the binary, it just results in ./main: error while loading shared libraries: libdispatch.so: cannot open shared object file: No such file or directory.

I’m aware there’s an effort to package Swift 6 in progress which may fix this, but is there any way to make it work with the currently packaged version of Swift? Was this ever intended to work on NixOS, or only macOS at the moment?

Would greatly appreciate any advice.

1 Like

How exactly have you built it? Can you share any related nix expression? Ideally in a way that we can reproduce the build and fix your code after experimenting with it locally.

Also, the nixpkgs manual you linked is a third party copy and was built roughly 3 years ago. I know the single page manual is shit, but please refer to that.

Even though I have to admit, at least on first glance I do not really see a difference between 23.11pre and 26.05 manual here…

https://nixos.org/manual/nixpkgs/stable/#swift

I get the same error that @niko described when running the basic example on 26.05 in the manual you linked.

As I said, it says the same. I am still waiting for related nix expressions or a reproducer.

You want a reproducer for what exactly?

Running the nix-shell example provided in the 26.05 manual with the swift package provided by https://cache.nixos.org/ for 26.05 results in the exact same missing libdispatch error.

Now I’ll go back into my hidey hole I suppose :sweat_smile:. I don’t have any more information to provide

I was expecting some swift code and a nix expression. I understand OP that the error happens when running the built artifact.

Though your message read much more as if the error is comming from swift itself, which would be a hint for a packaging issue.

Therefore I looked a bit closer and just ran that oneliner from the manual. Indeed it produces a broken main.

Looking at the build output a bit closer, it looks as if swift doesn’t link dependencies correctly.

A quick workaround would probably be a proper devshell that sets LD_LIBRARY_PATH such that all libs are found.

Even though that might break other problems within that shell.

A less quick workaround would be a shell that provides tooling to patchelf the artifact properly.

And the most proper but by far least quick, would be to fix swiftcs linking phase.

I don’t know swift or it’s ecosystem well enough to know whether this can be done easily in a wrapper or requires deeper patching of the compiler itself.

1 Like

Known issue, it only works on macos atm. Follow Update request: swift 5.8 → 6 · Issue #343210 · NixOS/nixpkgs · GitHub for updates.

2 Likes

While the imperative example at the top of the manual does not work, properly packaged projects should work fine on NixOS so long as they do not require features from Swift versions newer than 5.10. See GitHub - Samasaur1/nix-swift-hello · GitHub for an example (and I’ll put “update the manual” on my Swift todo list).

Let me know if you run into any more issues

2 Likes

I appreciate all the help, I was able to get it working for what I need to do! Thank you all so much :>

What exactly did you do?

1 Like