Is it possible to use Swift on NixOS?

Hi, I’m a NixOS user and am interested in trying out Swift.

NixOS is an unconventional Linux distribution which makes running most software not designed for it more difficult. This is because NixOS actively prevents programs from looking up dependencies in well-known locations.

This comes from the goal of the Nix package manager to support reproducible builds and to isolate all dependencies. It uses this reproducibility to transparently substitute remote builds such as from a Hydra build farm (the Nix project runs such a build farm that builds all open-source packages in their default configuration).

To do this, NixOS stores all packages in the Nix store under a directory named by the hash of the build instructions of the package. A package can only refer to another package if it knows its hash, and when the hash is used in the build instructions written in the Nix language, it is considered for the hash of the dependent package as well.

NixOS enforces this isolation for all packages and binaries. In particular, it even prevents the dynamic loader for dynamically linked binaries from being in a well-known location, which means binary artifacts from other distributions usually don’t work at all.

Because network access can lead to unreproducible builds, Nix requires all builds with network access to provide an output checksum that is verified after build. To isolate such failures it is common practice to split up builds into parts with network access that merely fetch data and parts without that do actual compilation.

All this can be difficult for compilers and toolchains used to different environments to deal with. Often, a compiler needs to be patched or wrapped in a script that informs the compiler of the to be compiled code’s dependencies’ locations. It also means that build processes that automatically download dependencies need these dependencies to be vendored by Nix, ideally from a lockfile.

Since it can’t be expected of most languages and packages to support this currently niche distribution and build model, often, the Nix community writes wrappers, patches, and utilities for working with certain languages and packages.

Does anybody know if such a project exists for Swift, or does anybody have any tips on how to manually get Swift to run on NixOS?

Nix already packages the Swift compiler and the Swift package manager as downloadable packages, but I have been unable to use them to compile a project. In particular, swift package resolve fails with this error:

error: 'swift-test': Invalid manifest (compiled with: ["/nix/store/xiqdlmqh19s71brh818z13f41cfx149r-swift-wrapper-5.8/bin/swiftc", "-vfsoverlay", "/tmp/nix-shell.RqhYRg/TemporaryDirectory.F9Lxwo/vfs.yaml", "-L", "/nix/store/p4639kdk7qlz8plfnlgka50fiicx5r9b-swiftpm-5.8/lib/swift/pm/ManifestAPI", "-lPackageDescription", "-Xlinker", "-rpath", "-Xlinker", "/nix/store/p4639kdk7qlz8plfnlgka50fiicx5r9b-swiftpm-5.8/lib/swift/pm/ManifestAPI", "-swift-version", "5", "-I", "/nix/store/p4639kdk7qlz8plfnlgka50fiicx5r9b-swiftpm-5.8/lib/swift/pm/ManifestAPI", "-package-description-version", "5.8.0", "/home/anselmschueler/Documents/git/localhost/swift-test/Package.swift", "-o", "/tmp/nix-shell.RqhYRg/TemporaryDirectory.BYsuHR/swift-test-manifest"])
/tmp/nix-shell.RqhYRg/TemporaryDirectory.BYsuHR/swift-test-manifest: error while loading shared libraries: libdispatch.so: cannot open shared object file: No such file or directory
1 Like

You may want to talk to @stephank on github and ask him, as he has been working a lot to get Swift running on NixOS.

1 Like