summaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorDoron Behar <doron.behar@gmail.com>2024-06-06 09:41:22 +0300
committerGitHub <noreply@github.com>2024-06-06 09:41:22 +0300
commit01d57709ecd4939e8788e7560bf7c8d2db897d18 (patch)
treea95805aed1f0f4b2e74089d0eac3285758a1e183 /doc
parent046fa57365234c41f12c4188e30484592ebbf28a (diff)
parentc704c029566aaf525d949b54b8a8d85bb6aab29b (diff)
Merge pull request #290715 from Scrumplex/pkgs/build-support/fetchPnpmDeps
pnpm.fetchDeps: init
Diffstat (limited to 'doc')
-rw-r--r--doc/languages-frameworks/javascript.section.md65
1 files changed, 65 insertions, 0 deletions
diff --git a/doc/languages-frameworks/javascript.section.md b/doc/languages-frameworks/javascript.section.md
index f706f92c6691..76db9d0007ce 100644
--- a/doc/languages-frameworks/javascript.section.md
+++ b/doc/languages-frameworks/javascript.section.md
@@ -310,6 +310,71 @@ See `node2nix` [docs](https://github.com/svanderburg/node2nix) for more info.
- `node2nix` has some [bugs](https://github.com/svanderburg/node2nix/issues/238) related to working with lock files from npm distributed with `nodejs_16`.
- `node2nix` does not like missing packages from npm. If you see something like `Cannot resolve version: vue-loader-v16@undefined` then you might want to try another tool. The package might have been pulled off of npm.
+### pnpm {#javascript-pnpm}
+
+Pnpm is available as the top-level package `pnpm`. Additionally, there are variants pinned to certain major versions, like `pnpm_8` and `pnpm_9`, which support different sets of lock file versions.
+
+When packaging an application that includes a `pnpm-lock.yaml`, you need to fetch the pnpm store for that project using a fixed-output-derivation. The functions `pnpm_8.fetchDeps` and `pnpm_9.fetchDeps` can create this pnpm store derivation. In conjunction, the setup hooks `pnpm_8.configHook` and `pnpm_9.configHook` will prepare the build environment to install the prefetched dependencies store. Here is an example for a package that contains a `package.json` and a `pnpm-lock.yaml` files using the above `pnpm_` attributes:
+
+```nix
+{
+ stdenv,
+ nodejs,
+ # This is pinned as { pnpm = pnpm_9; }
+ pnpm
+}:
+
+stdenv.mkDerivation (finalAttrs: {
+ pname = "foo";
+ version = "0-unstable-1980-01-01";
+
+ src = ...;
+
+ nativeBuildInputs = [
+ nodejs
+ pnpm.configHook
+ ];
+
+ pnpmDeps = pnpm.fetchDeps {
+ inherit (finalAttrs) pname version src;
+ hash = "...";
+ };
+})
+```
+
+NOTE: It is highly recommended to use a pinned version of pnpm (i.e. `pnpm_8` or `pnpm_9`), to increase future reproducibility. It might also be required to use an older version, if the package needs support for a certain lock file version.
+
+In case you are patching `package.json` or `pnpm-lock.yaml`, make sure to pass `finalAttrs.patches` to the function as well (i.e. `inherit (finalAttrs) patches`.
+
+#### Dealing with `sourceRoot` {#javascript-pnpm-sourceRoot}
+
+NOTE: Nixpkgs pnpm tooling doesn't support building projects with a `pnpm-workspace.yaml`, or building monorepos. It maybe possible to use `pnpm.fetchDeps` for these projects, but it may be hard or impossible to produce a binary from such projects ([an example attempt](https://github.com/NixOS/nixpkgs/pull/290715#issuecomment-2144543728)).
+
+If the pnpm project is in a subdirectory, you can just define `sourceRoot` or `setSourceRoot` for `fetchDeps`. Note, that projects using `pnpm-workspace.yaml` are currently not supported, and will probably not work using this approach.
+If `sourceRoot` is different between the parent derivation and `fetchDeps`, you will have to set `pnpmRoot` to effectively be the same location as it is in `fetchDeps`.
+
+Assuming the following directory structure, we can define `sourceRoot` and `pnpmRoot` as follows:
+
+```
+.
+├── frontend
+│   ├── ...
+│   ├── package.json
+│   └── pnpm-lock.yaml
+└── ...
+```
+
+```nix
+ ...
+ pnpmDeps = pnpm.fetchDeps {
+ ...
+ sourceRoot = "${finalAttrs.src.name}/frontend";
+ };
+
+ # by default the working directory is the extracted source
+ pnpmRoot = "frontend";
+```
+
### yarn2nix {#javascript-yarn2nix}
#### Preparation {#javascript-yarn2nix-preparation}