forked from TriliumNext/Trilium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
399 lines (359 loc) · 15 KB
/
Copy pathflake.nix
File metadata and controls
399 lines (359 loc) · 15 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
{
description = "Trilium Notes (experimental flake)";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
pnpm2nix = {
url = "github:FliegendeWurst/pnpm2nix-nzbr/main";
inputs = {
flake-utils.follows = "flake-utils";
nixpkgs.follows = "nixpkgs";
};
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
pnpm2nix,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs { inherit system; };
electronVersion = packageJsonDesktop.devDependencies.electron;
electronFromNixpkgs = pkgs."electron_${lib.versions.major electronVersion}";
# nixpkgs lags behind the Electron version pinned in apps/desktop/package.json
# (electron_43 is still 43.1.0), and its source build cannot be bumped without
# upstream's Chromium dependency hashes. Build the exact pinned version from
# Electron's official binary release instead, reusing the nixpkgs builder.
#
# When bumping Electron, refresh these hashes:
# zips: curl -sL https://github.com/electron/electron/releases/download/v<version>/SHASUMS256.txt
# headers: nix-prefetch-url --unpack https://artifacts.electronjs.org/headers/dist/v<version>/node-v<version>-headers.tar.gz
pinnedElectronVersion = "43.2.0";
pinnedElectronHashes = {
x86_64-linux = "f77ca6ed67bbc68702b69b56ad499bca6ae090705ade7d04f0ac545e409dec68";
armv7l-linux = "e5cf445bda3ef071bbe9f16dfbabbca61b24b528f2dcbdff227e98fb349c99e8";
aarch64-linux = "50e1cdefbf8590e0d89b0276314a99c7b98e8eed732204c6f1a1c2a38376ed87";
x86_64-darwin = "1349ff423539cfe2b3edf1b14111e618db234d9ba761cbe97ea549edcb2e7a98";
aarch64-darwin = "ad4a0ae3c37ee05aa06c7e2ed0627608389790f0505a2b0d20319efbe33ffe28";
headers = "0jxrbsi1ipzkq3ah7vbqd3glzd423603ii1d1i7kabirxmxra5kk";
};
mkElectronBin = pkgs.callPackage (
pkgs.path + "/pkgs/development/tools/electron/binary/generic.nix"
) { };
electron =
if electronFromNixpkgs.version == electronVersion then
electronFromNixpkgs
else
lib.throwIf (pinnedElectronVersion != electronVersion) ''
flake.nix pins Electron ${pinnedElectronVersion}, but apps/desktop/package.json wants ${electronVersion}.
Refresh pinnedElectronVersion/pinnedElectronHashes in flake.nix, or drop the override if nixpkgs ships ${electronVersion}.
'' (mkElectronBin pinnedElectronVersion pinnedElectronHashes);
nodejs = pkgs.nodejs_24;
# pnpm creates an overly long PATH env variable for child processes.
# This patch deduplicates entries in PATH, which results in an equivalent but shorter entry.
# https://github.com/pnpm/pnpm/issues/6106
# https://github.com/pnpm/pnpm/issues/8552
pnpm = (pkgs.pnpm_11.overrideAttrs (prev: {
postInstall = prev.postInstall + ''
patch $out/libexec/pnpm/dist/pnpm.mjs ${./patches/pnpm-PATH-reduction.patch}
'';
# pnpm sometimes fails with ERR_PNPM_ENOENT
# https://github.com/pnpm/pnpm/issues/12880
# "fix" from https://github.com/dniku/selfhostblocks/commit/ee6fc4f04fe34714fb8676704048d8d76aba85b7
postFixup = (prev.postFixup or "") + (if pkgs.stdenv.hostPlatform.isLinux then ''
mv "$out/bin/pnpm" "$out/bin/.pnpm-unwrapped"
cat > "$out/bin/pnpm" <<EOF
#!${stdenv.shell}
if [ "\''${1-}" = install ]; then
cpuMask="\$(${lib.getExe' pkgs.util-linux "taskset"} -cp "\$\$")"
# Keep only the cpuset reported after the colon.
cpuMask="\''${cpuMask##*: }"
# Pick the first comma-separated segment from the cpuset.
firstCpu="\''${cpuMask%%,*}"
# If that segment is a range, pick its first CPU.
firstCpu="\''${firstCpu%%-*}"
exec ${lib.getExe' pkgs.util-linux "taskset"} -c "\$firstCpu" "$out/bin/.pnpm-unwrapped" "\$@"
else
exec "$out/bin/.pnpm-unwrapped" "\$@"
fi
EOF
chmod +x "$out/bin/pnpm"
'' else "");
}));
inherit (pkgs)
copyDesktopItems
darwin
lib
makeBinaryWrapper
makeDesktopItem
makeShellWrapper
removeReferencesTo
stdenv
wrapGAppsHook3
xcodebuild
which
;
fullCleanSourceFilter =
name: type:
(lib.cleanSourceFilter name type)
&& (
let
baseName = baseNameOf (toString name);
in
# No need to copy the flake.
# No need to copy local copy of node_modules.
baseName != "flake.nix" && baseName != "flake.lock" && baseName != "node_modules"
);
fullCleanSource =
src:
lib.cleanSourceWith {
filter = fullCleanSourceFilter;
src = src;
};
# Minimal source used for pnpm2nix's dependency-fetching derivation.
# Only the files pnpm actually needs to resolve and fetch dependencies
# are included, so unrelated source changes don't bust the deps cache.
workspaceSourceFilter =
name: type:
let
baseName = baseNameOf (toString name);
rootStr = toString ./.;
relPath = lib.removePrefix "${rootStr}/" (toString name);
inPatches = relPath == "patches" || lib.hasPrefix "patches/" relPath;
in
(lib.cleanSourceFilter name type)
&& baseName != "node_modules"
&& (
type == "directory"
|| baseName == "package.json"
|| baseName == "pnpm-workspace.yaml"
|| baseName == "pnpm-lock.yaml"
|| inPatches
);
workspaceSource = lib.cleanSourceWith {
filter = workspaceSourceFilter;
src = ./.;
};
packageJson = builtins.fromJSON (builtins.readFile ./package.json);
packageJsonDesktop = builtins.fromJSON (builtins.readFile ./apps/desktop/package.json);
makeApp =
{
app,
buildTask,
mainProgram,
installCommands,
preBuildCommands ? "",
}:
pnpm2nix.packages.${system}.mkPnpmPackage rec {
pname = "trilium-${app}";
version = packageJson.version + (lib.optionalString (self ? shortRev) "-${self.shortRev}");
src = fullCleanSource ./.;
packageJSON = ./package.json;
pnpmLockYaml = ./pnpm-lock.yaml;
workspace = workspaceSource;
pnpmWorkspaceYaml = ./pnpm-workspace.yaml;
inherit nodejs pnpm;
extraNodeModuleSources = [
rec {
name = "patches";
value = ./patches;
}
];
# avoid ERR_PNPM_RESOLUTION_SHAPE_MISMATCH errors
env.PNPM_CONFIG_TRUST_LOCKFILE = "true";
# remove pnpm version override
preConfigure = ''
node -e "const p = require('./package.json'); delete p.packageManager; require('fs').writeFileSync('package.json', JSON.stringify(p, null, 2) + '\n')"
'';
postConfigure =
''
chmod +x node_modules/electron/install.js
patchShebangs --build node_modules
'';
extraNativeBuildInputs =
[
nodejs.python
removeReferencesTo
]
++ lib.optionals (app == "desktop" || app == "edit-docs") [
copyDesktopItems
# required for NIXOS_OZONE_WL expansion
# https://github.com/NixOS/nixpkgs/issues/172583
makeShellWrapper
wrapGAppsHook3
# For the launcher wrapper generated in installCommands:
which
electron
]
++ lib.optionals (app == "server" || app == "build-docs") [
makeBinaryWrapper
]
++ lib.optionals stdenv.hostPlatform.isDarwin [
xcodebuild
darwin.cctools
];
dontWrapGApps = true;
preBuild = ''
${preBuildCommands}
'';
scriptFull = "pnpm run ${buildTask}";
installPhase = ''
runHook preInstall
${installCommands}
runHook postInstall
'';
# Symlinks pointing to /build directory are not allowed in the Nix store.
# This removes all dangling symlinks that point to the temporary build directory.
postFixup = ''
find $out/opt -type l -lname '/build/*' -delete || true
'';
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/build-docs"
"apps/client"
"apps/db-compare"
"apps/desktop"
"apps/dump-db"
"apps/edit-docs"
"apps/server"
"packages/trilium-e2e"
];
desktopItems = lib.optionals (app == "desktop") [
(makeDesktopItem {
name = "Trilium Notes";
exec = meta.mainProgram;
icon = "trilium";
comment = meta.description;
desktopName = "Trilium Notes";
categories = [ "Office" ];
startupWMClass = "Trilium Notes";
})
];
meta = {
description = "Trilium: ${app}";
inherit mainProgram;
};
};
desktop = makeApp {
app = "desktop";
# better-sqlite3 v13 is N-API based and ships prebuilt binaries that
# load unchanged under Electron, so there is no native rebuild step
# (and no need for ELECTRON_NODEDIR) any more.
preBuildCommands = ''
pnpm postinstall
'';
buildTask = "desktop:build";
mainProgram = "trilium";
installCommands = ''
mkdir -p $out/{bin,share/icons/hicolor/512x512/apps,opt/trilium}
cp --archive apps/desktop/dist/* $out/opt/trilium
cp apps/client/src/assets/icon.png $out/share/icons/hicolor/512x512/apps/trilium.png
makeShellWrapper ${lib.getExe electron} $out/bin/trilium \
"''${gappsWrapperArgs[@]}" \
--add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}" \
--set-default ELECTRON_IS_DEV 0 \
--set TRILIUM_RESOURCE_DIR $out/opt/trilium \
--add-flags $out/opt/trilium/main.cjs
'';
};
server = makeApp {
app = "server";
# Important note: if pnpm throws an error similar to the follow at the end of `pnpm rebuild`,
# you should ensure the version of tsx (and other dependencies mentioned in the error) is identical project-wide.
# ERR_PNPM_MISSING_HOISTED_LOCATIONS
# vite@7.1.5(@types/node@24.3.0)(jiti@2.5.1)(less@4.1.3)(lightningcss@1.30.1)
# (sass-embedded@1.91.0)(sass@1.91.0)(terser@5.43.1)(tsx@4.20.5)(yaml@2.8.1)
# is not found in hoistedLocations inside node_modules/.modules.yaml
preBuildCommands = ''
pushd apps/server
pnpm rebuild
popd
'';
buildTask = "server:build";
mainProgram = "trilium-server";
installCommands = ''
# No better-sqlite3 cleanup needed here any more: the server build
# trims deps/, src/ and build/ (which held the store paths that used
# to need remove-references-to) out of dist itself.
mkdir -p $out/{bin,opt/trilium-server}
cp --archive apps/server/dist/* $out/opt/trilium-server
makeWrapper ${lib.getExe nodejs} $out/bin/trilium-server \
--add-flags $out/opt/trilium-server/main.cjs
'';
};
edit-docs = makeApp {
app = "edit-docs";
preBuildCommands = ''
pnpm postinstall
'';
buildTask = "edit-docs:build";
mainProgram = "trilium-edit-docs";
installCommands = ''
mkdir -p $out/{bin,opt/trilium-edit-docs}
cp --archive apps/edit-docs/dist/* $out/opt/trilium-edit-docs
makeShellWrapper ${lib.getExe electron} $out/bin/trilium-edit-docs \
--set-default ELECTRON_IS_DEV 0 \
--set TRILIUM_RESOURCE_DIR $out/opt/trilium-edit-docs \
--add-flags $out/opt/trilium-edit-docs/edit-docs.cjs
'';
};
build-docs = makeApp {
app = "build-docs";
preBuildCommands = ''
pushd apps/server
pnpm rebuild || true
popd
'';
buildTask = "client:build && pnpm run server:build && pnpm run --filter build-docs build";
mainProgram = "trilium-build-docs";
installCommands = ''
mkdir -p $out/{bin,opt/trilium-build-docs}
# Copy build-docs dist
cp --archive apps/build-docs/dist/* $out/opt/trilium-build-docs
# Copy server dist (needed for runtime)
mkdir -p $out/opt/trilium-build-docs/server
cp --archive apps/server/dist/* $out/opt/trilium-build-docs/server/
# Copy client dist (needed for runtime)
mkdir -p $out/opt/trilium-build-docs/client
cp --archive apps/client/dist/* $out/opt/trilium-build-docs/client/
# Copy share-theme (needed for exports)
mkdir -p $out/opt/trilium-build-docs/packages/share-theme
cp --archive packages/share-theme/dist/* $out/opt/trilium-build-docs/packages/share-theme/
# Create wrapper script
makeWrapper ${lib.getExe nodejs} $out/bin/trilium-build-docs \
--add-flags $out/opt/trilium-build-docs/cli.cjs \
--set TRILIUM_RESOURCE_DIR $out/opt/trilium-build-docs/server
'';
};
in
{
packages.desktop = desktop;
packages.server = server;
packages.edit-docs = edit-docs;
packages.build-docs = build-docs;
packages.default = desktop;
devShells.default = pkgs.mkShell {
buildInputs = [
nodejs
pnpm
electron
nodejs.python
];
};
}
);
}