about summary refs log tree commit diff
path: root/nixpkgs/pkgs/servers/web-apps/matomo/default.nix
blob: 383a8658c5216001270d2e041b3c5238497f3970 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{ lib, stdenv, fetchurl, makeWrapper, php, nixosTests }:

let
  versions = {
    matomo = {
      version = "4.16.1";
      hash = "sha256-cGnsxfpvt7FyhxFcA2/gWWe7CyanVGZVKtCDES3XLdI=";
    };
    matomo_5 = {
      version = "5.0.2";
      hash = "sha256-rLAShJLtzd3HB1Je+P+i8GKWdeklyC2sTnmPR07Md+8=";
    };
    matomo-beta = {
      version = "5.0.0";
      # `beta` examples: "b1", "rc1", null
      # when updating: use null if stable version is >= latest beta or release candidate
      beta = "rc9";
      hash = "sha256-OXxJCEXcrl6UXYh+jbNqLQGYphrSjxaOAZg3AZVPAqs=";
    };
  };
  common = pname: { version, hash, beta ? null }:
    let
      fullVersion = version + lib.optionalString (beta != null) "-${toString beta}";
      name = "${pname}-${fullVersion}";
    in

      stdenv.mkDerivation rec {
        inherit name;
        version = fullVersion;

        src = fetchurl {
          url = "https://builds.matomo.org/matomo-${version}.tar.gz";
          inherit hash;
        };

        nativeBuildInputs = [ makeWrapper ];

        patches = [
          # This changes the default value of the database server field
          # from 127.0.0.1 to localhost.
          # unix socket authentication only works with localhost,
          # but password-based SQL authentication works with both.
          # TODO: is upstream interested in this?
          # -> discussion at https://github.com/matomo-org/matomo/issues/12646
          ./make-localhost-default-database-host.patch
          # This changes the default config for path.geoip2 so that it doesn't point
          # to the nix store.
          (if lib.versionOlder version "5.0"
           then ./change-path-geoip2-4.x.patch
           else ./change-path-geoip2-5.x.patch)
        ];

        # this bootstrap.php adds support for getting PIWIK_USER_PATH
        # from an environment variable. Point it to a mutable location
        # to be able to use matomo read-only from the nix store
        postPatch = ''
          cp ${./bootstrap.php} bootstrap.php
        '';

        # TODO: future versions might rename the PIWIK_… variables to MATOMO_…
        # TODO: Move more unnecessary files from share/, especially using PIWIK_INCLUDE_PATH.
        #       See https://forum.matomo.org/t/bootstrap-php/5926/10 and
        #       https://github.com/matomo-org/matomo/issues/11654#issuecomment-297730843
        installPhase = ''
          runHook preInstall

          # copy everything to share/, used as webroot folder, and then remove what's known to be not needed
          mkdir -p $out/share
          cp -ra * $out/share/
          # tmp/ is created by matomo in PIWIK_USER_PATH
          rmdir $out/share/tmp
          # config/ needs to be accessed by PIWIK_USER_PATH anyway
          ln -s $out/share/config $out/

          makeWrapper ${php}/bin/php $out/bin/matomo-console \
            --add-flags "$out/share/console"

          runHook postInstall
        '';

        filesToFix = [
          "misc/composer/build-xhprof.sh"
          "misc/composer/clean-xhprof.sh"
          "misc/cron/archive.sh"
          "plugins/GeoIp2/config/config.php"
          "plugins/Installation/FormDatabaseSetup.php"
          "vendor/pear/archive_tar/sync-php4"
          "vendor/szymach/c-pchart/coverage.sh"
          "vendor/matomo/matomo-php-tracker/run_tests.sh"
          "vendor/twig/twig/drupal_test.sh"
        ];

        # This fixes the consistency check in the admin interface
        #
        # The filesToFix list may contain files that are exclusive to only one of the versions we build
        # make sure to test for existence to avoid erroring on an incompatible version and failing
        postFixup = ''
          pushd $out/share > /dev/null
          for f in $filesToFix; do
            if [ -f "$f" ]; then
              length="$(wc -c "$f" | cut -d' ' -f1)"
              hash="$(md5sum "$f" | cut -d' ' -f1)"
              sed -i "s:\\(\"$f\"[^(]*(\\).*:\\1\"$length\", \"$hash\"),:g" config/manifest.inc.php
            else
              echo "INFO(files-to-fix): $f does not exist in this version"
            fi
          done
          popd > /dev/null
        '';

        passthru = {
          tests = nixosTests.matomo."${pname}";
        };

        meta = with lib; {
          description = "A real-time web analytics application";
          license = licenses.gpl3Plus;
          homepage = "https://matomo.org/";
          platforms = platforms.all;
          maintainers = with maintainers; [ florianjacob kiwi sebbel twey boozedog ] ++ teams.flyingcircus.members;
        };
      };
in
lib.mapAttrs common versions