nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 buildNpmPackage,
3 lib,
4 autoPatchelfHook,
5 electron,
6 fetchFromGitHub,
7 gitUpdater,
8 makeWrapper,
9 ollama,
10 pkg-config,
11 stdenv,
12 vips,
13}:
14
15buildNpmPackage rec {
16 pname = "chatd";
17 version = "1.1.2";
18
19 src = fetchFromGitHub {
20 owner = "BruceMacD";
21 repo = "chatd";
22 rev = "v${version}";
23 hash = "sha256-6z5QoJk81NEP115uW+2ah7vxpDz8XQUmMLESPsZT9uU=";
24 };
25
26 makeCacheWritable = true; # sharp tries to build stuff in node_modules
27 ELECTRON_SKIP_BINARY_DOWNLOAD = true;
28
29 npmDepsHash = "sha256-jvGvhgNhY+wz/DFS7NDtmzKXbhHbNF3i0qVQoFFeB0M=";
30
31 dontNpmBuild = true; # missing script: build
32
33 nativeBuildInputs = [
34 makeWrapper
35 electron
36 pkg-config
37 ]
38 ++ lib.optional stdenv.hostPlatform.isLinux autoPatchelfHook; # for onnx libs
39
40 buildInputs = [
41 (lib.getLib stdenv.cc.cc) # for libstdc++.so, required by onnxruntime
42 vips # or it will try to download from the Internet
43 ];
44
45 installPhase = ''
46 runHook preInstall
47 mkdir -p $out/{bin,share}
48 cp -r . $out/share/chatd
49
50 for bin in ollama-darwin ollama-linux; do
51 makeWrapper ${lib.getExe ollama} $out/share/chatd/src/service/ollama/runners/$bin
52 done
53
54 makeWrapper ${lib.getExe electron} $out/bin/chatd \
55 --add-flags $out/share/chatd/src/index.js \
56 --chdir $out/share/chatd \
57 --set-default ELECTRON_FORCE_IS_PACKAGED 1
58 runHook postInstall
59 '';
60
61 postInstall = ''
62 substituteInPlace $out/share/chatd/node_modules/@xenova/transformers/src/env.js \
63 --replace-fail "import fs from 'fs';" "import fs from 'fs';import os from 'os';" \
64 --replace-fail 'path.dirname(path.dirname(url.fileURLToPath(import.meta.url)))' 'path.join(os.homedir(), ".cache", "chatd")'
65
66 rm -rf $out/share/electron{,-winstaller} $(find $out -name 'win32')
67 find $out/share/chatd/node_modules -name '*.exe' -or -name '*.dll' -or -name '*.pdb' -delete
68 rm -rf ${
69 lib.concatStringsSep " " (
70 (lib.optional (
71 !stdenv.hostPlatform.isx86_64
72 ) "$out/share/chatd/node_modules/onnxruntime-node/bin/napi-v3/*/x64")
73 ++ (lib.optional (
74 !stdenv.hostPlatform.isAarch64
75 ) "$out/share/chatd/node_modules/onnxruntime-node/bin/napi-v3/*/arm64")
76 ++ (lib.optional (
77 !stdenv.hostPlatform.isDarwin
78 ) "$out/share/chatd/node_modules/onnxruntime-node/bin/napi-v3/darwin")
79 ++ (lib.optional (
80 !stdenv.hostPlatform.isLinux
81 ) "$out/share/chatd/node_modules/onnxruntime-node/bin/napi-v3/linux")
82 )
83 }
84 '';
85
86 passthru.updateScript = gitUpdater { rev-prefix = "v"; };
87
88 meta = {
89 description = "Chat with your documents using local AI";
90 homepage = "https://github.com/BruceMacD/chatd";
91 changelog = "https://github.com/BruceMacD/chatd/releases/tag/v${version}";
92 license = lib.licenses.mit;
93 maintainers = [ lib.maintainers.lucasew ];
94 mainProgram = "chatd";
95 platforms = electron.meta.platforms;
96 };
97}