···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="module-services-piwik">
66+77+ <title>Piwik</title>
88+ <para>
99+ Piwik is a real-time web analytics application.
1010+ This module configures php-fpm as backend for piwik, optionally configuring an nginx vhost as well.
1111+ </para>
1212+1313+ <para>
1414+ An automatic setup is not suported by piwik, so you need to configure piwik itself in the browser-based piwik setup.
1515+ </para>
1616+1717+1818+ <section>
1919+ <title>Database Setup</title>
2020+2121+ <para>
2222+ You also need to configure a MariaDB or MySQL database and -user for piwik yourself,
2323+ and enter those credentials in your browser.
2424+ You can use passwordless database authentication via the UNIX_SOCKET authentication plugin
2525+ with the following SQL commands:
2626+ <programlisting>
2727+ INSTALL PLUGIN unix_socket SONAME 'auth_socket';
2828+ ALTER USER root IDENTIFIED VIA unix_socket;
2929+ CREATE DATABASE piwik;
3030+ CREATE USER 'piwik'@'localhost' IDENTIFIED VIA unix_socket;
3131+ GRANT ALL PRIVILEGES ON piwik.* TO 'piwik'@'localhost';
3232+ </programlisting>
3333+ Then fill in <literal>piwik</literal> as database user and database name, and leave the password field blank.
3434+ This works with MariaDB and MySQL. This authentication works by allowing only the <literal>piwik</literal> unix
3535+ user to authenticate as <literal>piwik</literal> database (without needing a password), but no other users.
3636+ For more information on passwordless login, see
3737+ <link xlink:href="https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin/" />.
3838+ </para>
3939+4040+ <para>
4141+ Of course, you can use password based authentication as well, e.g. when the database is not on the same host.
4242+ </para>
4343+ </section>
4444+4545+4646+ <section>
4747+ <title>Backup</title>
4848+ <para>
4949+ You only need to take backups of your MySQL database and the
5050+ <filename>/var/lib/piwik/config/config.ini.php</filename> file.
5151+ Use a user in the <literal>piwik</literal> group or root to access the file.
5252+ For more information, see <link xlink:href="https://piwik.org/faq/how-to-install/faq_138/" />.
5353+ </para>
5454+ </section>
5555+5656+5757+ <section>
5858+ <title>Issues</title>
5959+ <itemizedlist>
6060+ <listitem>
6161+ <para>
6262+ Piwik's file integrity check will warn you.
6363+ This is due to the patches necessary for NixOS, you can safely ignore this.
6464+ </para>
6565+ </listitem>
6666+6767+ <listitem>
6868+ <para>
6969+ Piwik will warn you that the JavaScript tracker is not writable.
7070+ This is because it's located in the read-only nix store.
7171+ You can safely ignore this, unless you need a plugin that needs JavaScript tracker access.
7272+ </para>
7373+ </listitem>
7474+7575+ <listitem>
7676+ <para>
7777+ Sending mail from piwik, e.g. for the password reset function, might not work out of the box:
7878+ There's a problem with using <command>sendmail</command> from <literal>php-fpm</literal> that is
7979+ being investigated at <link xlink:href="https://github.com/NixOS/nixpkgs/issues/26611" />.
8080+ If you have (or don't have) this problem as well, please report it. You can enable SMTP as method
8181+ to send mail in piwik's <quote>General Settings</quote> > <quote>Mail Server Settings</quote> instead.
8282+ </para>
8383+ </listitem>
8484+ </itemizedlist>
8585+ </section>
8686+8787+8888+ <section>
8989+ <title>Using other Web Servers than nginx</title>
9090+9191+ <para>
9292+ You can use other web servers by forwarding calls for <filename>index.php</filename> and
9393+ <filename>piwik.php</filename> to the <literal>/run/phpfpm-piwik.sock</literal> fastcgi unix socket.
9494+ You can use the nginx configuration in the module code as a reference to what else should be configured.
9595+ </para>
9696+ </section>
9797+</chapter>
+219
nixos/modules/services/web-apps/piwik.nix
···11+{ config, lib, pkgs, services, ... }:
22+with lib;
33+let
44+ cfg = config.services.piwik;
55+66+ user = "piwik";
77+ dataDir = "/var/lib/${user}";
88+99+ pool = user;
1010+ # it's not possible to use /run/phpfpm/${pool}.sock because /run/phpfpm/ is root:root 0770,
1111+ # and therefore is not accessible by the web server.
1212+ phpSocket = "/run/phpfpm-${pool}.sock";
1313+ phpExecutionUnit = "phpfpm-${pool}";
1414+ databaseService = "mysql.service";
1515+1616+in {
1717+ options = {
1818+ services.piwik = {
1919+ # NixOS PR for database setup: https://github.com/NixOS/nixpkgs/pull/6963
2020+ # piwik issue for automatic piwik setup: https://github.com/piwik/piwik/issues/10257
2121+ # TODO: find a nice way to do this when more NixOS MySQL and / or piwik automatic setup stuff is implemented.
2222+ enable = mkOption {
2323+ type = types.bool;
2424+ default = false;
2525+ description = ''
2626+ Enable piwik web analytics with php-fpm backend.
2727+ '';
2828+ };
2929+3030+ webServerUser = mkOption {
3131+ type = types.str;
3232+ example = "nginx";
3333+ description = ''
3434+ Name of the owner of the ${phpSocket} fastcgi socket for piwik.
3535+ If you want to use another webserver than nginx, you need to set this to that server's user
3636+ and pass fastcgi requests to `index.php` and `piwik.php` to this socket.
3737+ '';
3838+ };
3939+4040+ phpfpmProcessManagerConfig = mkOption {
4141+ type = types.str;
4242+ default = ''
4343+ ; default phpfpm process manager settings
4444+ pm = dynamic
4545+ pm.max_children = 75
4646+ pm.start_servers = 10
4747+ pm.min_spare_servers = 5
4848+ pm.max_spare_servers = 20
4949+ pm.max_requests = 500
5050+5151+ ; log worker's stdout, but this has a performance hit
5252+ catch_workers_output = yes
5353+ '';
5454+ description = ''
5555+ Settings for phpfpm's process manager. You might need to change this depending on the load for piwik.
5656+ '';
5757+ };
5858+5959+ nginx = mkOption {
6060+ # TODO: for maximum flexibility, it would be nice to use nginx's vhost_options module
6161+ # but this only makes sense if we can somehow specify defaults suitable for piwik.
6262+ # But users can always copy the piwik nginx config to their configuration.nix and customize it.
6363+ type = types.nullOr (types.submodule {
6464+ options = {
6565+ virtualHost = mkOption {
6666+ type = types.str;
6767+ default = "piwik.${config.networking.hostName}";
6868+ example = "piwik.$\{config.networking.hostName\}";
6969+ description = ''
7070+ Name of the nginx virtualhost to use and set up.
7171+ '';
7272+ };
7373+ enableSSL = mkOption {
7474+ type = types.bool;
7575+ default = true;
7676+ description = "Whether to enable https.";
7777+ };
7878+ forceSSL = mkOption {
7979+ type = types.bool;
8080+ default = true;
8181+ description = "Whether to always redirect to https.";
8282+ };
8383+ enableACME = mkOption {
8484+ type = types.bool;
8585+ default = true;
8686+ description = "Whether to ask Let's Encrypt to sign a certificate for this vhost.";
8787+ };
8888+ };
8989+ });
9090+ default = null;
9191+ example = { virtualHost = "stats.$\{config.networking.hostName\}"; };
9292+ description = ''
9393+ The options to use to configure an nginx virtualHost.
9494+ If null (the default), no nginx virtualHost will be configured.
9595+ '';
9696+ };
9797+ };
9898+ };
9999+100100+ config = mkIf cfg.enable {
101101+102102+ users.extraUsers.${user} = {
103103+ isSystemUser = true;
104104+ createHome = true;
105105+ home = dataDir;
106106+ group = user;
107107+ };
108108+ users.extraGroups.${user} = {};
109109+110110+ systemd.services.piwik_setup_update = {
111111+ # everything needs to set up and up to date before piwik php files are executed
112112+ requiredBy = [ "${phpExecutionUnit}.service" ];
113113+ before = [ "${phpExecutionUnit}.service" ];
114114+ # the update part of the script can only work if the database is already up and running
115115+ requires = [ databaseService ];
116116+ after = [ databaseService ];
117117+ path = [ pkgs.piwik ];
118118+ serviceConfig = {
119119+ Type = "oneshot";
120120+ User = user;
121121+ # hide especially config.ini.php from other
122122+ UMask = "0007";
123123+ Environment = "PIWIK_USER_PATH=${dataDir}";
124124+ # chown + chmod in preStart needs root
125125+ PermissionsStartOnly = true;
126126+ };
127127+ # correct ownership and permissions in case they're not correct anymore,
128128+ # e.g. after restoring from backup or moving from another system.
129129+ # Note that ${dataDir}/config/config.ini.php might contain the MySQL password.
130130+ preStart = ''
131131+ chown -R ${user}:${user} ${dataDir}
132132+ chmod -R ug+rwX,o-rwx ${dataDir}
133133+ '';
134134+ script = ''
135135+ # Use User-Private Group scheme to protect piwik data, but allow administration / backup via piwik group
136136+ # Copy config folder
137137+ chmod g+s "${dataDir}"
138138+ cp -r "${pkgs.piwik}/config" "${dataDir}/"
139139+ chmod -R u+rwX,g+rwX,o-rwx "${dataDir}"
140140+141141+ # check whether user setup has already been done
142142+ if test -f "${dataDir}/config/config.ini.php"; then
143143+ # then execute possibly pending database upgrade
144144+ piwik-console core:update --yes
145145+ fi
146146+ '';
147147+ };
148148+149149+ systemd.services.${phpExecutionUnit} = {
150150+ # stop phpfpm on package upgrade, do database upgrade via piwik_setup_update, and then restart
151151+ restartTriggers = [ pkgs.piwik ];
152152+ # stop config.ini.php from getting written with read permission for others
153153+ serviceConfig.UMask = "0007";
154154+ };
155155+156156+ services.phpfpm.poolConfigs = {
157157+ ${pool} = ''
158158+ listen = "${phpSocket}"
159159+ listen.owner = ${cfg.webServerUser}
160160+ listen.group = root
161161+ listen.mode = 0600
162162+ user = ${user}
163163+ env[PIWIK_USER_PATH] = ${dataDir}
164164+ ${cfg.phpfpmProcessManagerConfig}
165165+ '';
166166+ };
167167+168168+169169+ services.nginx.virtualHosts = mkIf (cfg.nginx != null) {
170170+ # References:
171171+ # https://fralef.me/piwik-hardening-with-nginx-and-php-fpm.html
172172+ # https://github.com/perusio/piwik-nginx
173173+ ${cfg.nginx.virtualHost} = {
174174+ root = "${pkgs.piwik}/share";
175175+ enableSSL = cfg.nginx.enableSSL;
176176+ enableACME = cfg.nginx.enableACME;
177177+ forceSSL = cfg.nginx.forceSSL;
178178+179179+ locations."/" = {
180180+ index = "index.php";
181181+ };
182182+ # allow index.php for webinterface
183183+ locations."= /index.php".extraConfig = ''
184184+ fastcgi_pass unix:${phpSocket};
185185+ '';
186186+ # allow piwik.php for tracking
187187+ locations."= /piwik.php".extraConfig = ''
188188+ fastcgi_pass unix:${phpSocket};
189189+ '';
190190+ # Any other attempt to access any php files is forbidden
191191+ locations."~* ^.+\.php$".extraConfig = ''
192192+ return 403;
193193+ '';
194194+ # Disallow access to unneeded directories
195195+ # config and tmp are already removed
196196+ locations."~ ^/(?:core|lang|misc)/".extraConfig = ''
197197+ return 403;
198198+ '';
199199+ # Disallow access to several helper files
200200+ locations."~* \.(?:bat|git|ini|sh|txt|tpl|xml|md)$".extraConfig = ''
201201+ return 403;
202202+ '';
203203+ # No crawling of this site for bots that obey robots.txt - no useful information here.
204204+ locations."= /robots.txt".extraConfig = ''
205205+ return 200 "User-agent: *\nDisallow: /\n";
206206+ '';
207207+ # let browsers cache piwik.js
208208+ locations."= /piwik.js".extraConfig = ''
209209+ expires 1M;
210210+ '';
211211+ };
212212+ };
213213+ };
214214+215215+ meta = {
216216+ doc = ./piwik-doc.xml;
217217+ maintainers = with stdenv.lib.maintainers; [ florianjacob ];
218218+ };
219219+}
+6
pkgs/servers/web-apps/piwik/bootstrap.php
···11+<?php
22+// get PIWIK_USER_PATH from environment variable,
33+// so this bootstrap.php can be read-only but still configure PIWIK_USER_PATH at runtime
44+if ($path = getenv('PIWIK_USER_PATH')) {
55+ define('PIWIK_USER_PATH', $path);
66+}
+60
pkgs/servers/web-apps/piwik/default.nix
···11+{ stdenv, fetchurl, makeWrapper, php }:
22+33+stdenv.mkDerivation rec {
44+ name = "piwik-${version}";
55+ version = "3.0.4";
66+77+ src = fetchurl {
88+ url = "https://builds.piwik.org/${name}.tar.gz";
99+ sha512 = "2i0vydr073ynv7wcn078zxhvywdv85c648hympkzicdd746g995878py9006m96iwkmk4q664wn3f8jnfqsl1jd9f26alz1nssizbn9";
1010+ };
1111+1212+ nativeBuildInputs = [ makeWrapper ];
1313+1414+ # regarding the PIWIK_USER_PATH substitutes:
1515+ # looks like this is just a bug / confusion of the directories, and nobody has tested this.
1616+ # PR at https://github.com/piwik/piwik/pull/11661
1717+ # regarding the 127.0.0.1 substitute:
1818+ # This replaces the default value of the database server field.
1919+ # unix socket authentication only works with localhost,
2020+ # but password-based SQL authentication works with both.
2121+ postPatch = ''
2222+ substituteInPlace core/AssetManager/UIAssetFetcher.php \
2323+ --replace "return PIWIK_USER_PATH;" "return PIWIK_DOCUMENT_ROOT;"
2424+ substituteInPlace core/AssetManager/UIAssetMerger/StylesheetUIAssetMerger.php \
2525+ --replace "setImportDir(PIWIK_USER_PATH);" "setImportDir(PIWIK_DOCUMENT_ROOT);"
2626+ substituteInPlace core/AssetManager/UIAssetMerger/StylesheetUIAssetMerger.php \
2727+ --replace "\$absolutePath = PIWIK_USER_PATH" "\$absolutePath = PIWIK_DOCUMENT_ROOT"
2828+ substituteInPlace plugins/Installation/FormDatabaseSetup.php \
2929+ --replace "=> '127.0.0.1'," "=> 'localhost',"
3030+ cp ${./bootstrap.php} bootstrap.php
3131+ '';
3232+3333+ # TODO: Move more unnecessary files from share/, especially using PIWIK_INCLUDE_PATH.
3434+ # See https://forum.piwik.org/t/bootstrap-php/5926/10 and
3535+ # https://github.com/piwik/piwik/issues/11654#issuecomment-297730843
3636+ installPhase = ''
3737+ runHook preInstall
3838+3939+ # copy evertything to share/, used as webroot folder, and then remove what's known to be not needed
4040+ mkdir -p $out/share
4141+ cp -ra * $out/share/
4242+ # tmp/ is created by piwik in PIWIK_USER_PATH
4343+ rmdir $out/share/tmp
4444+ # config/ needs to be copied to PIWIK_USER_PATH anyway
4545+ mv $out/share/config $out/
4646+4747+ makeWrapper ${php}/bin/php $out/bin/piwik-console \
4848+ --add-flags "$out/share/console"
4949+5050+ runHook postInstall
5151+ '';
5252+5353+ meta = with stdenv.lib; {
5454+ description = "A real-time web analytics application";
5555+ license = licenses.gpl3Plus;
5656+ homepage = https://piwik.org/;
5757+ platforms = platforms.all;
5858+ maintainers = [ maintainers.florianjacob ];
5959+ };
6060+}