Skip to content

Commit

Permalink
pnpm.fetchDeps: Add workspaces support and support for custom pnpm co…
Browse files Browse the repository at this point in the history
…nfiguration commands

Solves NixOS#316908

(cherry picked from commit 875c9f0)
  • Loading branch information
pyrox0 authored and GiovanniGrieco committed Aug 31, 2024
1 parent 0efcafd commit 97d29dd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
55 changes: 52 additions & 3 deletions doc/languages-frameworks/javascript.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,11 @@ NOTE: It is highly recommended to use a pinned version of pnpm (i.e. `pnpm_8` or
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}
`pnpm.configHook` supports adding additional `pnpm install` flags via `pnpmInstallFlags` which can be set to a Nix string array.
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)).
#### Dealing with `sourceRoot` {#javascript-pnpm-sourceRoot}
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 the pnpm project is in a subdirectory, you can just define `sourceRoot` or `setSourceRoot` for `fetchDeps`.
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:
Expand All @@ -375,6 +375,55 @@ Assuming the following directory structure, we can define `sourceRoot` and `pnpm
pnpmRoot = "frontend";
```
#### PNPM Workspaces {#javascript-pnpm-workspaces}
If you need to use a PNPM workspace for your project, then set `pnpmWorkspace = "<workspace project name>"` in your `pnpm.fetchDeps` call,
which will make PNPM only install dependencies for that workspace package.
For example:
```nix
...
pnpmWorkspace = "@astrojs/language-server";
pnpmDeps = pnpm.fetchDeps {
inherit (finalAttrs) pnpmWorkspace;
...
}
```
The above would make `pnpm.fetchDeps` call only install dependencies for the `@astrojs/language-server` workspace package.
Note that you do not need to set `sourceRoot` to make this work.
Usually in such cases, you'd want to use `pnpm --filter=$pnpmWorkspace build` to build your project, as `npmHooks.npmBuildHook` probably won't work. A `buildPhase` based on the following example will probably fit most workspace projects:
```nix
buildPhase = ''
runHook preBuild
pnpm --filter=@astrojs/language-server build
runHook postBuild
'';
```
#### Additional PNPM Commands and settings {#javascript-pnpm-extraCommands}
If you require setting an additional PNPM configuration setting (such as `dedupe-peer-dependents` or similar),
set `prePnpmInstall` to the right commands to run. For example:
```nix
prePnpmInstall = ''
pnpm config set dedupe-peer-dependants false
'';
pnpmDeps = pnpm.fetchDeps {
inherit (finalAttrs) prePnpmInstall;
...
};
```
In this example, `prePnpmInstall` will be run by both `pnpm.configHook` and by the `pnpm.fetchDeps` builder.
### yarn2nix {#javascript-yarn2nix}
#### Preparation {#javascript-yarn2nix-preparation}
Expand Down
6 changes: 6 additions & 0 deletions pkgs/development/tools/pnpm/fetch-deps/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
{
hash ? "",
pname,
pnpmWorkspace ? "",
prePnpmInstall ? "",
...
}@args:
let
Expand All @@ -29,6 +31,7 @@
outputHash = "";
outputHashAlgo = "sha256";
};
installFlags = lib.optionalString (pnpmWorkspace != "") "--filter=${pnpmWorkspace}";
in
stdenvNoCC.mkDerivation (finalAttrs: (
args'
Expand Down Expand Up @@ -58,11 +61,14 @@
pnpm config set side-effects-cache false
# As we pin pnpm versions, we don't really care about updates
pnpm config set update-notifier false
# Run any additional pnpm configuration commands that users provide.
${prePnpmInstall}
# pnpm is going to warn us about using --force
# --force allows us to fetch all dependencies including ones that aren't meant for our host platform
pnpm install \
--force \
--ignore-scripts \
${installFlags} \
--frozen-lockfile
runHook postInstall
Expand Down
6 changes: 6 additions & 0 deletions pkgs/development/tools/pnpm/fetch-deps/pnpm-config-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ pnpmConfigHook() {

echo "Installing dependencies"

if [[ -n "$pnpmWorkspace" ]]; then
pnpmInstallFlags+=("--filter=$pnpmWorkspace")
fi
runHook prePnpmInstall

pnpm install \
--offline \
--ignore-scripts \
"${pnpmInstallFlags[@]}" \
--frozen-lockfile


Expand Down

0 comments on commit 97d29dd

Please sign in to comment.