Using iroh Presets Like N0 for Quick Endpoint Setup
The N0 preset bundles TLS crypto providers, PKARR DNS publishing, address resolution via iroh.link, and public relay configuration into a single constructor call for instant iroh endpoint deployment.
The iroh networking library provides reusable configuration bundles called presets that eliminate boilerplate when initializing peer-to-peer endpoints. Using iroh presets like N0 for quick endpoint setup allows developers to obtain a production-ready endpoint configured for the public Number 0 infrastructure without manual TLS, DNS, or relay configuration. This approach leverages the Preset trait implementation in iroh/src/endpoint/presets.rs to apply secure defaults through the Endpoint::builder() pattern.
What the N0 Preset Configures
The N0 preset implements the Preset trait to automatically configure four critical endpoint capabilities. According to the source code in iroh/src/endpoint/presets.rs, the apply method mutates the Builder to enable the following components:
TLS Crypto Provider: Delegates to the Minimal preset (lines 81-92) to select between ring or aws-lc-rs based on enabled Cargo features, ensuring a secure cryptographic backend is initialized.
Address Publishing: Integrates a PKARR publisher that announces endpoint addresses to the default n0.computer DNS server at iroh.link (lines 98-103), enabling global addressability without static IP configuration.
DNS Resolution: Registers a DNS lookup mechanism that resolves endpoint information from iroh.link. On WASM browser targets, this uses a PKARR resolver; native platforms use standard DNS resolution (lines 106-115).
Relay Infrastructure: Sets RelayMode::Default (lines 119-120) to connect automatically to the public Number 0 relay servers, ensuring NAT traversal and connectivity behind firewalls.
Quick Endpoint Setup with the N0 Preset
Creating a fully functional endpoint requires only the preset import and a single builder call. The N0 preset encapsulates all mandatory configuration, allowing immediate binding without additional parameters.
use iroh::{Endpoint, endpoint::presets};
#[tokio::main]
async fn main() -> iroh::Result<()> {
// Apply the N0 preset and bind the endpoint
let ep = Endpoint::builder(presets::N0).bind().await?;
println!("Endpoint ID: {}", ep.id());
Ok(())
}
This code executes a three-phase initialization process:
Endpoint::builder(presets::N0)instantiates a freshBuilderstruct and passes it to the preset'sapplymethod.- The
N0preset mutates the builder to set crypto providers, DNS resolvers, address publishers, and relay modes. .bind().awaitfinalizes the configuration, opens network sockets, registers with relay services, and returns a ready-to-useEndpoint.
Customizing the N0 Preset Configuration
The builder pattern allows additive customization after applying the N0 preset. Because each builder method returns a new Builder instance, you can override specific defaults while retaining the preset's core configuration.
let ep = Endpoint::builder(presets::N0)
.secret_key(my_key) // Replace auto-generated key
.relay_mode(custom_map) // Override default relay map
.bind_addr("0.0.0.0:0".parse()?) // Specify local bind address
.bind()
.await?;
This flexibility enables production deployments to use N0 as a secure baseline while substituting specific parameters like static keys or custom relay infrastructure.
N0 Preset Architecture and Dependencies
The N0 preset relies on composition with the Minimal preset to ensure cryptographic requirements are satisfied. In iroh/src/endpoint/presets.rs, the N0 implementation first invokes Minimal.apply(builder) (lines 81-92) to configure the TLS crypto provider before adding its own DNS and relay configurations.
The Minimal preset (defined in lines 58-79) ensures that a required crypto provider is set, preventing binding errors that would otherwise occur with an empty configuration. This dependency hierarchy makes N0 a superset of Minimal with additional network infrastructure integration.
Real-World Usage in the iroh Codebase
The N0 preset appears in both example code and integration tests, demonstrating its role as the standard configuration for production workloads.
Transfer Example: The transfer example in iroh/examples/transfer.rs (lines 78-92) utilizes the Minimal preset as a foundation before layering user-specified options, illustrating the preset pattern's extensibility.
Integration Testing: The integration test suite in iroh/tests/integration.rs (lines 39-45) binds both client and server endpoints using presets::N0, validating that the default configuration achieves full stack connectivity across the public relay infrastructure.
Summary
- The
N0preset provides turn-key endpoint configuration for the iroh networking stack, bundling TLS, DNS, PKARR publishing, and relay settings into one constructor call. - Located in
iroh/src/endpoint/presets.rs, the preset implements thePresettrait to mutateBuilderinstances before binding. - Minimal setup requires only
Endpoint::builder(presets::N0).bind().awaitto obtain a production-ready endpoint. - The preset composes with
Minimalto ensure cryptographic providers are configured before adding network services. - Developers can chain additional builder methods after the preset to customize keys, relay maps, or bind addresses without losing the default security configuration.
Frequently Asked Questions
What is the difference between the N0 and Minimal presets in iroh?
The Minimal preset configures only the essential TLS crypto provider required for endpoint operation (lines 58-79 in iroh/src/endpoint/presets.rs), while N0 extends this foundation with PKARR DNS publishing, iroh.link resolution, and public relay connectivity. Use Minimal for isolated or custom network environments; use N0 for immediate integration with the public Number 0 infrastructure.
Can I use the N0 preset in WebAssembly (WASM) environments?
Yes, the N0 preset includes WASM-specific logic in its DNS resolver configuration (lines 106-115). On browser targets, it automatically substitutes the standard DNS resolver with a PKARR-based resolver compatible with WASM constraints, while maintaining the same iroh.link publishing and relay functionality.
How do I disable relay functionality when using the N0 preset?
For scenarios requiring direct connections without relay servers, iroh provides the N0DisableRelay preset as an alternative to N0. This variant applies the same TLS and DNS configuration as N0 but omits the default relay map setup. Alternatively, manually override the relay mode after applying N0 using .relay_mode(RelayMode::Disabled).
When should I use the Empty preset instead of N0?
Use the Empty preset when you require complete control over every configuration parameter and want to avoid any automatic DNS publishing, relay selection, or crypto provider detection. This is appropriate for embedded systems, private networks with custom discovery mechanisms, or when you need to minimize binary size by excluding default relay and DNS dependencies.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →