···239 <link xlink:href="http://borgbackup.readthedocs.io/en/stable/usage/upgrade.html#attic-and-borg-0-xx-to-borg-1-x">here</link>.
240 </para>
241 </listitem>
242+ <listitem>
243+ <para>
244+ The Piwik analytics software was renamed to Matomo:
245+ <itemizedlist>
246+ <listitem>
247+ <para>The package <literal>pkgs.piwik</literal> was renamed to <literal>pkgs.matomo</literal>.</para>
248+ </listitem>
249+ <listitem>
250+ <para>The service <literal>services.piwik</literal> was renamed to <literal>services.matomo</literal>.</para>
251+ </listitem>
252+ <listitem>
253+ <para>
254+ The data directory <filename>/var/lib/piwik</filename> was renamed to <filename>/var/lib/matomo</filename>.
255+ All files will be moved automatically on first startup, but you might need to adjust your backup scripts.
256+ </para>
257+ </listitem>
258+ <listitem>
259+ <para>
260+ The <literal>piwik</literal> user was renamed to <literal>matomo</literal>.
261+ The service will adjust ownership automatically for files in the data directory.
262+ If you use unix socket authentication, remember to give the new <literal>matomo</literal> user
263+ access to the database and to change the <literal>username</literal> to <literal>matomo</literal>
264+ in the <literal>[database]</literal> section of <filename>/var/lib/matomo/config/config.ini.php</filename>.
265+ </para>
266+ </listitem>
267+ <listitem>
268+ <para>
269+ If you named your database `piwik`, you might want to rename it to `matomo` to keep things clean,
270+ but this is neither enforced nor required.
271+ </para>
272+ </listitem>
273+ </itemizedlist>
274+ </para>
275+ </listitem>
276</itemizedlist>
277278</section>
···1+<chapter xmlns="http://docbook.org/ns/docbook"
2+ xmlns:xlink="http://www.w3.org/1999/xlink"
3+ xmlns:xi="http://www.w3.org/2001/XInclude"
4+ version="5.0"
5+ xml:id="module-services-matomo">
6+7+ <title>Matomo</title>
8+ <para>
9+ Matomo is a real-time web analytics application.
10+ This module configures php-fpm as backend for Matomo, optionally configuring an nginx vhost as well.
11+ </para>
12+13+ <para>
14+ An automatic setup is not suported by Matomo, so you need to configure Matomo itself in the browser-based Matomo setup.
15+ </para>
16+17+18+ <section>
19+ <title>Database Setup</title>
20+21+ <para>
22+ You also need to configure a MariaDB or MySQL database and -user for Matomo yourself,
23+ and enter those credentials in your browser.
24+ You can use passwordless database authentication via the UNIX_SOCKET authentication plugin
25+ with the following SQL commands:
26+27+ <programlisting>
28+ # For MariaDB
29+ INSTALL PLUGIN unix_socket SONAME 'auth_socket';
30+ CREATE DATABASE matomo;
31+ CREATE USER 'matomo'@'localhost' IDENTIFIED WITH unix_socket;
32+ GRANT ALL PRIVILEGES ON matomo.* TO 'matomo'@'localhost';
33+34+ # For MySQL
35+ INSTALL PLUGIN auth_socket SONAME 'auth_socket.so';
36+ CREATE DATABASE matomo;
37+ CREATE USER 'matomo'@'localhost' IDENTIFIED WITH auth_socket;
38+ GRANT ALL PRIVILEGES ON matomo.* TO 'matomo'@'localhost';
39+ </programlisting>
40+41+ Then fill in <literal>matomo</literal> as database user and database name, and leave the password field blank.
42+ This authentication works by allowing only the <literal>matomo</literal> unix user to authenticate as the
43+ <literal>matomo</literal> database user (without needing a password), but no other users.
44+ For more information on passwordless login, see
45+ <link xlink:href="https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin/" />.
46+ </para>
47+48+ <para>
49+ Of course, you can use password based authentication as well, e.g. when the database is not on the same host.
50+ </para>
51+ </section>
52+53+54+ <section>
55+ <title>Backup</title>
56+ <para>
57+ You only need to take backups of your MySQL database and the
58+ <filename>/var/lib/matomo/config/config.ini.php</filename> file.
59+ Use a user in the <literal>matomo</literal> group or root to access the file.
60+ For more information, see <link xlink:href="https://matomo.org/faq/how-to-install/faq_138/" />.
61+ </para>
62+ </section>
63+64+65+ <section>
66+ <title>Issues</title>
67+ <itemizedlist>
68+ <listitem>
69+ <para>
70+ Matomo's file integrity check will warn you.
71+ This is due to the patches necessary for NixOS, you can safely ignore this.
72+ </para>
73+ </listitem>
74+75+ <listitem>
76+ <para>
77+ Matomo will warn you that the JavaScript tracker is not writable.
78+ This is because it's located in the read-only nix store.
79+ You can safely ignore this, unless you need a plugin that needs JavaScript tracker access.
80+ </para>
81+ </listitem>
82+ </itemizedlist>
83+ </section>
84+85+86+ <section>
87+ <title>Using other Web Servers than nginx</title>
88+89+ <para>
90+ You can use other web servers by forwarding calls for <filename>index.php</filename> and
91+ <filename>piwik.php</filename> to the <literal>/run/phpfpm-matomo.sock</literal> fastcgi unix socket.
92+ You can use the nginx configuration in the module code as a reference to what else should be configured.
93+ </para>
94+ </section>
95+</chapter>
-95
nixos/modules/services/web-apps/piwik-doc.xml
···1-<chapter xmlns="http://docbook.org/ns/docbook"
2- xmlns:xlink="http://www.w3.org/1999/xlink"
3- xmlns:xi="http://www.w3.org/2001/XInclude"
4- version="5.0"
5- xml:id="module-services-piwik">
6-7- <title>Piwik</title>
8- <para>
9- Piwik is a real-time web analytics application.
10- This module configures php-fpm as backend for piwik, optionally configuring an nginx vhost as well.
11- </para>
12-13- <para>
14- An automatic setup is not suported by piwik, so you need to configure piwik itself in the browser-based piwik setup.
15- </para>
16-17-18- <section>
19- <title>Database Setup</title>
20-21- <para>
22- You also need to configure a MariaDB or MySQL database and -user for piwik yourself,
23- and enter those credentials in your browser.
24- You can use passwordless database authentication via the UNIX_SOCKET authentication plugin
25- with the following SQL commands:
26-27- <programlisting>
28- # For MariaDB
29- INSTALL PLUGIN unix_socket SONAME 'auth_socket';
30- CREATE DATABASE piwik;
31- CREATE USER 'piwik'@'localhost' IDENTIFIED WITH unix_socket;
32- GRANT ALL PRIVILEGES ON piwik.* TO 'piwik'@'localhost';
33-34- # For MySQL
35- INSTALL PLUGIN auth_socket SONAME 'auth_socket.so';
36- CREATE DATABASE piwik;
37- CREATE USER 'piwik'@'localhost' IDENTIFIED WITH auth_socket;
38- GRANT ALL PRIVILEGES ON piwik.* TO 'piwik'@'localhost';
39- </programlisting>
40-41- Then fill in <literal>piwik</literal> as database user and database name, and leave the password field blank.
42- This authentication works by allowing only the <literal>piwik</literal> unix user to authenticate as the
43- <literal>piwik</literal> database user (without needing a password), but no other users.
44- For more information on passwordless login, see
45- <link xlink:href="https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin/" />.
46- </para>
47-48- <para>
49- Of course, you can use password based authentication as well, e.g. when the database is not on the same host.
50- </para>
51- </section>
52-53-54- <section>
55- <title>Backup</title>
56- <para>
57- You only need to take backups of your MySQL database and the
58- <filename>/var/lib/piwik/config/config.ini.php</filename> file.
59- Use a user in the <literal>piwik</literal> group or root to access the file.
60- For more information, see <link xlink:href="https://piwik.org/faq/how-to-install/faq_138/" />.
61- </para>
62- </section>
63-64-65- <section>
66- <title>Issues</title>
67- <itemizedlist>
68- <listitem>
69- <para>
70- Piwik's file integrity check will warn you.
71- This is due to the patches necessary for NixOS, you can safely ignore this.
72- </para>
73- </listitem>
74-75- <listitem>
76- <para>
77- Piwik will warn you that the JavaScript tracker is not writable.
78- This is because it's located in the read-only nix store.
79- You can safely ignore this, unless you need a plugin that needs JavaScript tracker access.
80- </para>
81- </listitem>
82- </itemizedlist>
83- </section>
84-85-86- <section>
87- <title>Using other Web Servers than nginx</title>
88-89- <para>
90- You can use other web servers by forwarding calls for <filename>index.php</filename> and
91- <filename>piwik.php</filename> to the <literal>/run/phpfpm-piwik.sock</literal> fastcgi unix socket.
92- You can use the nginx configuration in the module code as a reference to what else should be configured.
93- </para>
94- </section>
95-</chapter>
···1{ config, lib, pkgs, services, ... }:
2with lib;
3let
4- cfg = config.services.piwik;
56- user = "piwik";
7 dataDir = "/var/lib/${user}";
089 pool = user;
10 # it's not possible to use /run/phpfpm/${pool}.sock because /run/phpfpm/ is root:root 0770,
···1516in {
17 options = {
18- services.piwik = {
19 # NixOS PR for database setup: https://github.com/NixOS/nixpkgs/pull/6963
20- # piwik issue for automatic piwik setup: https://github.com/piwik/piwik/issues/10257
21- # TODO: find a nice way to do this when more NixOS MySQL and / or piwik automatic setup stuff is implemented.
22 enable = mkOption {
23 type = types.bool;
24 default = false;
25 description = ''
26- Enable piwik web analytics with php-fpm backend.
27 Either the nginx option or the webServerUser option is mandatory.
28 '';
29 };
···32 type = types.nullOr types.str;
33 default = null;
34 example = "lighttpd";
035 description = ''
36- Name of the web server user that forwards requests to the ${phpSocket} fastcgi socket for piwik if the nginx
37 option is not used. Either this option or the nginx option is mandatory.
38 If you want to use another webserver than nginx, you need to set this to that server's user
39 and pass fastcgi requests to `index.php` and `piwik.php` to this socket.
···55 catch_workers_output = yes
56 '';
57 description = ''
58- Settings for phpfpm's process manager. You might need to change this depending on the load for piwik.
59 '';
60 };
61···65 (import ../web-servers/nginx/vhost-options.nix { inherit config lib; })
66 {
67 # enable encryption by default,
68- # as sensitive login and piwik data should not be transmitted in clear text.
69 options.forceSSL.default = true;
70 options.enableACME.default = true;
71 }
···77 enableACME = false;
78 };
79 description = ''
80- With this option, you can customize an nginx virtualHost which already has sensible defaults for piwik.
81 Either this option or the webServerUser option is mandatory.
82 Set this to {} to just enable the virtualHost if you don't need any customization.
83- If enabled, then by default, the serverName is piwik.$\{config.networking.hostName\}, SSL is active,
84 and certificates are acquired via ACME.
85 If this is set to null (the default), no nginx virtualHost will be configured.
86 '';
···9091 config = mkIf cfg.enable {
92 warnings = mkIf (cfg.nginx != null && cfg.webServerUser != null) [
93- "If services.piwik.nginx is set, services.piwik.nginx.webServerUser is ignored and should be removed."
94 ];
9596 assertions = [ {
97 assertion = cfg.nginx != null || cfg.webServerUser != null;
98- message = "Either services.piwik.nginx or services.piwik.nginx.webServerUser is mandatory";
99 }];
100101 users.extraUsers.${user} = {
···106 };
107 users.extraGroups.${user} = {};
108109- systemd.services.piwik_setup_update = {
110- # everything needs to set up and up to date before piwik php files are executed
111 requiredBy = [ "${phpExecutionUnit}.service" ];
112 before = [ "${phpExecutionUnit}.service" ];
113 # the update part of the script can only work if the database is already up and running
114 requires = [ databaseService ];
115 after = [ databaseService ];
116- path = [ pkgs.piwik ];
117 serviceConfig = {
118 Type = "oneshot";
119 User = user;
120 # hide especially config.ini.php from other
121 UMask = "0007";
0122 Environment = "PIWIK_USER_PATH=${dataDir}";
123 # chown + chmod in preStart needs root
124 PermissionsStartOnly = true;
···127 # e.g. after restoring from backup or moving from another system.
128 # Note that ${dataDir}/config/config.ini.php might contain the MySQL password.
129 preStart = ''
00000130 chown -R ${user}:${user} ${dataDir}
131 chmod -R ug+rwX,o-rwx ${dataDir}
132 '';
133 script = ''
134- # Use User-Private Group scheme to protect piwik data, but allow administration / backup via piwik group
135 # Copy config folder
136 chmod g+s "${dataDir}"
137- cp -r "${pkgs.piwik}/config" "${dataDir}/"
138 chmod -R u+rwX,g+rwX,o-rwx "${dataDir}"
139140 # check whether user setup has already been done
141 if test -f "${dataDir}/config/config.ini.php"; then
142 # then execute possibly pending database upgrade
143- piwik-console core:update --yes
144 fi
145 '';
146 };
147148 systemd.services.${phpExecutionUnit} = {
149- # stop phpfpm on package upgrade, do database upgrade via piwik_setup_update, and then restart
150- restartTriggers = [ pkgs.piwik ];
151 # stop config.ini.php from getting written with read permission for others
152 serviceConfig.UMask = "0007";
153 };
···175 # References:
176 # https://fralef.me/piwik-hardening-with-nginx-and-php-fpm.html
177 # https://github.com/perusio/piwik-nginx
0178 "${user}.${config.networking.hostName}" = mkMerge [ cfg.nginx {
179- # don't allow to override the root easily, as it will almost certainly break piwik.
180 # disadvantage: not shown as default in docs.
181- root = mkForce "${pkgs.piwik}/share";
182183 # define locations here instead of as the submodule option's default
184 # so that they can easily be extended with additional locations if required
185- # without needing to redefine the piwik ones.
186 # disadvantage: not shown as default in docs.
187 locations."/" = {
188 index = "index.php";
···191 locations."= /index.php".extraConfig = ''
192 fastcgi_pass unix:${phpSocket};
193 '';
0194 # allow piwik.php for tracking
195 locations."= /piwik.php".extraConfig = ''
196 fastcgi_pass unix:${phpSocket};
···212 locations."= /robots.txt".extraConfig = ''
213 return 200 "User-agent: *\nDisallow: /\n";
214 '';
0215 # let browsers cache piwik.js
216 locations."= /piwik.js".extraConfig = ''
217 expires 1M;
···221 };
222223 meta = {
224- doc = ./piwik-doc.xml;
225 maintainers = with stdenv.lib.maintainers; [ florianjacob ];
226 };
227}
···1{ config, lib, pkgs, services, ... }:
2with lib;
3let
4+ cfg = config.services.matomo;
56+ user = "matomo";
7 dataDir = "/var/lib/${user}";
8+ deprecatedDataDir = "/var/lib/piwik";
910 pool = user;
11 # it's not possible to use /run/phpfpm/${pool}.sock because /run/phpfpm/ is root:root 0770,
···1617in {
18 options = {
19+ services.matomo = {
20 # NixOS PR for database setup: https://github.com/NixOS/nixpkgs/pull/6963
21+ # matomo issue for automatic matomo setup: https://github.com/matomo-org/matomo/issues/10257
22+ # TODO: find a nice way to do this when more NixOS MySQL and / or matomo automatic setup stuff is implemented.
23 enable = mkOption {
24 type = types.bool;
25 default = false;
26 description = ''
27+ Enable matomo web analytics with php-fpm backend.
28 Either the nginx option or the webServerUser option is mandatory.
29 '';
30 };
···33 type = types.nullOr types.str;
34 default = null;
35 example = "lighttpd";
36+ # TODO: piwik.php might get renamed to matomo.php in future releases
37 description = ''
38+ Name of the web server user that forwards requests to the ${phpSocket} fastcgi socket for matomo if the nginx
39 option is not used. Either this option or the nginx option is mandatory.
40 If you want to use another webserver than nginx, you need to set this to that server's user
41 and pass fastcgi requests to `index.php` and `piwik.php` to this socket.
···57 catch_workers_output = yes
58 '';
59 description = ''
60+ Settings for phpfpm's process manager. You might need to change this depending on the load for matomo.
61 '';
62 };
63···67 (import ../web-servers/nginx/vhost-options.nix { inherit config lib; })
68 {
69 # enable encryption by default,
70+ # as sensitive login and matomo data should not be transmitted in clear text.
71 options.forceSSL.default = true;
72 options.enableACME.default = true;
73 }
···79 enableACME = false;
80 };
81 description = ''
82+ With this option, you can customize an nginx virtualHost which already has sensible defaults for matomo.
83 Either this option or the webServerUser option is mandatory.
84 Set this to {} to just enable the virtualHost if you don't need any customization.
85+ If enabled, then by default, the serverName is ${user}.$\{config.networking.hostName\}, SSL is active,
86 and certificates are acquired via ACME.
87 If this is set to null (the default), no nginx virtualHost will be configured.
88 '';
···9293 config = mkIf cfg.enable {
94 warnings = mkIf (cfg.nginx != null && cfg.webServerUser != null) [
95+ "If services.matomo.nginx is set, services.matomo.nginx.webServerUser is ignored and should be removed."
96 ];
9798 assertions = [ {
99 assertion = cfg.nginx != null || cfg.webServerUser != null;
100+ message = "Either services.matomo.nginx or services.matomo.nginx.webServerUser is mandatory";
101 }];
102103 users.extraUsers.${user} = {
···108 };
109 users.extraGroups.${user} = {};
110111+ systemd.services.matomo_setup_update = {
112+ # everything needs to set up and up to date before matomo php files are executed
113 requiredBy = [ "${phpExecutionUnit}.service" ];
114 before = [ "${phpExecutionUnit}.service" ];
115 # the update part of the script can only work if the database is already up and running
116 requires = [ databaseService ];
117 after = [ databaseService ];
118+ path = [ pkgs.matomo ];
119 serviceConfig = {
120 Type = "oneshot";
121 User = user;
122 # hide especially config.ini.php from other
123 UMask = "0007";
124+ # TODO: might get renamed to MATOMO_USER_PATH in future versions
125 Environment = "PIWIK_USER_PATH=${dataDir}";
126 # chown + chmod in preStart needs root
127 PermissionsStartOnly = true;
···130 # e.g. after restoring from backup or moving from another system.
131 # Note that ${dataDir}/config/config.ini.php might contain the MySQL password.
132 preStart = ''
133+ # migrate data from piwik to matomo folder
134+ if [ -d ${deprecatedDataDir} ]; then
135+ echo "Migrating from ${deprecatedDataDir} to ${dataDir}"
136+ mv -T ${deprecatedDataDir} ${dataDir}
137+ fi
138 chown -R ${user}:${user} ${dataDir}
139 chmod -R ug+rwX,o-rwx ${dataDir}
140 '';
141 script = ''
142+ # Use User-Private Group scheme to protect matomo data, but allow administration / backup via matomo group
143 # Copy config folder
144 chmod g+s "${dataDir}"
145+ cp -r "${pkgs.matomo}/config" "${dataDir}/"
146 chmod -R u+rwX,g+rwX,o-rwx "${dataDir}"
147148 # check whether user setup has already been done
149 if test -f "${dataDir}/config/config.ini.php"; then
150 # then execute possibly pending database upgrade
151+ matomo-console core:update --yes
152 fi
153 '';
154 };
155156 systemd.services.${phpExecutionUnit} = {
157+ # stop phpfpm on package upgrade, do database upgrade via matomo_setup_update, and then restart
158+ restartTriggers = [ pkgs.matomo ];
159 # stop config.ini.php from getting written with read permission for others
160 serviceConfig.UMask = "0007";
161 };
···183 # References:
184 # https://fralef.me/piwik-hardening-with-nginx-and-php-fpm.html
185 # https://github.com/perusio/piwik-nginx
186+ # TODO: better default
187 "${user}.${config.networking.hostName}" = mkMerge [ cfg.nginx {
188+ # don't allow to override the root easily, as it will almost certainly break matomo.
189 # disadvantage: not shown as default in docs.
190+ root = mkForce "${pkgs.matomo}/share";
191192 # define locations here instead of as the submodule option's default
193 # so that they can easily be extended with additional locations if required
194+ # without needing to redefine the matomo ones.
195 # disadvantage: not shown as default in docs.
196 locations."/" = {
197 index = "index.php";
···200 locations."= /index.php".extraConfig = ''
201 fastcgi_pass unix:${phpSocket};
202 '';
203+ # TODO: might get renamed to matomo.php in future versions
204 # allow piwik.php for tracking
205 locations."= /piwik.php".extraConfig = ''
206 fastcgi_pass unix:${phpSocket};
···222 locations."= /robots.txt".extraConfig = ''
223 return 200 "User-agent: *\nDisallow: /\n";
224 '';
225+ # TODO: might get renamed to matomo.js in future versions
226 # let browsers cache piwik.js
227 locations."= /piwik.js".extraConfig = ''
228 expires 1M;
···232 };
233234 meta = {
235+ doc = ./matomo-doc.xml;
236 maintainers = with stdenv.lib.maintainers; [ florianjacob ];
237 };
238}