refactor(flake): use pnpm2nix, add server package

This commit is contained in:
FliegendeWurst 2025-06-02 17:55:43 +02:00
parent cf24308cb0
commit d86337a993
2 changed files with 211 additions and 62 deletions

57
flake.lock generated
View File

@ -18,6 +18,24 @@
"type": "github" "type": "github"
} }
}, },
"flake-utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1701680307,
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1748437600, "lastModified": 1748437600,
@ -34,10 +52,32 @@
"type": "github" "type": "github"
} }
}, },
"pnpm2nix": {
"inputs": {
"flake-utils": "flake-utils_2",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1748881470,
"narHash": "sha256-Uk4J9StEEH/JaJQ+cw7wVvgLPMLZNoOGDGxKSbSDXyc=",
"owner": "FliegendeWurst",
"repo": "pnpm2nix-nzbr",
"rev": "85497400540854c2895b9f59b5cc741a1f838da2",
"type": "github"
},
"original": {
"owner": "FliegendeWurst",
"repo": "pnpm2nix-nzbr",
"type": "github"
}
},
"root": { "root": {
"inputs": { "inputs": {
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs",
"pnpm2nix": "pnpm2nix"
} }
}, },
"systems": { "systems": {
@ -54,6 +94,21 @@
"repo": "default", "repo": "default",
"type": "github" "type": "github"
} }
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
} }
}, },
"root": "root", "root": "root",

216
flake.nix
View File

@ -4,6 +4,10 @@
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
pnpm2nix = {
url = "github:FliegendeWurst/pnpm2nix-nzbr";
inputs.nixpkgs.follows = "nixpkgs";
};
}; };
outputs = outputs =
@ -11,69 +15,164 @@
self, self,
nixpkgs, nixpkgs,
flake-utils, flake-utils,
pnpm2nix,
}: }:
flake-utils.lib.eachDefaultSystem ( flake-utils.lib.eachDefaultSystem (
system: system:
let let
packageJSON = builtins.fromJSON (builtins.readFile ./package.json);
pkgs = import nixpkgs { inherit system; }; pkgs = import nixpkgs { inherit system; };
electron = pkgs.electron_35; electron = pkgs.electron_35;
nodejs = pkgs.nodejs_22;
pnpm = pkgs.pnpm_10;
inherit (pkgs) inherit (pkgs)
copyDesktopItems copyDesktopItems
darwin
lib lib
makeBinaryWrapper makeBinaryWrapper
makeDesktopItem makeDesktopItem
nodejs removeReferencesTo
pnpm
stdenv stdenv
wrapGAppsHook3 wrapGAppsHook3
xcodebuild xcodebuild
darwin
; ;
desktop = stdenv.mkDerivation (finalAttrs: {
pname = "triliumnext-desktop";
version = packageJSON.version;
src = lib.cleanSource ./.;
nativeBuildInputs = fullCleanSourceFilter =
[ name: type:
pnpm.configHook (lib.cleanSourceFilter name type)
nodejs || (
nodejs.python let
copyDesktopItems baseName = baseNameOf (toString name);
makeBinaryWrapper in
wrapGAppsHook3 # No need to copy the flake.
] # Don't copy local development instance of NX cache.
++ lib.optionals stdenv.hostPlatform.isDarwin [ baseName == "flake.nix" || baseName == "flake.lock" || baseName == ".nx"
xcodebuild );
darwin.cctools fullCleanSource =
src:
lib.cleanSourceWith {
filter = fullCleanSourceFilter;
src = src;
};
packageJson = builtins.fromJSON (builtins.readFile ./package.json);
makeApp =
{
app,
buildTask,
mainProgram,
installCommands,
preBuildCommands ? "",
}:
pnpm2nix.packages.${system}.mkPnpmPackage rec {
pname = "triliumnext-${app}";
version = packageJson.version + (lib.optionalString (self ? shortRev) "-${self.shortRev}");
src = fullCleanSource ./.;
packageJSON = ./package.json;
pnpmLockYaml = ./pnpm-lock.yaml;
workspace = fullCleanSource ./.;
pnpmWorkspaceYaml = ./pnpm-workspace.yaml;
inherit nodejs pnpm;
extraNodeModuleSources = [
rec {
name = "patches";
value = ./patches;
}
]; ];
dontWrapGApps = true; postConfigure =
''
chmod +x node_modules/.pnpm/electron@*/node_modules/electron/install.js
patchShebangs --build node_modules
''
+ lib.optionalString stdenv.hostPlatform.isLinux ''
patchelf --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \
node_modules/.pnpm/sass-embedded-linux-x64@*/node_modules/sass-embedded-linux-x64/dart-sass/src/dart
'';
preBuild = lib.optionalString stdenv.hostPlatform.isLinux '' extraNativeBuildInputs =
patchelf --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \ [
node_modules/.pnpm/sass-embedded-linux-x64@*/node_modules/sass-embedded-linux-x64/dart-sass/src/dart makeBinaryWrapper
''; nodejs.python
removeReferencesTo
]
++ lib.optionals (app == "desktop") [
copyDesktopItems
wrapGAppsHook3
]
++ lib.optionals stdenv.hostPlatform.isDarwin [
xcodebuild
darwin.cctools
];
dontWrapGApps = true;
buildPhase = '' env.ELECTRON_SKIP_BINARY_DOWNLOAD = "1";
runHook preBuild
# Disable NX interaction preBuild = ''
export NX_TUI=false ${preBuildCommands}
export NX_DAEMON=false '';
pnpm nx run desktop:build --outputStyle stream --verbose scriptFull = "pnpm nx ${buildTask} --outputStyle stream --verbose";
# Rebuild dependencies installPhase = ''
export npm_config_nodedir=${electron.headers} runHook preInstall
pnpm nx run desktop:rebuild-deps --outputStyle stream --verbose
runHook postBuild ${installCommands}
'';
installPhase = '' runHook postInstall
runHook preInstall '';
components = [
"packages/ckeditor5"
"packages/ckeditor5-admonition"
"packages/ckeditor5-footnotes"
"packages/ckeditor5-keyboard-marker"
"packages/ckeditor5-math"
"packages/ckeditor5-mermaid"
"packages/codemirror"
"packages/commons"
"packages/express-partial-content"
"packages/highlightjs"
"packages/turndown-plugin-gfm"
"apps/client"
"apps/db-compare"
"apps/desktop"
"apps/dump-db"
"apps/edit-docs"
"apps/server"
"apps/server-e2e"
];
desktopItems = lib.optionals (app == "desktop") [
(makeDesktopItem {
name = "TriliumNext Notes";
exec = meta.mainProgram;
icon = "trilium";
comment = meta.description;
desktopName = "TriliumNext Notes";
categories = [ "Office" ];
startupWMClass = "Trilium Notes Next";
})
];
meta = {
description = "TriliumNext: ${app}";
inherit mainProgram;
};
};
desktop = makeApp {
app = "desktop";
preBuildCommands = "export npm_config_nodedir=${electron.headers}";
buildTask = "run desktop:rebuild-deps";
mainProgram = "trilium";
installCommands = ''
remove-references-to -t ${electron.headers} apps/desktop/dist/node_modules/better-sqlite3/build/config.gypi
remove-references-to -t ${nodejs.python} apps/desktop/dist/node_modules/better-sqlite3/build/config.gypi
mkdir -p $out/{bin,share/icons/hicolor/512x512/apps,opt/trilium} mkdir -p $out/{bin,share/icons/hicolor/512x512/apps,opt/trilium}
cp --archive apps/desktop/dist/* $out/opt/trilium cp --archive apps/desktop/dist/* $out/opt/trilium
@ -82,34 +181,29 @@
"''${gappsWrapperArgs[@]}" \ "''${gappsWrapperArgs[@]}" \
--set-default ELECTRON_IS_DEV 0 \ --set-default ELECTRON_IS_DEV 0 \
--add-flags $out/opt/trilium/main.cjs --add-flags $out/opt/trilium/main.cjs
runHook postInstall
''; '';
};
desktopItems = [ server = makeApp {
(makeDesktopItem { app = "server";
name = "TriliumNext Notes"; preBuildCommands = "pushd apps/server; pnpm rebuild; popd";
exec = finalAttrs.meta.mainProgram; buildTask = "--project=server build";
icon = "trilium"; mainProgram = "trilium-server";
comment = finalAttrs.meta.description; installCommands = ''
desktopName = "TriliumNext Notes"; remove-references-to -t ${nodejs.python} apps/server/dist/node_modules/better-sqlite3/build/config.gypi
categories = [ "Office" ]; remove-references-to -t ${pnpm} apps/server/dist/node_modules/better-sqlite3/build/config.gypi
startupWMClass = "Trilium Notes Next";
})
];
pnpmDeps = pnpm.fetchDeps { mkdir -p $out/{bin,opt/trilium-server}
inherit (finalAttrs) pname version src; cp --archive apps/server/dist/* $out/opt/trilium-server
hash = "sha256-xC0u1h92wtthylOAw+IF9mpFi0c4xajJhUcA9pqzcAw="; makeWrapper ${lib.getExe nodejs} $out/bin/trilium-server \
}; --add-flags $out/opt/trilium-server/main.cjs
'';
meta = { };
description = "Free and open-source, cross-platform hierarchical note taking application with focus on building large personal knowledge bases";
mainProgram = "trilium";
};
});
in in
{ {
packages.desktop = desktop;
packages.server = server;
packages.default = desktop; packages.default = desktop;
} }
); );