about summary refs log tree commit diff
path: root/nixpkgs/pkgs/servers/web-apps/plausible
diff options
context:
space:
mode:
Diffstat (limited to 'nixpkgs/pkgs/servers/web-apps/plausible')
-rw-r--r--nixpkgs/pkgs/servers/web-apps/plausible/default.nix95
-rwxr-xr-xnixpkgs/pkgs/servers/web-apps/plausible/update.sh62
2 files changed, 157 insertions, 0 deletions
diff --git a/nixpkgs/pkgs/servers/web-apps/plausible/default.nix b/nixpkgs/pkgs/servers/web-apps/plausible/default.nix
new file mode 100644
index 000000000000..24f684c0c954
--- /dev/null
+++ b/nixpkgs/pkgs/servers/web-apps/plausible/default.nix
@@ -0,0 +1,95 @@
+{ lib
+, beamPackages
+, buildNpmPackage
+, fetchFromGitHub
+, nodejs
+, nixosTests
+, ...
+}:
+
+let
+  pname = "plausible";
+  version = "2.0.0";
+
+  src = fetchFromGitHub {
+    owner = "plausible";
+    repo = "analytics";
+    rev = "v${version}";
+    hash = "sha256-yrTwxBguAZbfEKucUL+w49Hr6D7v9/2OjY1h27+w5WI=";
+  };
+
+  # TODO consider using `mix2nix` as soon as it supports git dependencies.
+  mixFodDeps = beamPackages.fetchMixDeps {
+    pname = "${pname}-deps";
+    inherit src version;
+    hash = "sha256-CAyZLpjmw1JreK3MopqI0XsWhP+fJEMpXlww7CibSaM=";
+  };
+
+  assets = buildNpmPackage {
+    pname = "${pname}-assets";
+    inherit version;
+    src = "${src}/assets";
+    npmDepsHash = "sha256-2t1M6RQhBjZxx36qawVUVC+ob9SvQIq5dy4HgVeY2Eo=";
+    dontNpmBuild = true;
+    installPhase = ''
+      runHook preInstall
+      cp -r . "$out"
+      runHook postInstall
+    '';
+  };
+
+  tracker = buildNpmPackage {
+    pname = "${pname}-tracker";
+    inherit version;
+    src = "${src}/tracker";
+    npmDepsHash = "sha256-y09jVSwUrxF0nLpLqS1yQweYL+iMF6jVx0sUdQtvrpc=";
+    dontNpmBuild = true;
+    installPhase = ''
+      runHook preInstall
+      cp -r . "$out"
+      runHook postInstall
+    '';
+  };
+in
+beamPackages.mixRelease {
+  inherit pname version src mixFodDeps;
+
+  nativeBuildInputs = [
+    nodejs
+  ];
+
+  passthru = {
+    tests = { inherit (nixosTests) plausible; };
+    updateScript = ./update.sh;
+  };
+
+  postPatch = ''
+    substituteInPlace lib/plausible_release.ex --replace 'defp prepare do' 'def prepare do'
+  '';
+
+  preBuild = ''
+    rm -r assets tracker
+    cp -r ${assets} assets
+    cp -r ${tracker} tracker
+  '';
+
+  postBuild = ''
+    export NODE_OPTIONS=--openssl-legacy-provider # required for webpack compatibility with OpenSSL 3 (https://github.com/webpack/webpack/issues/14532)
+    npm run deploy --prefix ./assets
+    npm run deploy --prefix ./tracker
+
+    # for external task you need a workaround for the no deps check flag
+    # https://github.com/phoenixframework/phoenix/issues/2690
+    mix do deps.loadpaths --no-deps-check, phx.digest
+  '';
+
+  meta = with lib; {
+    license = licenses.agpl3Plus;
+    homepage = "https://plausible.io/";
+    changelog = "https://github.com/plausible/analytics/blob/${src.rev}/CHANGELOG.md";
+    description = " Simple, open-source, lightweight (< 1 KB) and privacy-friendly web analytics alternative to Google Analytics";
+    mainProgram = "plausible";
+    maintainers = with maintainers; [ ];
+    platforms = platforms.unix;
+  };
+}
diff --git a/nixpkgs/pkgs/servers/web-apps/plausible/update.sh b/nixpkgs/pkgs/servers/web-apps/plausible/update.sh
new file mode 100755
index 000000000000..8ed285cfcb88
--- /dev/null
+++ b/nixpkgs/pkgs/servers/web-apps/plausible/update.sh
@@ -0,0 +1,62 @@
+#!/usr/bin/env nix-shell
+#!nix-shell -i bash -p jq nix-prefetch-github yarn yarn2nix-moretea.yarn2nix moreutils
+
+# NOTE: please check on new releases which steps aren't necessary anymore!
+# Currently the following things are done:
+#
+# * Add correct `name`/`version` field to `package.json`, otherwise `yarn2nix` fails to
+#   find required dependencies.
+# * Adjust `file:`-dependencies a bit for the structure inside a Nix build.
+# * Update hashes for the tarball & the fixed-output drv with all `mix`-dependencies.
+# * Generate `yarn.lock` & `yarn.nix` in a temporary directory.
+
+set -euxo pipefail
+
+dir="$(realpath $(dirname "$0"))"
+export latest="$(curl -q https://api.github.com/repos/plausible/analytics/releases/latest \
+  | jq -r '.tag_name')"
+nix_version="$(cut -c2- <<< "$latest")"
+
+if [[ "$(nix-instantiate -A plausible.version --eval --json | jq -r)" = "$nix_version" ]];
+then
+  echo "Already using version $latest, skipping"
+  exit 0
+fi
+
+SRC="https://raw.githubusercontent.com/plausible/analytics/${latest}"
+
+package_json="$(curl -qf "$SRC/assets/package.json")"
+
+echo "$package_json" \
+  | jq '. + {"name":"plausible","version": $ENV.latest}' \
+  | sed -e 's,../deps/,../../tmp/deps/,g' \
+  > $dir/package.json
+
+tarball_meta="$(nix-prefetch-github plausible analytics --rev "$latest")"
+tarball_hash="$(jq -r '.hash' <<< "$tarball_meta")"
+tarball_path="$(nix-build -E 'with import ./. {}; { p }: fetchFromGitHub (builtins.fromJSON p)' --argstr p "$tarball_meta")"
+fake_hash="$(nix-instantiate --eval -A lib.fakeHash | xargs echo)"
+
+sed -i "$dir/default.nix" \
+  -e 's,version = ".*",version = "'"$nix_version"'",' \
+  -e '/^  src = fetchFromGitHub/,+4{;s#hash = "\(.*\)"#hash = "'"$tarball_hash"'"#}' \
+  -e '/^  mixFodDeps =/,+3{;s#hash = "\(.*\)"#hash = "'"$fake_hash"'"#}'
+
+mix_hash="$(nix-build -A plausible.mixFodDeps 2>&1 | tail -n3 | grep 'got:' | cut -d: -f2- | xargs echo || true)"
+
+sed -i "$dir/default.nix" -e '/^  mixFodDeps =/,+3{;s#hash = "\(.*\)"#hash = "'"$mix_hash"'"#}'
+
+tmp_setup_dir="$(mktemp -d)"
+trap "rm -rf $tmp_setup_dir" EXIT
+
+cp -r $tarball_path/* $tmp_setup_dir/
+cp -r "$(nix-build -A plausible.mixFodDeps)" "$tmp_setup_dir/deps"
+chmod -R u+rwx "$tmp_setup_dir"
+
+pushd $tmp_setup_dir/assets
+yarn
+yarn2nix > "$dir/yarn.nix"
+cp yarn.lock "$dir/yarn.lock"
+popd
+
+nix-build -A plausible