Skip to content

This page may contain unreviewed machine generated text. Use with care.

Pages

Quick start

Two NixOS hosts joined into one mesh: keys, trust document, config and service.

This page joins 2 NixOS hosts into one mesh. Host A runs the node example/alpha at 192.0.2.1, and host B runs example/bravo at 192.0.2.2. Each dials and answers the other on UDP port 13000, holds an address of 203.0.113.0/24 on its tun and announces it. Running it as a service lists the module's options, and the configuration reference every key.

Use the host flake on each host, with flakes enabled as that section describes. Obtain the binary on each host and on a separate signing machine as getting started describes.

Keys

Each host makes its own Ed25519 key and prints its public half as padded base64 DER SubjectPublicKeyInfo:

sudo install -d -m 0700 /var/lib/ranet3
sudo openssl genpkey -algorithm ed25519 -out /var/lib/ranet3/key.pem
sudo chmod 0600 /var/lib/ranet3/key.pem
sudo openssl pkey -in /var/lib/ranet3/key.pem -pubout -outform DER | openssl base64 -A

Keep each private key on its own host. The document gives each device a separate node record and public key, as key files describes.

On the signing machine, generate a separate authority key once:

ranet3 key generate --type ed25519 --out authority.pem

Save the public key printed by this command for both hosts' auth.authority. Keep authority.pem private on the signing machine, outside the Nix store. Neither host needs the authority private key.

Trust document

On the signing machine, write document.json with the following structure. Replace ALPHA_PUBLIC_KEY and BRAVO_PUBLIC_KEY with the public keys printed on their respective hosts. Replace the example endpoint addresses with the addresses the hosts can dial:

{
  "nodes": [
    {
      "endpoints": [
        {
          "address": "192.0.2.1",
          "family": "ip4",
          "id": "0",
          "port": 13000
        }
      ],
      "key": "ALPHA_PUBLIC_KEY",
      "name": "example/alpha"
    },
    {
      "endpoints": [
        {
          "address": "192.0.2.2",
          "family": "ip4",
          "id": "0",
          "port": 13000
        }
      ],
      "key": "BRAVO_PUBLIC_KEY",
      "name": "example/bravo"
    }
  ]
}

Canonicalize it to the member order the signer requires, without whitespace or a trailing newline, then sign it once:

jq -c -j -S . document.json > document.canonical.json
ranet3 trust init document.canonical.json --key authority.pem --out trust.cose

The output path must not exist. Replace AUTHORITY_PUBLIC_KEY below with the public key printed by key generate, then verify the signed file:

ranet3 trust verify trust.cose --authority AUTHORITY_PUBLIC_KEY

Copy this same trust.cose to /var/lib/ranet3/trust.cose on both hosts. The daemon reads the signed file, not either authored JSON file. Do not run trust init separately for each host, since each invocation creates a new mesh identifier. Use successor signing when changing membership.

Each endpoint carries the address the other host dials, and a port equal to the node's own link.port, which the daemon does not check, as endpoint says.

Config and service

Host A imports the module and writes its settings:

{
  imports = [ inputs.ranet3.nixosModules.ranet3 ];

  networking.ranet3 = {
    enable = true;
    openFirewall = true;
    settings = {
      node = "example/alpha";
      overlay = [ "203.0.113.0/24" ];
      auth = {
        key = "/var/lib/ranet3/key.pem";
        trust = "/var/lib/ranet3/trust.cose";
        authority = [ "AUTHORITY_PUBLIC_KEY" ];
      };
      link = {
        port = 13000;
        endpoints = [
          {
            serial = "0";
            family = "ip4";
          }
        ];
        listen = true;
      };
      dial.all = true;
      cap = {
        table.addresses = [ "203.0.113.1/32" ];
        route.announce = [ "203.0.113.1/32" ];
      };
    };
  };
}

Replace AUTHORITY_PUBLIC_KEY with the signing authority's public key on both hosts. Host B writes the same settings with example/bravo as node and 203.0.113.2/32 in both address lists.

  • inputs.ranet3 is github:ranet3/ranet3 as an input of the host's own flake, which the host's flake binds.
  • dial.all dials every other node the trust document names, and link.listen answers the other host's dial, so each host dials and answers.
  • openFirewall lets UDP 13000 in through networking.firewall.
  • cap.table.addresses puts the node's address on its tun, and cap.route.announce announces it. Without the announcement the session still forms and carries nothing to that address, as getting started gives.
  • overlay names the prefix the mesh numbers its nodes from, as configuration describes.

In the host flake, name the configuration nixosConfigurations.alpha on host A and nixosConfigurations.bravo on host B instead of nixosConfigurations.gateway. Host A runs the first line to switch, and host B the second:

sudo nixos-rebuild switch --flake /etc/nixos#alpha
sudo nixos-rebuild switch --flake /etc/nixos#bravo

A daemon that starts before its private key or signed file exists waits for them, as restarts and the network describes. It verifies the snapshot and saves it in its own state directory as trust.store. Keep that store across restarts, and do not copy it between hosts. Trust order describes selection and refusals.

Checking it

On either host:

sudo ranet3 status
sudo ranet3 sessions
sudo ranet3 routes

status reports the held snapshot, initially at height 1. sessions lists a session with the other node, and routes lists the other node's address once its announcement arrives. Command line lists the other views.

To carry the LANs behind the 2 hosts, follow a site behind each node.