From 67d2c574bda491cc27e131bcc971ba42731b7498 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 4 Aug 2024 09:30:12 +0300 Subject: [PATCH 1/3] nixos/trilium: add adjustable package (cherry picked from commit 655768419e15811f2143941f98d35594f566e78a) --- nixos/modules/services/web-apps/trilium.nix | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/web-apps/trilium.nix b/nixos/modules/services/web-apps/trilium.nix index 42b0a16827c3e..5a3542cdd23e5 100644 --- a/nixos/modules/services/web-apps/trilium.nix +++ b/nixos/modules/services/web-apps/trilium.nix @@ -26,6 +26,8 @@ in options.services.trilium-server = with lib; { enable = mkEnableOption "trilium-server"; + package = mkPackageOption pkgs "trilium-server" {}; + dataDir = mkOption { type = types.str; default = "/var/lib/trilium"; @@ -117,7 +119,7 @@ in wantedBy = [ "multi-user.target" ]; environment.TRILIUM_DATA_DIR = cfg.dataDir; serviceConfig = { - ExecStart = "${pkgs.trilium-server}/bin/trilium-server"; + ExecStart = lib.getExe cfg.package; User = "trilium"; Group = "trilium"; PrivateTmp = "true"; From 1336ee57b3fb5e5f6441bfa2eb3571662cd95760 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 29 Jul 2024 10:10:28 +0300 Subject: [PATCH 2/3] trilium-next-{desktop,server}: init at 0.90.12 Co-Authored-By: FliegendeWurst <2012gdwu+github@posteo.de> (cherry picked from commit 4fcdf706929246476e52e960760b6643cb8395c4) --- .../tr/trilium-next-desktop/package.nix | 129 ++++++++++++++++++ .../by-name/tr/trilium-next-desktop/update.sh | 23 ++++ ...sole-logger-instead-of-rolling-files.patch | 79 +++++++++++ .../tr/trilium-next-server/package.nix | 63 +++++++++ 4 files changed, 294 insertions(+) create mode 100644 pkgs/by-name/tr/trilium-next-desktop/package.nix create mode 100755 pkgs/by-name/tr/trilium-next-desktop/update.sh create mode 100644 pkgs/by-name/tr/trilium-next-server/0001-Use-console-logger-instead-of-rolling-files.patch create mode 100644 pkgs/by-name/tr/trilium-next-server/package.nix diff --git a/pkgs/by-name/tr/trilium-next-desktop/package.nix b/pkgs/by-name/tr/trilium-next-desktop/package.nix new file mode 100644 index 0000000000000..50ae15afeabd0 --- /dev/null +++ b/pkgs/by-name/tr/trilium-next-desktop/package.nix @@ -0,0 +1,129 @@ +{ + stdenv, + lib, + unzip, + fetchurl, + fetchzip, + makeBinaryWrapper, + # use specific electron since it has to load a compiled module + electron_31, + autoPatchelfHook, + makeDesktopItem, + copyDesktopItems, + wrapGAppsHook3, + asar, +}: + +let + pname = "trilium-next-desktop"; + version = "0.90.12"; + + linuxSource.url = "https://github.com/TriliumNext/Notes/releases/download/v${version}/TriliumNextNotes-v${version}-linux-x64.zip"; + linuxSource.sha256 = "0ji28l60wyzhjbi6g5845dnm763bvg7535zfgzcmfgwjs6zr6nfq"; + + darwinSource.url = "https://github.com/TriliumNext/Notes/releases/download/v${version}/TriliumNextNotes-v${version}-macos-x64.zip"; + darwinSource.sha256 = "0jv80k7dk6gpyfj36iin6y7fk7qan4bya72f14jcgfla95wvk6ls"; + + meta = { + description = "Hierarchical note taking application with focus on building large personal knowledge bases"; + homepage = "https://github.com/TriliumNext/Notes"; + license = lib.licenses.agpl3Plus; + sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ]; + maintainers = with lib.maintainers; [ + eliandoran + fliegendewurst + ]; + mainProgram = "trilium"; + platforms = [ + "x86_64-linux" + "x86_64-darwin" + ]; + }; + + linux = stdenv.mkDerivation rec { + inherit pname version meta; + + src = fetchurl linuxSource; + + # Remove trilium-portable.sh, so trilium knows it is packaged making it stop auto generating a desktop item on launch + postPatch = '' + rm ./trilium-portable.sh + ''; + + nativeBuildInputs = [ + unzip + makeBinaryWrapper + wrapGAppsHook3 + copyDesktopItems + autoPatchelfHook + asar + ]; + + buildInputs = [ + (lib.getLib stdenv.cc.cc) + ]; + + desktopItems = [ + (makeDesktopItem { + name = "Trilium"; + exec = "trilium"; + icon = "trilium"; + comment = meta.description; + desktopName = "TriliumNext Notes"; + categories = [ "Office" ]; + startupWMClass = "Trilium Notes Next"; + }) + ]; + + installPhase = '' + runHook preInstall + mkdir -p "$out/bin" + mkdir -p "$out/share/trilium" + mkdir -p "$out/share/icons/hicolor/512x512/apps" + + cp -r ./* "$out/share/trilium/" + rm $out/share/trilium/{*.so*,trilium,chrome_crashpad_handler,chrome-sandbox} + + # Rebuild the ASAR archive, hardcoding the resourcesPath + tmp=$(mktemp -d) + asar extract $out/share/trilium/resources/app.asar $tmp + rm $out/share/trilium/resources/app.asar + + for f in "src/services/utils.ts" "dist/src/services/utils.js"; do + substituteInPlace $tmp/$f \ + --replace-fail "process.resourcesPath" "'$out/share/trilium/resources'" + done + autoPatchelf $tmp + cp $tmp/src/public/icon.png $out/share/icons/hicolor/512x512/apps/trilium.png + + asar pack $tmp/ $out/share/trilium/resources/app.asar + rm -rf $tmp + + makeWrapper ${lib.getExe electron_31} $out/bin/trilium \ + "''${gappsWrapperArgs[@]}" \ + --set-default ELECTRON_IS_DEV 0 \ + --add-flags $out/share/trilium/resources/app.asar + + runHook postInstall + ''; + + dontWrapGApps = true; + + passthru.updateScript = ./update.sh; + }; + + darwin = stdenv.mkDerivation { + inherit pname version meta; + + src = fetchzip darwinSource; + + installPhase = '' + runHook preInstall + mkdir -p $out/Applications + cp -r *.app $out/Applications + runHook postInstall + ''; + }; + +in +if stdenv.hostPlatform.isDarwin then darwin else linux diff --git a/pkgs/by-name/tr/trilium-next-desktop/update.sh b/pkgs/by-name/tr/trilium-next-desktop/update.sh new file mode 100755 index 0000000000000..763e62c467c22 --- /dev/null +++ b/pkgs/by-name/tr/trilium-next-desktop/update.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -p coreutils curl jq +set -euo pipefail + +cd $(dirname "${BASH_SOURCE[0]}") + +setKV () { + sed -i "s|$2 = \".*\"|$2 = \"${3:-}\"|" $1 +} + +version=$(curl -s --show-error "https://api.github.com/repos/TriliumNext/Notes/releases/latest" | jq -r '.tag_name' | tail -c +2) +setKV ./package.nix version $version + +# Update desktop application +sha256_linux64=$(nix-prefetch-url --quiet https://github.com/TriliumNext/Notes/releases/download/v${version}/TriliumNextNotes-v${version}-linux-x64.zip) +sha256_darwin64=$(nix-prefetch-url --quiet https://github.com/TriliumNext/Notes/releases/download/v${version}/TriliumNextNotes-v${version}-macos-x64.zip) +setKV ./package.nix linuxSource.sha256 $sha256_linux64 +setKV ./package.nix darwinSource.sha256 $sha256_darwin64 + +# Update server +sha256_linux64_server=$(nix-prefetch-url --quiet https://github.com/TriliumNext/Notes/releases/download/v${version}/TriliumNextNotes-v${version}-server-linux-x64.tar.xz) +setKV ../trilium-next-server/package.nix version $version +setKV ../trilium-next-server/package.nix serverSource.sha256 $sha256_linux64_server diff --git a/pkgs/by-name/tr/trilium-next-server/0001-Use-console-logger-instead-of-rolling-files.patch b/pkgs/by-name/tr/trilium-next-server/0001-Use-console-logger-instead-of-rolling-files.patch new file mode 100644 index 0000000000000..871076a76331e --- /dev/null +++ b/pkgs/by-name/tr/trilium-next-server/0001-Use-console-logger-instead-of-rolling-files.patch @@ -0,0 +1,79 @@ +diff --git a/src/services/log.js b/src/services/log.js +index 2840c185a..7fb16dd08 100644 +--- a/src/services/log.js ++++ b/src/services/log.js +@@ -1,45 +1,12 @@ + "use strict"; +-import fs from "fs"; +-import dataDir from "./data_dir.js"; + import cls from "./cls.js"; +-if (!fs.existsSync(dataDir.LOG_DIR)) { +- fs.mkdirSync(dataDir.LOG_DIR, 0o700); +-} +-let logFile; + const SECOND = 1000; + const MINUTE = 60 * SECOND; +-const HOUR = 60 * MINUTE; +-const DAY = 24 * HOUR; +-const NEW_LINE = process.platform === "win32" ? '\r\n' : '\n'; +-let todaysMidnight; +-initLogFile(); +-function getTodaysMidnight() { +- const now = new Date(); +- return new Date(now.getFullYear(), now.getMonth(), now.getDate()); +-} +-function initLogFile() { +- todaysMidnight = getTodaysMidnight(); +- const path = `${dataDir.LOG_DIR}/trilium-${formatDate()}.log`; +- if (logFile) { +- logFile.end(); +- } +- logFile = fs.createWriteStream(path, { flags: 'a' }); +-} +-function checkDate(millisSinceMidnight) { +- if (millisSinceMidnight >= DAY) { +- initLogFile(); +- millisSinceMidnight -= DAY; +- } +- return millisSinceMidnight; +-} + function log(str) { + const bundleNoteId = cls.get("bundleNoteId"); + if (bundleNoteId) { + str = `[Script ${bundleNoteId}] ${str}`; +- } +- let millisSinceMidnight = Date.now() - todaysMidnight.getTime(); +- millisSinceMidnight = checkDate(millisSinceMidnight); +- logFile.write(`${formatTime(millisSinceMidnight)} ${str}${NEW_LINE}`); ++ } + console.log(str); + } + function info(message) { +@@ -61,27 +28,6 @@ function request(req, res, timeMs, responseLength = "?") { + info((timeMs >= 10 ? "Slow " : "") + + `${res.statusCode} ${req.method} ${req.url} with ${responseLength} bytes took ${timeMs}ms`); + } +-function pad(num) { +- num = Math.floor(num); +- return num < 10 ? (`0${num}`) : num.toString(); +-} +-function padMilli(num) { +- if (num < 10) { +- return `00${num}`; +- } +- else if (num < 100) { +- return `0${num}`; +- } +- else { +- return num.toString(); +- } +-} +-function formatTime(millisSinceMidnight) { +- return `${pad(millisSinceMidnight / HOUR)}:${pad((millisSinceMidnight % HOUR) / MINUTE)}:${pad((millisSinceMidnight % MINUTE) / SECOND)}.${padMilli(millisSinceMidnight % SECOND)}`; +-} +-function formatDate() { +- return `${pad(todaysMidnight.getFullYear())}-${pad(todaysMidnight.getMonth() + 1)}-${pad(todaysMidnight.getDate())}`; +-} + export default { + info, + error, diff --git a/pkgs/by-name/tr/trilium-next-server/package.nix b/pkgs/by-name/tr/trilium-next-server/package.nix new file mode 100644 index 0000000000000..4c29d71ebd4d1 --- /dev/null +++ b/pkgs/by-name/tr/trilium-next-server/package.nix @@ -0,0 +1,63 @@ +{ + stdenv, + lib, + autoPatchelfHook, + fetchurl, + makeBinaryWrapper, +}: + +let + version = "0.90.12"; + + serverSource.url = "https://github.com/TriliumNext/Notes/releases/download/v${version}/TriliumNextNotes-v${version}-server-linux-x64.tar.xz"; + serverSource.sha256 = "0gvb01cj334n805rs230xwwcv4rf2z2giikpagw8wqrs54gy3b35"; +in +stdenv.mkDerivation { + pname = "trilium-next-server"; + inherit version; + + src = fetchurl serverSource; + + patches = [ + # patch logger to use console instead of rolling files + ./0001-Use-console-logger-instead-of-rolling-files.patch + ]; + + nativeBuildInputs = [ + autoPatchelfHook + makeBinaryWrapper + ]; + + buildInputs = [ + (lib.getLib stdenv.cc.cc) + ]; + + dontConfigure = true; + dontBuild = true; + + installPhase = '' + runHook preInstall + mkdir -p "$out/share/trilium-server" + + cp -r ./* "$out/share/trilium-server/" + + makeWrapper "$out/share/trilium-server/node/bin/node" "$out/bin/trilium-server" \ + --chdir "$out/share/trilium-server" \ + --add-flags "src/main" + + runHook postInstall + ''; + + meta = { + description = "Hierarchical note taking application with focus on building large personal knowledge bases"; + homepage = "https://github.com/TriliumNext/Notes"; + license = lib.licenses.agpl3Plus; + sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ]; + platforms = [ "x86_64-linux" ]; + maintainers = with lib.maintainers; [ + eliandoran + fliegendewurst + ]; + mainProgram = "trilium-server"; + }; +} From 5ceb97ecbbe1cbb01b1db9b147aabdf6f2fc9f85 Mon Sep 17 00:00:00 2001 From: FliegendeWurst Date: Wed, 15 Jan 2025 22:52:11 +0100 Subject: [PATCH 3/3] trilium-next-desktop: fix Darwin build (cherry picked from commit e6cf3e6db33f131e7caba1c4d5d489c65f2cfcae) --- pkgs/by-name/tr/trilium-next-desktop/package.nix | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkgs/by-name/tr/trilium-next-desktop/package.nix b/pkgs/by-name/tr/trilium-next-desktop/package.nix index 50ae15afeabd0..91663165f311a 100644 --- a/pkgs/by-name/tr/trilium-next-desktop/package.nix +++ b/pkgs/by-name/tr/trilium-next-desktop/package.nix @@ -115,12 +115,16 @@ let darwin = stdenv.mkDerivation { inherit pname version meta; - src = fetchzip darwinSource; + src = fetchurl darwinSource; + + nativeBuildInputs = [ + unzip + ]; installPhase = '' runHook preInstall - mkdir -p $out/Applications - cp -r *.app $out/Applications + mkdir -p "$out/Applications/TriliumNext Notes.app" + cp -r * "$out/Applications/TriliumNext Notes.app/" runHook postInstall ''; };