Building the Swift toolchain from source on NixOS (flake + recipe)

Hi everyone,

I have been building the full Swift 6.5 toolchain from source on NixOS: compiler, stdlib, CxxStdlib, libdispatch, and Foundation, with zero patches against upstream. The repo is a Nix flake plus a build script that handles the whole pipeline.

NixOS does not follow the FHS (no /usr/lib, no /lib), so most of the existing build documentation does not apply directly. The interesting part of this project was figuring out how to satisfy the build system's expectations without patching the toolchain itself, just by shaping the Nix environment correctly.

Current state: the test suite passes 257/260 with llvm-lit. The three failures are llvm_link_time_opt.swift, cdecl_official_run.swift / cdecl_implementation_run.swift.

Repo is here: GitHub - lucasly-ba/swift-nixos: Build the Swift toolchain from source on NixOS: compiler, stdlib, C++ interop and Foundation · GitHub

I built this mainly to get a working environment to start contributing to swiftlang/swift; a first PR is the next step for me. Happy to answer questions.

11 Likes

Do you think you could submit this to nixpkgs, which appears to be way behind right now?

2 Likes

Yes you're right, it's pretty far behind. nixpkgs is still on swift-5.10.1-RELEASE so it's missing the whole 6.x line (6.0 up to 6.3.2), and I've been building 6.5-dev off main.

I'd like to help with this, but I should be honest about how my repo differs from what nixpkgs actually needs, because they're not really the same thing.

Mine is a nix develop dev shell, not a hermetic derivation. It builds main from source with no patches to the toolchain, but the only reason that works is that a dev shell is impure: it can shape the build environment at runtime (sysroot assembly, toolchain/SDK injection, that kind of thing). The nixpkgs derivation is basically the opposite: sandboxed, no network, pure inputs, and it already carries a bunch of patches. So porting my flake over isn't a version bump, it's rewriting all that runtime env-shaping as a proper hermetic derivation, which is a much bigger job and would probably need more patching than the zero I have now.

So I'd treat my repo more as a reference for how to get the build working on a non-FHS system without touching the toolchain, rather than something you can drop in.

That said, I'm up for taking it on, I'd just want to start from what's already there rather than reinventing it. So a couple of questions first:

  • Is anyone already working on moving nixpkgs swift to 6.x, or is there an open PR?
  • What's actually kept it on 5.10.1? Build-system churn across the 6.x releases, the patch set going stale, or just nobody having time?

I'm going to take a real run at this. Plan is to try reproducing the 6.x build inside a sandboxed derivation and report back what breaks.

2 Likes

No idea, talk to Stephan or Sam over there and see what they say.

I don't use NixOS, you'll have to ask them. I had some minimal contact with Stephan years ago, I suggest you get in touch with him on GitHub.

Thanks ! I will reach out to them

I think reckenrode is working on getting the bootstrap for 6.X working in nixpkgs and has opened a matrix channel to discuss development.

Edit: (Seems like you already had found it :laughing:)

2 Likes

Yes I have been in contact with him, im working on it right now haha thanks !

1 Like

Very excited by this. Being able to easily get a reproducible dev environment with nix really lowers the barrier to contribution for OSS. The maintenance cost is high, but hopefully worth it.

2 Likes

First bringing Swift 6.x to nixpkgs, then keeping this flake and other packages maintained ! Does Anyone know good places to share this kind of work to get more people involved in Swift on NixOS?