1# Mosquitto {#module-services-mosquitto}
2
3Mosquitto is a MQTT broker often used for IoT or home automation data transport.
4
5## Quickstart {#module-services-mosquitto-quickstart}
6
7A minimal configuration for Mosquitto is
8
9```nix
10services.mosquitto = {
11 enable = true;
12 listeners = [ {
13 acl = [ "pattern readwrite #" ];
14 omitPasswordAuth = true;
15 settings.allow_anonymous = true;
16 } ];
17};
18```
19
20This will start a broker on port 1883, listening on all interfaces of the machine, allowing
21read/write access to all topics to any user without password requirements.
22
23User authentication can be configured with the `users` key of listeners. A config that gives
24full read access to a user `monitor` and restricted write access to a user `service` could look
25like
26
27```nix
28services.mosquitto = {
29 enable = true;
30 listeners = [ {
31 users = {
32 monitor = {
33 acl = [ "read #" ];
34 password = "monitor";
35 };
36 service = {
37 acl = [ "write service/#" ];
38 password = "service";
39 };
40 };
41 } ];
42};
43```
44
45TLS authentication is configured by setting TLS-related options of the listener:
46
47```nix
48services.mosquitto = {
49 enable = true;
50 listeners = [ {
51 port = 8883; # port change is not required, but helpful to avoid mistakes
52 # ...
53 settings = {
54 cafile = "/path/to/mqtt.ca.pem";
55 certfile = "/path/to/mqtt.pem";
56 keyfile = "/path/to/mqtt.key";
57 };
58 } ];
59```
60
61## Configuration {#module-services-mosquitto-config}
62
63The Mosquitto configuration has four distinct types of settings:
64the global settings of the daemon, listeners, plugins, and bridges.
65Bridges and listeners are part of the global configuration, plugins are part of listeners.
66Users of the broker are configured as parts of listeners rather than globally, allowing
67configurations in which a given user is only allowed to log in to the broker using specific
68listeners (eg to configure an admin user with full access to all topics, but restricted to
69localhost).
70
71Almost all options of Mosquitto are available for configuration at their appropriate levels, some
72as NixOS options written in camel case, the remainders under `settings` with their exact names in
73the Mosquitto config file. The exceptions are `acl_file` (which is always set according to the
74`acl` attributes of a listener and its users) and `per_listener_settings` (which is always set to
75`true`).
76
77### Password authentication {#module-services-mosquitto-config-passwords}
78
79Mosquitto can be run in two modes, with a password file or without. Each listener has its own
80password file, and different listeners may use different password files. Password file generation
81can be disabled by setting `omitPasswordAuth = true` for a listener; in this case it is necessary
82to either set `settings.allow_anonymous = true` to allow all logins, or to configure other
83authentication methods like TLS client certificates with `settings.use_identity_as_username = true`.
84
85The default is to generate a password file for each listener from the users configured to that
86listener. Users with no configured password will not be added to the password file and thus
87will not be able to use the broker.
88
89### ACL format {#module-services-mosquitto-config-acl}
90
91Every listener has a Mosquitto `acl_file` attached to it. This ACL is configured via two
92attributes of the config:
93
94 * the `acl` attribute of the listener configures pattern ACL entries and topic ACL entries
95 for anonymous users. Each entry must be prefixed with `pattern` or `topic` to distinguish
96 between these two cases.
97 * the `acl` attribute of every user configures in the listener configured the ACL for that
98 given user. Only topic ACLs are supported by Mosquitto in this setting, so no prefix is
99 required or allowed.
100
101The default ACL for a listener is empty, disallowing all accesses from all clients. To configure
102a completely open ACL, set `acl = [ "pattern readwrite #" ]` in the listener.