This example demonstrates how to use bufrnix to generate Swift code from Protocol Buffer definitions.
- Nix with flakes enabled
- Swift compiler (provided by the dev shell)
-
Enter the development shell:
nix develop
-
Initialize the proto structure:
bufrnix_init
-
Generate Swift code from proto files:
bufrnix
-
Build and run the Swift example:
swift build swift run
.
├── flake.nix # Nix flake configuration
├── Package.swift # Swift Package Manager configuration
├── proto/ # Protocol Buffer definitions
│ └── example/
│ └── v1/
│ └── example.proto
├── proto/gen/swift/ # Generated Swift code
└── Sources/ # Swift source code
└── SwiftExample/
└── main.swift
You can customize the Swift code generation by modifying the flake.nix file:
config = {
languages = {
swift = {
enable = true;
outputPath = "proto/gen/swift";
options = [
# Add protoc-gen-swift options here
];
};
};
};enable: Enable/disable Swift code generationoutputPath: Directory where generated Swift files will be placedoptions: Additional options to pass toprotoc-gen-swiftpackageName: Swift package name for generated code