1{ lib
2, pkgs
3, newScope
4, darwin
5, llvmPackages
6, llvmPackages_15
7, overrideCC
8}:
9
10let
11 swiftLlvmPackages = llvmPackages_15;
12
13 self = rec {
14
15 callPackage = newScope self;
16
17 # Current versions of Swift on Darwin require macOS SDK 10.15 at least.
18 # Re-export this so we can rely on the minimum Swift SDK elsewhere.
19 apple_sdk = pkgs.darwin.apple_sdk_11_0;
20
21 # Swift builds its own Clang for internal use. We wrap that clang with a
22 # cc-wrapper derived from the clang configured below. Because cc-wrapper
23 # applies a specific resource-root, the two versions are best matched, or
24 # we'll often run into compilation errors.
25 #
26 # The following selects the correct Clang version, matching the version
27 # used in Swift, and applies the same libc overrides as `apple_sdk.stdenv`.
28 clang = if pkgs.stdenv.isDarwin
29 then
30 swiftLlvmPackages.clang.override rec {
31 libc = apple_sdk.Libsystem;
32 bintools = pkgs.bintools.override { inherit libc; };
33 # Ensure that Swift’s internal clang uses the same libc++ and libc++abi as the
34 # default Darwin stdenv. Using the default libc++ avoids issues (such as crashes)
35 # that can happen when a Swift application dynamically links different versions
36 # of libc++ and libc++abi than libraries it links are using.
37 inherit (llvmPackages) libcxx;
38 }
39 else
40 swiftLlvmPackages.clang;
41
42 # Overrides that create a useful environment for swift packages, allowing
43 # packaging with `swiftPackages.callPackage`. These are similar to
44 # `apple_sdk_11_0.callPackage`, with our clang on top.
45 inherit (clang) bintools;
46 stdenv = overrideCC pkgs.stdenv clang;
47 darwin = pkgs.darwin.overrideScope (_: prev: {
48 inherit apple_sdk;
49 inherit (apple_sdk) Libsystem LibsystemCross libcharset libunwind objc4 configd IOKit Security;
50 CF = apple_sdk.CoreFoundation // { __attrsFailEvaluation = true; };
51 __attrsFailEvaluation = true;
52 });
53 xcodebuild = pkgs.xcbuild.override {
54 inherit (apple_sdk.frameworks) CoreServices CoreGraphics ImageIO;
55 inherit stdenv;
56 sdkVer = "10.15";
57 };
58 xcbuild = xcodebuild;
59
60 swift-unwrapped = callPackage ./compiler {
61 inherit (darwin) DarwinTools sigtool;
62 inherit (apple_sdk) MacOSX-SDK CLTools_Executables;
63 inherit (apple_sdk.frameworks) CoreServices Foundation Combine;
64 };
65
66 swiftNoSwiftDriver = callPackage ./wrapper {
67 swift = swift-unwrapped;
68 useSwiftDriver = false;
69 };
70
71 Dispatch = if stdenv.isDarwin
72 then null # part of libsystem
73 else callPackage ./libdispatch { swift = swiftNoSwiftDriver; };
74
75 Foundation = if stdenv.isDarwin
76 then apple_sdk.frameworks.Foundation
77 else callPackage ./foundation { swift = swiftNoSwiftDriver; };
78
79 # TODO: Apple distributes a binary XCTest with Xcode, but it is not part of
80 # CLTools (or SUS), so would have to figure out how to fetch it. The binary
81 # version has several extra features, like a test runner and ObjC support.
82 XCTest = callPackage ./xctest {
83 inherit (darwin) DarwinTools;
84 swift = swiftNoSwiftDriver;
85 };
86
87 swiftpm = callPackage ./swiftpm {
88 inherit (darwin) DarwinTools;
89 inherit (apple_sdk.frameworks) CryptoKit LocalAuthentication;
90 swift = swiftNoSwiftDriver;
91 };
92
93 swift-driver = callPackage ./swift-driver {
94 swift = swiftNoSwiftDriver;
95 };
96
97 swift = callPackage ./wrapper {
98 swift = swift-unwrapped;
99 };
100
101 sourcekit-lsp = callPackage ./sourcekit-lsp {
102 inherit (apple_sdk.frameworks) CryptoKit LocalAuthentication;
103 };
104
105 swift-docc = callPackage ./swift-docc {
106 inherit (apple_sdk.frameworks) CryptoKit LocalAuthentication;
107 };
108
109 swift-format = callPackage ./swift-format { };
110
111 swiftpm2nix = callPackage ./swiftpm2nix { };
112
113 };
114
115in self