1# Swift {#swift} 2 3The Swift compiler is provided by the `swift` package: 4 5```sh 6# Compile and link a simple executable. 7nix-shell -p swift --run 'swiftc -' <<< 'print("Hello world!")' 8# Run it! 9./main 10``` 11 12The `swift` package also provides the `swift` command, with some caveats: 13 14- Swift Package Manager (SwiftPM) is packaged separately as `swiftpm`. If you 15 need functionality like `swift build`, `swift run`, `swift test`, you must 16 also add the `swiftpm` package to your closure. 17- On Darwin, the `swift repl` command requires an Xcode installation. This is 18 because it uses the system LLDB debugserver, which has special entitlements. 19 20## Module search paths {#ssec-swift-module-search-paths} 21 22Like other toolchains in Nixpkgs, the Swift compiler executables are wrapped 23to help Swift find your application's dependencies in the Nix store. These 24wrappers scan the `buildInputs` of your package derivation for specific 25directories where Swift modules are placed by convention, and automatically 26add those directories to the Swift compiler search paths. 27 28Swift follows different conventions depending on the platform. The wrappers 29look for the following directories: 30 31- On Darwin platforms: `lib/swift/macosx` 32 (If not targeting macOS, replace `macosx` with the Xcode platform name.) 33- On other platforms: `lib/swift/linux/x86_64` 34 (Where `linux` and `x86_64` are from lowercase `uname -sm`.) 35- For convenience, Nixpkgs also adds `lib/swift` to the search path. 36 This can save a bit of work packaging Swift modules, because many Nix builds 37 will produce output for just one target any way. 38 39## Core libraries {#ssec-swift-core-libraries} 40 41In addition to the standard library, the Swift toolchain contains some 42additional 'core libraries' that, on Apple platforms, are normally distributed 43as part of the OS or Xcode. These are packaged separately in Nixpkgs, and can 44be found (for use in `buildInputs`) as: 45 46- `swiftPackages.Dispatch` 47- `swiftPackages.Foundation` 48- `swiftPackages.XCTest` 49 50## Packaging with SwiftPM {#ssec-swift-packaging-with-swiftpm} 51 52Nixpkgs includes a small helper `swiftpm2nix` that can fetch your SwiftPM 53dependencies for you, when you need to write a Nix expression to package your 54application. 55 56The first step is to run the generator: 57 58```sh 59cd /path/to/my/project 60# Enter a Nix shell with the required tools. 61nix-shell -p swift swiftpm swiftpm2nix 62# First, make sure the workspace is up-to-date. 63swift package resolve 64# Now generate the Nix code. 65swiftpm2nix 66``` 67 68This produces some files in a directory `nix`, which will be part of your Nix 69expression. The next step is to write that expression: 70 71```nix 72{ stdenv, swift, swiftpm, swiftpm2nix, fetchFromGitHub }: 73 74let 75 # Pass the generated files to the helper. 76 generated = swiftpm2nix.helpers ./nix; 77in 78 79stdenv.mkDerivation rec { 80 pname = "myproject"; 81 version = "0.0.0"; 82 83 src = fetchFromGitHub { 84 owner = "nixos"; 85 repo = pname; 86 rev = version; 87 hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; 88 }; 89 90 # Including SwiftPM as a nativeBuildInput provides a buildPhase for you. 91 # This by default performs a release build using SwiftPM, essentially: 92 # swift build -c release 93 nativeBuildInputs = [ swift swiftpm ]; 94 95 # The helper provides a configure snippet that will prepare all dependencies 96 # in the correct place, where SwiftPM expects them. 97 configurePhase = generated.configure; 98 99 installPhase = '' 100 # This is a special function that invokes swiftpm to find the location 101 # of the binaries it produced. 102 binPath="$(swiftpmBinPath)" 103 # Now perform any installation steps. 104 mkdir -p $out/bin 105 cp $binPath/myproject $out/bin/ 106 ''; 107} 108``` 109 110### Custom build flags {#ssec-swiftpm-custom-build-flags} 111 112If you'd like to build a different configuration than `release`: 113 114```nix 115{ 116 swiftpmBuildConfig = "debug"; 117} 118``` 119 120It is also possible to provide additional flags to `swift build`: 121 122```nix 123{ 124 swiftpmFlags = [ "--disable-dead-strip" ]; 125} 126``` 127 128The default `buildPhase` already passes `-j` for parallel building. 129 130If these two customization options are insufficient, provide your own 131`buildPhase` that invokes `swift build`. 132 133### Running tests {#ssec-swiftpm-running-tests} 134 135Including `swiftpm` in your `nativeBuildInputs` also provides a default 136`checkPhase`, but it must be enabled with: 137 138```nix 139{ 140 doCheck = true; 141} 142``` 143 144This essentially runs: `swift test -c release` 145 146### Patching dependencies {#ssec-swiftpm-patching-dependencies} 147 148In some cases, it may be necessary to patch a SwiftPM dependency. SwiftPM 149dependencies are located in `.build/checkouts`, but the `swiftpm2nix` helper 150provides these as symlinks to read-only `/nix/store` paths. In order to patch 151them, we need to make them writable. 152 153A special function `swiftpmMakeMutable` is available to replace the symlink 154with a writable copy: 155 156```nix 157{ 158 configurePhase = generated.configure ++ '' 159 # Replace the dependency symlink with a writable copy. 160 swiftpmMakeMutable swift-crypto 161 # Now apply a patch. 162 patch -p1 -d .build/checkouts/swift-crypto -i ${./some-fix.patch} 163 ''; 164} 165``` 166 167## Considerations for custom build tools {#ssec-swift-considerations-for-custom-build-tools} 168 169### Linking the standard library {#ssec-swift-linking-the-standard-library} 170 171The `swift` package has a separate `lib` output containing just the Swift 172standard library, to prevent Swift applications needing a dependency on the 173full Swift compiler at run-time. Linking with the Nixpkgs Swift toolchain 174already ensures binaries correctly reference the `lib` output. 175 176Sometimes, Swift is used only to compile part of a mixed codebase, and the 177link step is manual. Custom build tools often locate the standard library 178relative to the `swift` compiler executable, and while the result will work, 179when this path ends up in the binary, it will have the Swift compiler as an 180unintended dependency. 181 182In this case, you should investigate how your build process discovers the 183standard library, and override the path. The correct path will be something 184like: `"${swift.swift.lib}/${swift.swiftModuleSubdir}"`