···11+# Mosquitto {#module-services-mosquitto}
22+33+Mosquitto is a MQTT broker often used for IoT or home automation data transport.
44+55+## Quickstart {#module-services-mosquitto-quickstart}
66+77+A minimal configuration for Mosquitto is
88+99+```nix
1010+services.mosquitto = {
1111+ enable = true;
1212+ listeners = [ {
1313+ acl = [ "pattern readwrite #" ];
1414+ omitPasswordAuth = true;
1515+ settings.allow_anonymous = true;
1616+ } ];
1717+};
1818+```
1919+2020+This will start a broker on port 1883, listening on all interfaces of the machine, allowing
2121+read/write access to all topics to any user without password requirements.
2222+2323+User authentication can be configured with the `users` key of listeners. A config that gives
2424+full read access to a user `monitor` and restricted write access to a user `service` could look
2525+like
2626+2727+```nix
2828+services.mosquitto = {
2929+ enable = true;
3030+ listeners = [ {
3131+ users = {
3232+ monitor = {
3333+ acl = [ "read #" ];
3434+ password = "monitor";
3535+ };
3636+ service = {
3737+ acl = [ "write service/#" ];
3838+ password = "service";
3939+ };
4040+ };
4141+ } ];
4242+};
4343+```
4444+4545+TLS authentication is configured by setting TLS-related options of the listener:
4646+4747+```nix
4848+services.mosquitto = {
4949+ enable = true;
5050+ listeners = [ {
5151+ port = 8883; # port change is not required, but helpful to avoid mistakes
5252+ # ...
5353+ settings = {
5454+ cafile = "/path/to/mqtt.ca.pem";
5555+ certfile = "/path/to/mqtt.pem";
5656+ keyfile = "/path/to/mqtt.key";
5757+ };
5858+ } ];
5959+```
6060+6161+## Configuration {#module-services-mosquitto-config}
6262+6363+The Mosquitto configuration has four distinct types of settings:
6464+the global settings of the daemon, listeners, plugins, and bridges.
6565+Bridges and listeners are part of the global configuration, plugins are part of listeners.
6666+Users of the broker are configured as parts of listeners rather than globally, allowing
6767+configurations in which a given user is only allowed to log in to the broker using specific
6868+listeners (eg to configure an admin user with full access to all topics, but restricted to
6969+localhost).
7070+7171+Almost all options of Mosquitto are available for configuration at their appropriate levels, some
7272+as NixOS options written in camel case, the remainders under `settings` with their exact names in
7373+the Mosquitto config file. The exceptions are `acl_file` (which is always set according to the
7474+`acl` attributes of a listener and its users) and `per_listener_settings` (which is always set to
7575+`true`).
7676+7777+### Password authentication {#module-services-mosquitto-config-passwords}
7878+7979+Mosquitto can be run in two modes, with a password file or without. Each listener has its own
8080+password file, and different listeners may use different password files. Password file generation
8181+can be disabled by setting `omitPasswordAuth = true` for a listener; in this case it is necessary
8282+to either set `settings.allow_anonymous = true` to allow all logins, or to configure other
8383+authentication methods like TLS client certificates with `settings.use_identity_as_username = true`.
8484+8585+The default is to generate a password file for each listener from the users configured to that
8686+listener. Users with no configured password will not be added to the password file and thus
8787+will not be able to use the broker.
8888+8989+### ACL format {#module-services-mosquitto-config-acl}
9090+9191+Every listener has a Mosquitto `acl_file` attached to it. This ACL is configured via two
9292+attributes of the config:
9393+9494+ * the `acl` attribute of the listener configures pattern ACL entries and topic ACL entries
9595+ for anonymous users. Each entry must be prefixed with `pattern` or `topic` to distinguish
9696+ between these two cases.
9797+ * the `acl` attribute of every user configures in the listener configured the ACL for that
9898+ given user. Only topic ACLs are supported by Mosquitto in this setting, so no prefix is
9999+ required or allowed.
100100+101101+The default ACL for a listener is empty, disallowing all accesses from all clients. To configure
102102+a completely open ACL, set `acl = [ "pattern readwrite #" ]` in the listener.
+18-3
nixos/modules/services/networking/mosquitto.nix
···257257258258 users = mkOption {
259259 type = attrsOf userOptions;
260260- example = { john = { password = "123456"; acl = [ "topic readwrite john/#" ]; }; };
260260+ example = { john = { password = "123456"; acl = [ "readwrite john/#" ]; }; };
261261 description = ''
262262 A set of users and their passwords and ACLs.
263263 '';
264264 default = {};
265265 };
266266267267+ omitPasswordAuth = mkOption {
268268+ type = bool;
269269+ description = ''
270270+ Omits password checking, allowing anyone to log in with any user name unless
271271+ other mandatory authentication methods (eg TLS client certificates) are configured.
272272+ '';
273273+ default = false;
274274+ };
275275+267276 acl = mkOption {
268277 type = listOf str;
269278 description = ''
270279 Additional ACL items to prepend to the generated ACL file.
271280 '';
281281+ example = [ "pattern read #" "topic readwrite anon/report/#" ];
272282 default = [];
273283 };
274284···294304 formatListener = idx: listener:
295305 [
296306 "listener ${toString listener.port} ${toString listener.address}"
297297- "password_file ${cfg.dataDir}/passwd-${toString idx}"
298307 "acl_file ${makeACLFile idx listener.users listener.acl}"
299308 ]
309309+ ++ optional (! listener.omitPasswordAuth) "password_file ${cfg.dataDir}/passwd-${toString idx}"
300310 ++ formatFreeform {} listener.settings
301311 ++ concatMap formatAuthPlugin listener.authPlugins;
302312···645655646656 };
647657648648- meta.maintainers = with lib.maintainers; [ pennae ];
658658+ meta = {
659659+ maintainers = with lib.maintainers; [ pennae ];
660660+ # Don't edit the docbook xml directly, edit the md and generate it:
661661+ # `pandoc mosquitto.md -t docbook --top-level-division=chapter --extract-media=media -f markdown+smart > mosquitto.xml`
662662+ doc = ./mosquitto.xml;
663663+ };
649664}
+147
nixos/modules/services/networking/mosquitto.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="module-services-mosquitto">
22+ <title>Mosquitto</title>
33+ <para>
44+ Mosquitto is a MQTT broker often used for IoT or home automation
55+ data transport.
66+ </para>
77+ <section xml:id="module-services-mosquitto-quickstart">
88+ <title>Quickstart</title>
99+ <para>
1010+ A minimal configuration for Mosquitto is
1111+ </para>
1212+ <programlisting language="bash">
1313+services.mosquitto = {
1414+ enable = true;
1515+ listeners = [ {
1616+ acl = [ "pattern readwrite #" ];
1717+ omitPasswordAuth = true;
1818+ settings.allow_anonymous = true;
1919+ } ];
2020+};
2121+</programlisting>
2222+ <para>
2323+ This will start a broker on port 1883, listening on all interfaces
2424+ of the machine, allowing read/write access to all topics to any
2525+ user without password requirements.
2626+ </para>
2727+ <para>
2828+ User authentication can be configured with the
2929+ <literal>users</literal> key of listeners. A config that gives
3030+ full read access to a user <literal>monitor</literal> and
3131+ restricted write access to a user <literal>service</literal> could
3232+ look like
3333+ </para>
3434+ <programlisting language="bash">
3535+services.mosquitto = {
3636+ enable = true;
3737+ listeners = [ {
3838+ users = {
3939+ monitor = {
4040+ acl = [ "read #" ];
4141+ password = "monitor";
4242+ };
4343+ service = {
4444+ acl = [ "write service/#" ];
4545+ password = "service";
4646+ };
4747+ };
4848+ } ];
4949+};
5050+</programlisting>
5151+ <para>
5252+ TLS authentication is configured by setting TLS-related options of
5353+ the listener:
5454+ </para>
5555+ <programlisting language="bash">
5656+services.mosquitto = {
5757+ enable = true;
5858+ listeners = [ {
5959+ port = 8883; # port change is not required, but helpful to avoid mistakes
6060+ # ...
6161+ settings = {
6262+ cafile = "/path/to/mqtt.ca.pem";
6363+ certfile = "/path/to/mqtt.pem";
6464+ keyfile = "/path/to/mqtt.key";
6565+ };
6666+ } ];
6767+</programlisting>
6868+ </section>
6969+ <section xml:id="module-services-mosquitto-config">
7070+ <title>Configuration</title>
7171+ <para>
7272+ The Mosquitto configuration has four distinct types of settings:
7373+ the global settings of the daemon, listeners, plugins, and
7474+ bridges. Bridges and listeners are part of the global
7575+ configuration, plugins are part of listeners. Users of the broker
7676+ are configured as parts of listeners rather than globally,
7777+ allowing configurations in which a given user is only allowed to
7878+ log in to the broker using specific listeners (eg to configure an
7979+ admin user with full access to all topics, but restricted to
8080+ localhost).
8181+ </para>
8282+ <para>
8383+ Almost all options of Mosquitto are available for configuration at
8484+ their appropriate levels, some as NixOS options written in camel
8585+ case, the remainders under <literal>settings</literal> with their
8686+ exact names in the Mosquitto config file. The exceptions are
8787+ <literal>acl_file</literal> (which is always set according to the
8888+ <literal>acl</literal> attributes of a listener and its users) and
8989+ <literal>per_listener_settings</literal> (which is always set to
9090+ <literal>true</literal>).
9191+ </para>
9292+ <section xml:id="module-services-mosquitto-config-passwords">
9393+ <title>Password authentication</title>
9494+ <para>
9595+ Mosquitto can be run in two modes, with a password file or
9696+ without. Each listener has its own password file, and different
9797+ listeners may use different password files. Password file
9898+ generation can be disabled by setting
9999+ <literal>omitPasswordAuth = true</literal> for a listener; in
100100+ this case it is necessary to either set
101101+ <literal>settings.allow_anonymous = true</literal> to allow all
102102+ logins, or to configure other authentication methods like TLS
103103+ client certificates with
104104+ <literal>settings.use_identity_as_username = true</literal>.
105105+ </para>
106106+ <para>
107107+ The default is to generate a password file for each listener
108108+ from the users configured to that listener. Users with no
109109+ configured password will not be added to the password file and
110110+ thus will not be able to use the broker.
111111+ </para>
112112+ </section>
113113+ <section xml:id="module-services-mosquitto-config-acl">
114114+ <title>ACL format</title>
115115+ <para>
116116+ Every listener has a Mosquitto <literal>acl_file</literal>
117117+ attached to it. This ACL is configured via two attributes of the
118118+ config:
119119+ </para>
120120+ <itemizedlist spacing="compact">
121121+ <listitem>
122122+ <para>
123123+ the <literal>acl</literal> attribute of the listener
124124+ configures pattern ACL entries and topic ACL entries for
125125+ anonymous users. Each entry must be prefixed with
126126+ <literal>pattern</literal> or <literal>topic</literal> to
127127+ distinguish between these two cases.
128128+ </para>
129129+ </listitem>
130130+ <listitem>
131131+ <para>
132132+ the <literal>acl</literal> attribute of every user
133133+ configures in the listener configured the ACL for that given
134134+ user. Only topic ACLs are supported by Mosquitto in this
135135+ setting, so no prefix is required or allowed.
136136+ </para>
137137+ </listitem>
138138+ </itemizedlist>
139139+ <para>
140140+ The default ACL for a listener is empty, disallowing all
141141+ accesses from all clients. To configure a completely open ACL,
142142+ set <literal>acl = [ "pattern readwrite #" ]</literal>
143143+ in the listener.
144144+ </para>
145145+ </section>
146146+ </section>
147147+</chapter>