Nyx by Example

DNS Resolve

resolve(hostname) is a built-in function that performs DNS resolution, converting a hostname to an IP address string. This is the first step in any network connection — before you can tcp_connect to a host, you need its IP.

Code

// DNS resolution — resolve hostnames to IP addresses

fn main() -> int {
    let ip: String = resolve("localhost")
    print("localhost -> " + ip)

    let nyx_ip: String = resolve("nyxlang.com")
    print("nyxlang.com -> " + nyx_ip)

    if nyx_ip.length() > 0 {
        print("resolution successful")
    } else {
        print("resolution failed")
    }

    return 0
}

Output

localhost -> 127.0.0.1
nyxlang.com -> 52.207.231.44
resolution successful

Explanation

resolve calls the system's DNS resolver (via getaddrinfo in the runtime) and returns the first IPv4 address as a dotted-decimal string. If resolution fails, it returns an empty string.

For localhost, the result typically comes from /etc/hosts rather than a DNS query. For public domains like nyxlang.com, the runtime performs a full DNS lookup through the configured nameservers.

This is a synchronous, blocking call. For high-performance servers that need non-blocking resolution, consider running the resolve in a separate thread with thread_spawn.

← Previous Next →

Source: examples/by-example/45-dns-resolve.nx