Modules & Imports in Nyx
Nyx features a modern, hierarchical module system designed for encapsulation, clean namespace isolation, and blazing-fast compilation.
1. Declaring a Module
Every Nyx file can declare its module name using the module keyword at the top of the file:
module auth
pub struct UserSession {
pub user_id: String,
pub token: String,
pub expires_at: i64,
}
pub fn create_session(user_id: String) -> UserSession {
UserSession {
user_id: user_id,
token: "tok_secure_session_91823".to_string(),
expires_at: 1724550000,
}
}
2. Importing Standard Library Modules
You can import modules from Nyx's rich standard library using dotted module identifiers:
import std.io
import std.vec
import std.string
import std.cloud
import std.gis.geo
import std.ml.ai_model
fn main() {
std.io.println("All standard modules imported successfully.")
}
3. Aliased Imports
To avoid naming collisions or shorten long module paths, use the as keyword:
import std.math as m
import std.sec.vault as v
fn calculate(val: f64) -> f64 {
m.sqrt(val) * 2.0
}
4. Visibility and Access Control
By default, functions, structs, and fields in Nyx are private to their declaring module. Use the pub modifier to export symbols publicly:
module ledger
// Public struct with both public and private fields
pub struct Account {
pub account_id: String,
pub balance: f64,
internal_salt: String, // Private to module 'ledger'
}
// Public constructor
pub fn open_account(id: String, initial_deposit: f64) -> Account {
Account {
account_id: id,
balance: initial_deposit,
internal_salt: "entropy_seed_99".to_string(),
}
}
// Private helper function
fn audit_transaction(id: String, amount: f64) {
// Internal logging logic
}
5. Project Structure & nyx.toml
Nyx projects are configured using a root nyx.toml manifest:
[package]
name = "enterprise-service"
version = "0.1.0"
authors = ["Engineering Team <dev@example.com>"]
edition = "2026"
[dependencies]
nyx-http = "1.2.0"
nyx-crypto = "0.9.4"
[build]
opt-level = 3
target = "native"
Standard Project Directory Layout:
my-project/
├── nyx.toml
├── src/
│ ├── main.nyx
│ ├── auth/
│ │ ├── session.nyx
│ │ └── tokens.nyx
│ └── services/
│ └── api.nyx
└── tests/
└── api_test.nyx