biome: 1.9.2 -> 1.9.3
[NixPkgs.git] / pkgs / applications / misc / pagefind / default.nix
blob8caf7c0fbe17da44d9dbb75bad0457b5ccaa5196
2   lib,
3   stdenv,
4   rustPlatform,
5   fetchFromGitHub,
6   fetchNpmDeps,
7   fetchurl,
8   httplz,
9   binaryen,
10   gzip,
11   nodejs,
12   npmHooks,
13   python3,
14   rustc,
15   wasm-bindgen-cli,
16   wasm-pack,
19 let
21   wasm-bindgen-92 = wasm-bindgen-cli.override {
22     version = "0.2.92";
23     hash = "sha256-1VwY8vQy7soKEgbki4LD+v259751kKxSxmo/gqE6yV0=";
24     cargoHash = "sha256-aACJ+lYNEU8FFBs158G1/JG8sc6Rq080PeKCMnwdpH0=";
25   };
27   # the lindera-unidic v0.32.2 crate uses [1] an outdated unidic-mecab fork [2] and builds it in pure rust
28   # [1] https://github.com/lindera/lindera/blob/v0.32.2/lindera-unidic/build.rs#L5-L11
29   # [2] https://github.com/lindera/unidic-mecab
30   lindera-unidic-src = fetchurl {
31     url = "https://dlwqk3ibdg1xh.cloudfront.net/unidic-mecab-2.1.2.tar.gz";
32     hash = "sha256-JKx1/k5E2XO1XmWEfDX6Suwtt6QaB7ScoSUUbbn8EYk=";
33   };
37 rustPlatform.buildRustPackage rec {
38   pname = "pagefind";
39   version = "1.1.1";
41   src = fetchFromGitHub {
42     owner = "cloudcannon";
43     repo = "pagefind";
44     rev = "refs/tags/v${version}";
45     hash = "sha256-4NfosDp5Wwz2lnqaFNcaIbWpjWiaQ4WCL6TcKEkfPck=";
46   };
48   cargoHash = "sha256-hnT9w3j8/YuN00x0LBPr75BKGWSnIYUNFTWIgtghJP4";
50   env.npmDeps_web_js = fetchNpmDeps {
51     name = "npm-deps-web-js";
52     src = "${src}/pagefind_web_js";
53     hash = "sha256-1gdVBCxxLEGFihIxoSSgxw/tMyVgwe7HFG/JjEfYVnQ=";
54   };
55   env.npmDeps_ui_default = fetchNpmDeps {
56     name = "npm-deps-ui-default";
57     src = "${src}/pagefind_ui/default";
58     hash = "sha256-voCs49JneWYE1W9U7aB6G13ypH6JqathVDeF58V57U8=";
59   };
60   env.npmDeps_ui_modular = fetchNpmDeps {
61     name = "npm-deps-ui-modular";
62     src = "${src}/pagefind_ui/modular";
63     hash = "sha256-O0RqZUsRFtByxMQdwNGNcN38Rh+sDqqNo9YlBcrnsF4=";
64   };
65   env.cargoDeps_web = rustPlatform.fetchCargoTarball {
66     name = "cargo-deps-web";
67     src = "${src}/pagefind_web/";
68     hash = "sha256-vDkVXyDePKgYTYE5ZTLLfOHwPYfgaqP9p5/fKCQQi0g=";
69   };
71   postPatch = ''
72     # Tricky way to run npmConfigHook multiple times
73     (
74       local postPatchHooks=() # written to by npmConfigHook
75       source ${npmHooks.npmConfigHook}/nix-support/setup-hook
76       npmRoot=pagefind_web_js     npmDeps=$npmDeps_web_js     npmConfigHook
77       npmRoot=pagefind_ui/default npmDeps=$npmDeps_ui_default npmConfigHook
78       npmRoot=pagefind_ui/modular npmDeps=$npmDeps_ui_modular npmConfigHook
79     )
80     (
81       cd pagefind_web
82       cargoDeps=$cargoDeps_web cargoSetupPostUnpackHook
83       cargoDeps=$cargoDeps_web cargoSetupPostPatchHook
84     )
86     # patch a build-time dependency download
87     (
88       cd $cargoDepsCopy/lindera-unidic
89       oldHash=$(sha256sum build.rs | cut -d " " -f 1)
91       # serve lindera-unidic on localhost vacant port
92       httplz_port="${
93         if stdenv.buildPlatform.isDarwin then
94           ''$(python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')''
95         else
96           "34567"
97       }"
98       mkdir .lindera-http-plz
99       ln -s ${lindera-unidic-src} .lindera-http-plz/unidic-mecab-2.1.2.tar.gz
100       httplz --port "$httplz_port" -- .lindera-http-plz/ &
101       echo $! >$TMPDIR/.httplz_pid
103       # file:// does not work
104       substituteInPlace build.rs --replace-fail \
105           "https://dlwqk3ibdg1xh.cloudfront.net/unidic-mecab-2.1.2.tar.gz" \
106           "http://localhost:$httplz_port/unidic-mecab-2.1.2.tar.gz"
108       newHash=$(sha256sum build.rs | cut -d " " -f 1)
109       substituteInPlace .cargo-checksum.json --replace-fail $oldHash $newHash
110     )
111   '';
113   __darwinAllowLocalNetworking = true;
115   nativeBuildInputs =
116     [
117       binaryen
118       gzip
119       nodejs
120       rustc
121       rustc.llvmPackages.lld
122       wasm-bindgen-92
123       wasm-pack
124       httplz
125     ]
126     ++ lib.optionals stdenv.buildPlatform.isDarwin [
127       python3
128     ];
130   # build wasm and js assets
131   # based on "test-and-build" in https://github.com/CloudCannon/pagefind/blob/main/.github/workflows/release.yml
132   preBuild = ''
133     export HOME=$(mktemp -d)
135     echo entering pagefind_web_js...
136     (
137       cd pagefind_web_js
138       npm run build-coupled
139     )
141     echo entering pagefind_web...
142     (
143       cd pagefind_web
144       bash ./local_build.sh
145     )
147     echo entering pagefind_ui/default...
148     (
149       cd pagefind_ui/default
150       npm run build
151     )
153     echo entering pagefind_ui/modular...
154     (
155       cd pagefind_ui/modular
156       npm run build
157     )
158   '';
160   # the file is also fetched during checkPhase
161   preInstall = ''
162     kill ${lib.optionalString stdenv.hostPlatform.isDarwin "-9"} $(cat $TMPDIR/.httplz_pid)
163   '';
165   buildFeatures = [ "extended" ];
167   meta = {
168     description = "Generate low-bandwidth search index for your static website";
169     homepage = "https://pagefind.app/";
170     license = lib.licenses.mit;
171     maintainers = with lib.maintainers; [ pbsds ];
172     platforms = lib.platforms.unix;
173     mainProgram = "pagefind";
174   };