Mosquitto: Difference between revisions

From NixOS Wiki
imported>Onny
Add usage example
imported>Onny
Add authentication config example
Line 23: Line 23:
</syntaxHighlight>
</syntaxHighlight>


== Configuration ==
=== Password authentication ===
The following command will generate a hashed representation for the password <code>mypasswd</code>.
<syntaxHighlight lang="bash">
echo mypasswd | mkpasswd -m sha-512 -s | sed 's/$/==/'
</syntaxHighlight>
Change the Mosquitto listeners configuration by addming a user, in this example called <code>root</code> with the hased password from the command above.
<syntaxHighlight lang="nix">
mosquitto = {
  enable = true;
  listeners = [
    {
      users.root = {
        acl = [
          "readwrite #"
        ];
        hashedPassword = "$6$ww4noj7Iu2BWqfaL$FyM68Iq5mpqoQZWnB/.xbgq3e3eM2J/Fe7qGBfz52TFu9DpTflliL2oo8iK26ARZckYpZF7lLltrqENSEahgk.==";
      };
    }
  ];
};
</syntaxHighlight>


== Usage ==
== Usage ==

Revision as of 10:52, 1 April 2023

Mosquitto is an open source message broker that implements the MQTT protocol, a lightweight and popular communication method for the Internet of Things (IoT). Mosquitto supports MQTT versions 5.0, 3.1.1 and 3.1, and can run on various devices, from low power single board computers to full servers.

Setup

The following setup enables a local Mosquitto server listening on port 1883, allowing anonymous access for demonstration purpose.

mosquitto = {
  enable = true;
  listeners = [
    {
      acl = [ "pattern readwrite #" ];
      omitPasswordAuth = true;
      settings.allow_anonymous = true;
    }
  ];
};

networking.firewall = {
  enable = true;
  allowedTCPPorts = [ 1883 ];
};

Configuration

Password authentication

The following command will generate a hashed representation for the password mypasswd.

echo mypasswd | mkpasswd -m sha-512 -s | sed 's/$/==/'

Change the Mosquitto listeners configuration by addming a user, in this example called root with the hased password from the command above.

mosquitto = {
  enable = true;
  listeners = [
    {
      users.root = {
        acl = [
          "readwrite #"
        ];
        hashedPassword = "$6$ww4noj7Iu2BWqfaL$FyM68Iq5mpqoQZWnB/.xbgq3e3eM2J/Fe7qGBfz52TFu9DpTflliL2oo8iK26ARZckYpZF7lLltrqENSEahgk.==";
      };
    }
  ];
};

Usage

Testing the server is possible by running a listening comand

nix shell nixpkgs#mosquitto --command mosquitto_sub -h localhost -t test

and sending on a different shell a message which should be received by the command above

nix shell nixpkgs#mosquitto --command mosquitto_pub -h localhost -t test -m "Hello"