I am working on a Swift Package that I plan to integrate with CI/CD later. I would like to inject environment-specific build configuration values (like API_BASE_URL, etc.) into my Swift Package at build time, and have these values accessible within the package modules.
I know Xcode projects can use .xcconfig files or Info.plist, but since this is a pure Swift Package, I am exploring alternatives that work well with SwiftPM and are CI/CD friendly.
So far, I have looked into two approaches:
Using swiftSettings with Package.swift to define compile-time flags.
Generating a BuildConfig.swift file before build time using a script.
I am leaning towards code generation since it seems more flexible, especially in CI/CD where I can inject values from environment variables.
However, I am curious if there is a more idiomatic or recommended way to handle this in SwiftPM?
Any guidance or shared experiences would be appreciated!
Love to know the proper answer to this. I am looking to incorporate a git tag + build date as well. Looks like perhaps a simple plugin should be able to do this.
I would also like to know this. I have a package that needs to expose some configuration for the app but with strict concurrency it's impossible without tying all functions to @MainActor which isn't practical. Is there a way to set a non-isolated global variable once at init which can then be accessed through the life of the application? Or a setting that can be set at compile time? I think SwiftSettings would be best but I hate to have a boolean for each state rather than set a variable to an enum value once.
It's not clear what you mean, as this thread was about injecting config values into a package's build, not concurrency. Are you saying you have a package which can't adopt Swift 6 mode and want to allow some of its types to be used from an app that can? In that case you don't really need to do anything, as you can keep compiling the package in Swift 5 mode. If you haven't adopted concurrency at all you can import the package using @preconcurrency import, otherwise we'd need more details to know what you're asking.
I'd like to be compatible with Swift 6 mode. I have worked around the warnings by throwing a nonisolated(unsafe) before the static settings variable but that doesn't seem right (though practically, as long as the settings are set once first thing during init and never changed, it shouldn't have any concurrency issues). One solution would be to set up the package with build time configuration of the project since those settings never need to change at runtime, hence being added to this question.
No, not to enable concurrency. Any custom settings to configure my swift package when being imported into different apps that may want my package to behave differently. The desire to support Swift 6 concurrency was merely what generated the need to find an alternate way to inject build time configurations for my swift package.