30 stable releases

new 2.2.38 Oct 24, 2024
2.2.35 Aug 20, 2024
2.2.33 Jun 13, 2024
2.2.27 Mar 20, 2024
2.1.2 Jul 12, 2022

#135 in Unix APIs

Download history 181/week @ 2024-07-04 84/week @ 2024-07-11 302/week @ 2024-07-18 132/week @ 2024-07-25 311/week @ 2024-08-01 172/week @ 2024-08-08 344/week @ 2024-08-15 183/week @ 2024-08-22 113/week @ 2024-08-29 151/week @ 2024-09-05 88/week @ 2024-09-12 303/week @ 2024-09-19 401/week @ 2024-09-26 155/week @ 2024-10-03 121/week @ 2024-10-10 73/week @ 2024-10-17

784 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

~5–17MB
~264K SLoC