1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 fetchurl,
6 autoPatchelfHook,
7 makeWrapper,
8 nodejs,
9 pnpm_9,
10 testers,
11}:
12let
13 go-turbo-version = "1.7.4";
14 go-turbo-srcs = {
15 x86_64-linux = fetchurl {
16 url = "https://registry.npmjs.org/turbo-linux-64/-/turbo-linux-64-${go-turbo-version}.tgz";
17 hash = "sha256-bwi+jthoDe+SEvCPPNNNv9AR8n5IA1fc4I8cnfC095Y=";
18 };
19 aarch64-linux = fetchurl {
20 url = "https://registry.npmjs.org/turbo-linux-arm64/-/turbo-linux-arm64-${go-turbo-version}.tgz";
21 hash = "sha256-j3mUd3x16tYR3QQweIB07IbCKYuKPeEkKkUHhrpHzyc=";
22 };
23 };
24 go-turbo = stdenv.mkDerivation {
25 pname = "go-turbo";
26 version = go-turbo-version;
27 src =
28 go-turbo-srcs.${stdenv.hostPlatform.system}
29 or (throw "Unsupported system: ${stdenv.hostPlatform.system}");
30 nativeBuildInputs = [ autoPatchelfHook ];
31 dontBuild = true;
32 installPhase = ''
33 install -Dm755 bin/go-turbo -t $out/bin
34 '';
35 };
36in
37
38stdenv.mkDerivation (finalAttrs: {
39 pname = "zenn-cli";
40 version = "0.2.1";
41
42 src = fetchFromGitHub {
43 owner = "zenn-dev";
44 repo = "zenn-editor";
45 tag = finalAttrs.version;
46 hash = "sha256-LFbC3GXYLCgBOYBdNJRdESGb37DuGBCEL8ak1Qf9DVA=";
47 # turborepo requires .git directory
48 leaveDotGit = true;
49 };
50
51 nativeBuildInputs = [
52 nodejs
53 pnpm_9.configHook
54 makeWrapper
55 ];
56
57 pnpmDeps = pnpm_9.fetchDeps {
58 inherit (finalAttrs) pname version src;
59 fetcherVersion = 1;
60 hash = "sha256-AjdXclrNl1AHJ4LXq9I5Rk6KGyDaWXW187o2uLwRy/o=";
61 };
62
63 preBuild = ''
64 echo VITE_EMBED_SERVER_ORIGIN="https://embed.zenn.studio" > packages/zenn-cli/.env
65 ''
66 # replace go-turbo since the existing one can't be executed
67 + lib.optionalString stdenv.hostPlatform.isLinux ''
68 cp ${go-turbo}/bin/go-turbo node_modules/.pnpm/turbo-linux-*/node_modules/turbo-linux*/bin/go-turbo
69 '';
70
71 buildPhase = ''
72 runHook preBuild
73
74 pnpm build --no-daemon
75
76 runHook postBuild
77 '';
78
79 installPhase = ''
80 runHook preInstall
81
82 mkdir -p $out/{bin,lib/node_modules/zenn-cli}
83 cp -r packages/zenn-cli/{dist,LICENSE,package.json,README.md} $out/lib/node_modules/zenn-cli
84
85 makeWrapper "${lib.getExe nodejs}" "$out/bin/zenn" \
86 --add-flags "$out/lib/node_modules/zenn-cli/dist/server/zenn.js"
87
88 runHook postInstall
89 '';
90
91 passthru = {
92 tests.version = testers.testVersion { package = finalAttrs.finalPackage; };
93 };
94
95 meta = {
96 description = "Preview Zenn content locally";
97 homepage = "https://github.com/zenn-dev/zenn-editor";
98 changelog = "https://github.com/zenn-dev/zenn-editor/releases/tag/${finalAttrs.version}";
99 license = lib.licenses.mit;
100 maintainers = with lib.maintainers; [ natsukium ];
101 mainProgram = "zenn";
102 platforms = nodejs.meta.platforms;
103 };
104})