π§ Amalgam
Generate type-safe Nickel configurations from any schema source
Amalgam transforms Kubernetes CRDs, OpenAPI schemas, and other type definitions into strongly-typed Nickel configuration language, enabling type-safe infrastructure as code with automatic validation and completion.
π― Why Nickel?
Nickel is a powerful configuration language that offers:
- Gradual Typing - Mix static types with dynamic code as needed
- Contracts - Runtime validation with custom predicates
- Merging - Powerful record merging and extension
- Functions - First-class functions for abstraction
- Correctness - Designed to prevent configuration errors
Amalgam bridges the gap between existing schemas (K8s CRDs, OpenAPI) and Nickel's type system, giving you the best of both worlds: auto-generated types from authoritative sources with Nickel's powerful configuration capabilities.
β¨ Features
- π¦ Import Kubernetes CRDs - Convert CRDs to strongly-typed Nickel configurations
- π Smart Import Resolution - Automatically resolves K8s type references with proper imports
- π Package Generation - Creates organized package structures from multiple CRDs
- π Extensible Architecture - Plugin-based resolver system for adding new type mappings
- π GitHub Integration - Fetch CRDs directly from GitHub repositories
π₯ Installation
# Clone the repository
# Build with Cargo
# Install locally
π Quick Start
Import a Single CRD
# Import from a local file
# Import from a URL
Import Crossplane CRDs
# Fetch all Crossplane CRDs and generate a Nickel package
This generates a structured Nickel package:
crossplane-types/
βββ mod.ncl # Main module
βββ apiextensions.crossplane.io/
β βββ mod.ncl # Group module
β βββ v1/
β β βββ mod.ncl # Version module
β β βββ composition.ncl # Type definitions
β β βββ compositeresourcedefinition.ncl
β βββ v1beta1/
β βββ ...
βββ pkg.crossplane.io/
βββ ...
π Generated Nickel Output Example
Amalgam automatically resolves Kubernetes type references and generates clean Nickel code:
# Module: composition.apiextensions.crossplane.io
let k8s_io_v1 = import "../../k8s_io/v1/objectmeta.ncl" in
{
Composition = {
apiVersion | optional | String,
kind | optional | String,
metadata | optional | k8s_io_v1.ObjectMeta,
spec | optional | {
compositeTypeRef | {
apiVersion | String,
kind | String,
},
# ... more fields
},
},
}
π― Key Features Explained
Import Resolution
The tool intelligently detects and resolves Kubernetes type references:
- Detects:
io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta - Generates Import:
let k8s_io_v1 = import "../../k8s_io/v1/objectmeta.ncl" in - Resolves Reference:
k8s_io_v1.ObjectMeta
Extensible Resolver System
Add custom type resolvers using the plugin architecture:
Built-in resolvers:
KubernetesResolver- Handles K8s API typesLocalTypeResolver- Resolves local type references- Easy to add custom resolvers for other type systems
π» CLI Commands
Main Commands
-
import- Import types from various sourcescrd- Import from a CRD fileurl- Import from URL (GitHub, raw files)open-api- Import from OpenAPI speck8s- Import from Kubernetes cluster (planned)
-
generate- Generate code from IR -
convert- Convert between formats -
vendor- Manage vendored packages
Options
-v, --verbose- Enable verbose output-d, --debug- Enable debug output with detailed tracing
ποΈ Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β amalgam CLI β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Schema Pipeline β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
β β CRD β β OpenAPI β β Go β β Protobuf β β
β β Parser β β Parser β β AST β β Parser β β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Intermediate Representation (IR) β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Unified Type System (Algebraic Types) β β
β β - Sum Types (Enums/Unions) β β
β β - Product Types (Structs/Records) β β
β β - Contracts & Refinement Types β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Code Generation β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
β β Nickel β β Go β β CUE β β WASM β β
β βGenerator β βGenerator β βGenerator β β Module β β
β ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Project Structure
amalgam/
βββ Cargo.toml # Workspace definition
βββ crates/
β βββ amalgam-core/ # Core IR and type system
β βββ amalgam-parser/ # Schema parsers (CRD, OpenAPI)
β βββ amalgam-codegen/ # Code generators with resolver system
β βββ amalgam-cli/ # Command-line interface
βββ examples/ # Example configurations
βββ tests/ # Integration tests
π‘ Use Cases
Kubernetes Configuration Management
Generate type-safe Nickel configurations from your CRDs:
# Import your custom CRDs
# Use in Nickel configurations
& {
# Type-safe configuration with auto-completion
# ...
}
}
CrossPlane Composition
Type-safe CrossPlane compositions in Nickel with full IDE support:
let crossplane = import "crossplane-types/mod.ncl" in
let composition = crossplane.apiextensions.v1.Composition & {
metadata.name = "my-composition",
spec = {
compositeTypeRef = {
apiVersion = "example.io/v1",
kind = "XDatabase",
},
# Full type checking and validation
}
}
π οΈ Development
Building
# Build all crates
# Run tests
# Run with debug logging
Testing
The project includes comprehensive test coverage:
- Unit tests for type resolution and parsing
- Integration tests with real CRDs
- Snapshot tests for generated output
- Property-based tests for round-trip conversions
# Run all tests
# Run specific test suite
# Update snapshots
π€ Contributing
Contributions are welcome! Areas of interest:
- Additional schema parsers (Protobuf, GraphQL)
- More code generators (TypeScript, Python)
- Kubernetes cluster integration
- Enhanced type inference
- IDE plugins for generated types
π License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Why Apache 2.0?
- β Enterprise-friendly - Widely accepted in corporate environments
- β Patent protection - Includes express patent grants
- β Commercial-ready - Allows building proprietary products and services
- β Contribution clarity - Clear terms for contributions
π Acknowledgments
- Generates code for Nickel - A powerful configuration language with contracts and gradual typing
- Inspired by CUE and its approach to configuration
- Uses patterns from dhall-kubernetes