31 stable releases

new 2.2.39 Nov 20, 2024
2.2.37 Sep 30, 2024
2.2.33 Jun 13, 2024
2.2.27 Mar 20, 2024
2.1.2 Jul 12, 2022

#120 in Unix APIs

Download history 244/week @ 2024-08-04 184/week @ 2024-08-11 322/week @ 2024-08-18 201/week @ 2024-08-25 84/week @ 2024-09-01 137/week @ 2024-09-08 192/week @ 2024-09-15 236/week @ 2024-09-22 387/week @ 2024-09-29 177/week @ 2024-10-06 78/week @ 2024-10-13 238/week @ 2024-10-20 153/week @ 2024-10-27 104/week @ 2024-11-03 331/week @ 2024-11-10 367/week @ 2024-11-17

977 downloads per month
Used in 3 crates

Apache-2.0

1.5MB
45K SLoC

Declarative API for Host Network Management Nmstate is a library with an accompanying command line tool that manages host networking settings in a declarative manner. The networking state is described by a pre-defined schema. Reporting of current state and changes to it (desired state) both conform to the schema.

Nmstate is aimed to satisfy enterprise needs to manage host networking through a northbound declarative API and multi provider support on the southbound. NetworkManager acts as the main provider supported to provide persistent network configuration after reboot. Kernel mode is also provided as tech-preview to apply network configurations without NetworkManager.

The [NetworkState] and its subordinates are all implemented the serde Deserialize and Serialize, instead of building up [NetworkState] manually, you may deserialize it from file(e.g. JSON, YAML and etc).

Features

The nmstate crate has these cargo features:

  • gen_conf -- Generate offline network configures.
  • query_apply -- Query and apply network state.

By default, both features are enabled. The gen_conf feature is only supported on Linux platform. The query_apply feature is supported and tested on both Linux and MacOS.

Examples

To retrieve current network state:

use nmstate::NetworkState;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut net_state = NetworkState::new();
    // Use kernel mode
    net_state.set_kernel_only(true);
    net_state.retrieve()?;
    println!("{}", serde_yaml::to_string(&net_state)?);
    Ok(())
}

To apply network configuration(e.g. Assign static IP to eth1):

use nmstate::NetworkState;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut net_state: NetworkState = serde_yaml::from_str(
        r#"---
        interfaces:
          - name: eth1
            type: ethernet
            state: up
            mtu: 1500
            ipv4:
              address:
              - ip: 192.0.2.252
                prefix-length: 24
              - ip: 192.0.2.251
                prefix-length: 24
              dhcp: false
              enabled: true
            ipv6:
              address:
                - ip: 2001:db8:2::1
                  prefix-length: 64
                - ip: 2001:db8:1::1
                  prefix-length: 64
              autoconf: false
              dhcp: false
              enabled: true
        "#,
    )?;
    net_state.set_kernel_only(true);
    net_state.apply()?;
    Ok(())
}

Dependencies

~4–16MB
~245K SLoC