apple/swift-configuration
API package for reading configuration.
Overview
Swift Configuration defines an abstraction layer between configuration readers and providers.
Applications and libraries read configuration through a consistent API, while the actual provider is set up once at the application's entry point.
Examples
Swift Configuration allows you to combine multiple providers in a hierarchy, where values from higher-priority sources override those from lower-priority ones.
For example, if you have a default configuration in JSON:
{
"http": {
"timeout": 30
}
}And want to be able to provide an override for that using an environment variable:
# Environment variables:
HTTP_TIMEOUT=15The example code below creates the two relevant providers, and resolves them in the order you list:
let config = ConfigReader(providers: [
EnvironmentVariablesProvider(),
try await FileProvider<JSONSnapshot>(filePath: "/etc/config.json")
])
let httpTimeout = config.int(forKey: "http.timeout", default: 60)
print(httpTimeout) // prints 15The resolved configuration value is 15 from the environment variable. Without the environment variable, it would use 30 from the JSON file. If both sources are unavailable, the fallback default of 60 is returned.
Tip: More example use cases are described in example use cases, and complete working examples are available in the Examples directory.
Quick start
Add the dependency to your Package.swift:
.package(url: "https://github.com/apple/swift-configuration", from: "1.0.0")Add the library dependency to your target:
.product(name: "Configuration", package: "swift-configuration")Import and use in your code:
import Configuration
let config = ConfigReader(provider: EnvironmentVariablesProvider())
let httpTimeout = config.int(forKey: "http.timeout", default: 60)
print("The HTTP timeout is: \(httpTimeout)")Getting started guides
For more, check out the full documentation.
Package traits
This package offers additional integrations you can enable using package traits. To enable an additional trait on the package, update the package dependency:
.package(
url: "https://github.com/apple/swift-configuration",
from: "1.0.0",
+ traits: [.defaults, "YAML"]
)Available traits:
JSON(default): Adds support forFileProvider<JSONSnapshot>, aConfigProviderfor reading JSON files.Logging(opt-in): Adds support forAccessLogger, a way to emit access events into aSwiftLog.Logger.Reloading(opt-in): Adds support for auto-reloading variants of file providers, such asReloadingFileProvider<JSONSnapshot>(whenJSONis enabled),ReloadingFileProvider<YAMLSnapshot>(whenYAMLis enabled), andReloadingFileProvider<PropertyListSnapshot>(whenPropertyListis enabled).CommandLineArguments(opt-in): Adds support forCommandLineArgumentsProviderfor parsing command line arguments.YAML(opt-in): Adds support forFileProvider<YAMLSnapshot>, aConfigProviderfor reading YAML files.PropertyList(opt-in): Adds support forFileProvider<PropertyListSnapshot>, aConfigProviderfor reading property list files.
Supported platforms and minimum versions
The library is supported on Apple platforms, Linux, and Android.
| Component | macOS | Android | Linux | iOS | tvOS | watchOS | visionOS | | ------------- | ----- | ------- | ----- | --- | ---- | ------- | -------- | | Configuration | ✅ 15+ | ✅ API 28+ | ✅ | ✅ 18+ | ✅ 18+ | ✅ 11+ | ✅ 2+ |
Configuration providers
Built-in providers
The library includes comprehensive built-in provider support:
- Environment variables:
EnvironmentVariablesProvider - Command-line arguments:
CommandLineArgumentsProvider - JSON file:
FileProvider<JSONSnapshot>andReloadingFileProvider<JSONSnapshot> - YAML file:
FileProvider<YAMLSnapshot>andReloadingFileProvider<YAMLSnapshot> - Property list file:
FileProvider<PropertyListSnapshot>andReloadingFileProvider<PropertyListSnapshot> - Directory of files:
DirectoryFilesProvider - In-memory:
InMemoryProviderandMutableInMemoryProvider - Key transforming:
KeyMappingProvider
Community providers
- TOML file: mattt/swift-configuration-toml
- AWS Secrets Manager: songshift/swift-configuration-aws
You can also implement a custom ConfigProvider for specialized configuration formats and sources.
Key features
Documentation
Comprehensive documentation is hosted on the Swift Package Index.
Package Metadata
Repository: apple/swift-configuration
Homepage: https://swiftpackageindex.com/apple/swift-configuration/documentation
Stars: 779
Forks: 44
Open issues: 42
Default branch: main
Primary language: swift
License: Apache-2.0
Topics: configuration, server, swift
README: README.md