Nyx by Example

TOML Config

std/toml parses TOML configuration strings into a flat Map with dot-notation keys. TOML is the configuration format used by nyx.toml (the package manager manifest) and proxy.toml (nyx-proxy configuration).

Code

// Parsing TOML configuration strings

import "std/toml"

fn main() -> int {
    let config: String = "[server]\nhost = \"localhost\"\nport = 8080\ndebug = true\n\n[database]\npath = \"/var/data/app.db\"\nmax_connections = 50"

    let parsed: Map = toml_parse(config)

    let host: String = toml_get(parsed, "server.host", "0.0.0.0")
    print("host: " + host)

    let port: int = toml_get_int(parsed, "server.port", 3000)
    print("port: " + int_to_string(port))

    let debug: bool = toml_get_bool(parsed, "server.debug", false)
    if debug {
        print("debug mode enabled")
    }

    if toml_has(parsed, "database.path") {
        let db_path: String = toml_get(parsed, "database.path", "")
        print("db: " + db_path)
    }

    return 0
}

Output

host: localhost
port: 8080
debug mode enabled
db: /var/data/app.db

Explanation

toml_parse reads a TOML string and returns a Map<String> where section keys use dot notation: [server] + host = "localhost" becomes "server.host" → "localhost". All values are stored as strings internally.

Typed accessors — toml_get (string), toml_get_int, toml_get_bool — convert from the string representation and accept a default value if the key is missing. toml_has checks whether a key exists without reading its value.

This is the same parser used by nyx build to read nyx.toml project manifests and by nyx-proxy to read its routing configuration.

← Previous Next →

Source: examples/by-example/43-toml-config.nx