|
1 | 1 | { |
2 | 2 | pkgs, |
3 | 3 | craneLib, |
4 | | - src, |
| 4 | + cargoSrc, |
| 5 | + runtimeAssetsSrc, |
| 6 | + frontendSrc, |
5 | 7 | }: let |
6 | 8 | inherit (pkgs) lib onnxruntime; |
7 | 9 |
|
8 | 10 | # Read version from Cargo.toml |
9 | | - cargoToml = fromTOML (builtins.readFile "${src}/Cargo.toml"); |
| 11 | + cargoToml = fromTOML (builtins.readFile "${cargoSrc}/Cargo.toml"); |
10 | 12 | inherit (cargoToml.package) version; |
11 | 13 |
|
12 | 14 | buildInputs = with pkgs; [ |
|
23 | 25 | cmake |
24 | 26 | ]; |
25 | 27 |
|
| 28 | + frontendPackageLock = lib.importJSON "${frontendSrc}/interface/package-lock.json"; |
| 29 | + frontendPackage = |
| 30 | + let |
| 31 | + originalPackage = lib.importJSON "${frontendSrc}/interface/package.json"; |
| 32 | + rootDependencies = originalPackage.dependencies or {}; |
| 33 | + lockPackages = frontendPackageLock.packages; |
| 34 | + rootDependencyNames = builtins.attrNames rootDependencies; |
| 35 | + |
| 36 | + collectPeerClosure = dependencyNames: |
| 37 | + let |
| 38 | + peerNames = |
| 39 | + lib.unique |
| 40 | + (builtins.concatLists ( |
| 41 | + map ( |
| 42 | + dependencyName: |
| 43 | + let |
| 44 | + packagePath = "node_modules/${dependencyName}"; |
| 45 | + packageEntry = |
| 46 | + if builtins.hasAttr packagePath lockPackages |
| 47 | + then lockPackages.${packagePath} |
| 48 | + else {}; |
| 49 | + in builtins.attrNames (packageEntry.peerDependencies or {}) |
| 50 | + ) |
| 51 | + dependencyNames |
| 52 | + )); |
| 53 | + |
| 54 | + expandedNames = lib.unique (dependencyNames ++ peerNames); |
| 55 | + in |
| 56 | + if builtins.length expandedNames == builtins.length dependencyNames |
| 57 | + then dependencyNames |
| 58 | + else collectPeerClosure expandedNames; |
| 59 | + |
| 60 | + peerDependencyNames = |
| 61 | + lib.filter (dependencyName: !(lib.elem dependencyName rootDependencyNames)) |
| 62 | + (collectPeerClosure rootDependencyNames); |
| 63 | + |
| 64 | + peerDependencyVersions = builtins.listToAttrs ( |
| 65 | + lib.filter (entry: entry != null) ( |
| 66 | + map ( |
| 67 | + dependencyName: |
| 68 | + let |
| 69 | + packagePath = "node_modules/${dependencyName}"; |
| 70 | + in |
| 71 | + if builtins.hasAttr packagePath lockPackages |
| 72 | + then { |
| 73 | + name = dependencyName; |
| 74 | + value = lockPackages.${packagePath}.version; |
| 75 | + } |
| 76 | + else null |
| 77 | + ) |
| 78 | + peerDependencyNames |
| 79 | + ) |
| 80 | + ); |
| 81 | + in |
| 82 | + originalPackage |
| 83 | + // { |
| 84 | + dependencies = rootDependencies // peerDependencyVersions; |
| 85 | + }; |
| 86 | + |
26 | 87 | frontend = pkgs.buildNpmPackage { |
27 | 88 | pname = "spacebot-frontend"; |
28 | 89 | inherit version; |
29 | | - src = "${src}/interface"; |
30 | | - |
31 | | - npmDepsHash = "sha256-J11BrUDbZLKQDnS/+ux7QzK2vfLhtxUSZye0hIWnLPk="; |
| 90 | + src = "${frontendSrc}/interface"; |
| 91 | + |
| 92 | + npmDeps = pkgs.importNpmLock { |
| 93 | + npmRoot = "${frontendSrc}/interface"; |
| 94 | + package = frontendPackage; |
| 95 | + packageLock = frontendPackageLock; |
| 96 | + }; |
| 97 | + npmConfigHook = pkgs.importNpmLock.npmConfigHook; |
32 | 98 | npmInstallFlags = ["--legacy-peer-deps"]; |
33 | 99 | makeCacheWritable = true; |
34 | 100 |
|
|
39 | 105 | }; |
40 | 106 |
|
41 | 107 | commonArgs = { |
42 | | - inherit src nativeBuildInputs buildInputs; |
| 108 | + src = cargoSrc; |
| 109 | + inherit nativeBuildInputs buildInputs; |
43 | 110 | strictDeps = true; |
44 | 111 | cargoExtraArgs = ""; |
45 | 112 | }; |
46 | 113 |
|
| 114 | + dummyRustSource = pkgs.writeText "dummy.rs" '' |
| 115 | + fn main() {} |
| 116 | + ''; |
| 117 | + |
47 | 118 | cargoArtifacts = craneLib.buildDepsOnly (commonArgs |
48 | 119 | // { |
| 120 | + src = craneLib.mkDummySrc { |
| 121 | + src = cargoSrc; |
| 122 | + dummyrs = dummyRustSource; |
| 123 | + dummyBuildrs = "build.rs"; |
| 124 | + extraDummyScript = '' |
| 125 | + cp ${dummyRustSource} $out/build.rs |
| 126 | + ''; |
| 127 | + }; |
49 | 128 | preBuild = '' |
50 | 129 | export ORT_LIB_LOCATION=${onnxruntime}/lib |
51 | 130 | ''; |
|
67 | 146 |
|
68 | 147 | postInstall = '' |
69 | 148 | mkdir -p $out/share/spacebot |
70 | | - cp -r ${src}/prompts $out/share/spacebot/ |
71 | | - cp -r ${src}/migrations $out/share/spacebot/ |
| 149 | + cp -r ${runtimeAssetsSrc}/prompts $out/share/spacebot/ |
| 150 | + cp -r ${runtimeAssetsSrc}/migrations $out/share/spacebot/ |
72 | 151 | chmod -R u+w $out/share/spacebot |
73 | 152 | ''; |
74 | 153 |
|
|
0 commit comments