mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-12 04:35:41 +03:00
Merge pull request #323493 from pyrox0/pnpm-fetchdeps-improve
pnpm.fetchDeps: Add workspace and custom pnpm config commands support
This commit is contained in:
commit
d6f7206fe0
8 changed files with 151 additions and 19 deletions
|
@ -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`.
|
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`.
|
||||||
|
|
||||||
|
`pnpm.configHook` supports adding additional `pnpm install` flags via `pnpmInstallFlags` which can be set to a Nix string array.
|
||||||
|
|
||||||
#### Dealing with `sourceRoot` {#javascript-pnpm-sourceRoot}
|
#### 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`.
|
||||||
|
|
||||||
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`.
|
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:
|
Assuming the following directory structure, we can define `sourceRoot` and `pnpmRoot` as follows:
|
||||||
|
@ -375,6 +375,55 @@ Assuming the following directory structure, we can define `sourceRoot` and `pnpm
|
||||||
pnpmRoot = "frontend";
|
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.
|
||||||
|
|
||||||
|
|
||||||
### Yarn {#javascript-yarn}
|
### Yarn {#javascript-yarn}
|
||||||
|
|
||||||
Yarn based projects use a `yarn.lock` file instead of a `package-lock.json` to pin dependencies. Nixpkgs provides the Nix function `fetchYarnDeps` which fetches an offline cache suitable for running `yarn install` before building the project. In addition, Nixpkgs provides the hooks:
|
Yarn based projects use a `yarn.lock` file instead of a `package-lock.json` to pin dependencies. Nixpkgs provides the Nix function `fetchYarnDeps` which fetches an offline cache suitable for running `yarn install` before building the project. In addition, Nixpkgs provides the hooks:
|
||||||
|
|
69
pkgs/by-name/as/astro-language-server/package.nix
Normal file
69
pkgs/by-name/as/astro-language-server/package.nix
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
{
|
||||||
|
lib,
|
||||||
|
stdenv,
|
||||||
|
fetchFromGitHub,
|
||||||
|
pnpm_8,
|
||||||
|
nodejs_22,
|
||||||
|
}:
|
||||||
|
|
||||||
|
stdenv.mkDerivation (finalAttrs: {
|
||||||
|
pname = "astro-language-server";
|
||||||
|
version = "2.10.0";
|
||||||
|
|
||||||
|
src = fetchFromGitHub {
|
||||||
|
owner = "withastro";
|
||||||
|
repo = "language-tools";
|
||||||
|
rev = "@astrojs/language-server@${finalAttrs.version}";
|
||||||
|
hash = "sha256-WdeQQaC9AVHT+/pXLzaC6MZ6ddHsFSpxoDPHqWvqmiQ=";
|
||||||
|
};
|
||||||
|
|
||||||
|
pnpmDeps = pnpm_8.fetchDeps {
|
||||||
|
inherit (finalAttrs)
|
||||||
|
pname
|
||||||
|
version
|
||||||
|
src
|
||||||
|
pnpmWorkspace
|
||||||
|
prePnpmInstall
|
||||||
|
;
|
||||||
|
hash = "sha256-n7HTd/rKxJdQKnty5TeOcyvBU9j/EClQ9IHqbBaEwQE=";
|
||||||
|
};
|
||||||
|
|
||||||
|
nativeBuildInputs = [
|
||||||
|
nodejs_22
|
||||||
|
pnpm_8.configHook
|
||||||
|
];
|
||||||
|
|
||||||
|
buildInputs = [ nodejs_22 ];
|
||||||
|
|
||||||
|
pnpmWorkspace = "@astrojs/language-server";
|
||||||
|
prePnpmInstall = ''
|
||||||
|
pnpm config set dedupe-peer-dependents false
|
||||||
|
'';
|
||||||
|
|
||||||
|
buildPhase = ''
|
||||||
|
runHook preBuild
|
||||||
|
|
||||||
|
pnpm --filter=@astrojs/language-server build
|
||||||
|
|
||||||
|
runHook postBuild
|
||||||
|
'';
|
||||||
|
|
||||||
|
installPhase = ''
|
||||||
|
runHook preInstall
|
||||||
|
|
||||||
|
mkdir -p $out/{bin,lib/astro-language-server}
|
||||||
|
cp -r {packages,node_modules} $out/lib/astro-language-server
|
||||||
|
ln -s $out/lib/astro-language-server/packages/language-server/bin/nodeServer.js $out/bin/astro-ls
|
||||||
|
|
||||||
|
runHook postInstall
|
||||||
|
'';
|
||||||
|
|
||||||
|
meta = {
|
||||||
|
description = "The Astro language server";
|
||||||
|
homepage = "https://github.com/withastro/language-tools";
|
||||||
|
license = lib.licenses.mit;
|
||||||
|
maintainers = with lib.maintainers; [ pyrox0 ];
|
||||||
|
mainProgram = "astro-ls";
|
||||||
|
platforms = lib.platforms.unix;
|
||||||
|
};
|
||||||
|
})
|
|
@ -3,7 +3,6 @@
|
||||||
, fetchFromGitHub
|
, fetchFromGitHub
|
||||||
, pnpm_8
|
, pnpm_8
|
||||||
, nodejs
|
, nodejs
|
||||||
, npmHooks
|
|
||||||
, makeBinaryWrapper
|
, makeBinaryWrapper
|
||||||
, shellcheck
|
, shellcheck
|
||||||
}:
|
}:
|
||||||
|
@ -19,26 +18,23 @@ stdenv.mkDerivation (finalAttrs: {
|
||||||
hash = "sha256-yJ81oGd9aNsWQMLvDSgMVVH1//Mw/SVFYFIPsJTQYzE=";
|
hash = "sha256-yJ81oGd9aNsWQMLvDSgMVVH1//Mw/SVFYFIPsJTQYzE=";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pnpmWorkspace = "bash-language-server";
|
||||||
pnpmDeps = pnpm_8.fetchDeps {
|
pnpmDeps = pnpm_8.fetchDeps {
|
||||||
inherit (finalAttrs) pname version src;
|
inherit (finalAttrs) pname version src pnpmWorkspace;
|
||||||
hash = "sha256-W25xehcxncBs9QgQBt17F5YHK0b+GDEmt27XzTkyYWg=";
|
hash = "sha256-W25xehcxncBs9QgQBt17F5YHK0b+GDEmt27XzTkyYWg=";
|
||||||
};
|
};
|
||||||
|
|
||||||
nativeBuildInputs = [
|
nativeBuildInputs = [
|
||||||
nodejs
|
nodejs
|
||||||
pnpm_8.configHook
|
pnpm_8.configHook
|
||||||
npmHooks.npmBuildHook
|
|
||||||
makeBinaryWrapper
|
makeBinaryWrapper
|
||||||
];
|
];
|
||||||
npmBuildScript = "compile";
|
buildPhase = ''
|
||||||
# We are only interested in the bash-language-server executable, which is
|
runHook preBuild
|
||||||
# part of the `./server` directory. From some reason, the `./vscode-client`
|
|
||||||
# directory is not included in upstream's `pnpm-workspace.yaml`, so perhaps
|
pnpm --filter=bash-language-server build
|
||||||
# that's why our ${pnpmDeps} don't include the dependencies required for it.
|
|
||||||
preBuild = ''
|
runHook postBuild
|
||||||
rm -r vscode-client
|
|
||||||
substituteInPlace tsconfig.json \
|
|
||||||
--replace-fail '{ "path": "./vscode-client" },' ""
|
|
||||||
'';
|
'';
|
||||||
|
|
||||||
installPhase = ''
|
installPhase = ''
|
||||||
|
|
|
@ -37,6 +37,7 @@ in
|
||||||
|
|
||||||
mapAliases {
|
mapAliases {
|
||||||
"@antora/cli" = pkgs.antora; # Added 2023-05-06
|
"@antora/cli" = pkgs.antora; # Added 2023-05-06
|
||||||
|
"@astrojs/language-server" = pkgs.astro-language-server; # Added 2024-02-12
|
||||||
"@bitwarden/cli" = pkgs.bitwarden-cli; # added 2023-07-25
|
"@bitwarden/cli" = pkgs.bitwarden-cli; # added 2023-07-25
|
||||||
"@emacs-eask/cli" = pkgs.eask; # added 2023-08-17
|
"@emacs-eask/cli" = pkgs.eask; # added 2023-08-17
|
||||||
"@forge/cli" = throw "@forge/cli was removed because it was broken"; # added 2023-09-20
|
"@forge/cli" = throw "@forge/cli was removed because it was broken"; # added 2023-09-20
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
# Packages that provide a single executable.
|
# Packages that provide a single executable.
|
||||||
"@angular/cli" = "ng";
|
"@angular/cli" = "ng";
|
||||||
"@astrojs/language-server" = "astro-ls";
|
|
||||||
"@babel/cli" = "babel";
|
"@babel/cli" = "babel";
|
||||||
"@commitlint/cli" = "commitlint";
|
"@commitlint/cli" = "commitlint";
|
||||||
"@gitbeaker/cli" = "gitbeaker";
|
"@gitbeaker/cli" = "gitbeaker";
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
[
|
[
|
||||||
"@angular/cli"
|
"@angular/cli"
|
||||||
, "@antfu/ni"
|
, "@antfu/ni"
|
||||||
, "@astrojs/language-server"
|
|
||||||
, "@babel/cli"
|
, "@babel/cli"
|
||||||
, "@commitlint/cli"
|
, "@commitlint/cli"
|
||||||
, "@commitlint/config-conventional"
|
, "@commitlint/config-conventional"
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
{
|
{
|
||||||
hash ? "",
|
hash ? "",
|
||||||
pname,
|
pname,
|
||||||
|
pnpmWorkspace ? "",
|
||||||
|
prePnpmInstall ? "",
|
||||||
...
|
...
|
||||||
}@args:
|
}@args:
|
||||||
let
|
let
|
||||||
|
@ -29,6 +31,7 @@
|
||||||
outputHash = "";
|
outputHash = "";
|
||||||
outputHashAlgo = "sha256";
|
outputHashAlgo = "sha256";
|
||||||
};
|
};
|
||||||
|
installFlags = lib.optionalString (pnpmWorkspace != "") "--filter=${pnpmWorkspace}";
|
||||||
in
|
in
|
||||||
stdenvNoCC.mkDerivation (finalAttrs: (
|
stdenvNoCC.mkDerivation (finalAttrs: (
|
||||||
args'
|
args'
|
||||||
|
@ -58,9 +61,15 @@
|
||||||
pnpm config set side-effects-cache false
|
pnpm config set side-effects-cache false
|
||||||
# As we pin pnpm versions, we don't really care about updates
|
# As we pin pnpm versions, we don't really care about updates
|
||||||
pnpm config set update-notifier false
|
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
|
# 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
|
# --force allows us to fetch all dependencies including ones that aren't meant for our host platform
|
||||||
pnpm install --frozen-lockfile --ignore-script --force
|
pnpm install \
|
||||||
|
--force \
|
||||||
|
--ignore-scripts \
|
||||||
|
${installFlags} \
|
||||||
|
--frozen-lockfile
|
||||||
|
|
||||||
runHook postInstall
|
runHook postInstall
|
||||||
'';
|
'';
|
||||||
|
|
|
@ -24,7 +24,17 @@ pnpmConfigHook() {
|
||||||
|
|
||||||
echo "Installing dependencies"
|
echo "Installing dependencies"
|
||||||
|
|
||||||
pnpm install --offline --frozen-lockfile --ignore-script
|
if [[ -n "$pnpmWorkspace" ]]; then
|
||||||
|
pnpmInstallFlags+=("--filter=$pnpmWorkspace")
|
||||||
|
fi
|
||||||
|
runHook prePnpmInstall
|
||||||
|
|
||||||
|
pnpm install \
|
||||||
|
--offline \
|
||||||
|
--ignore-scripts \
|
||||||
|
"${pnpmInstallFlags[@]}" \
|
||||||
|
--frozen-lockfile
|
||||||
|
|
||||||
|
|
||||||
echo "Patching scripts"
|
echo "Patching scripts"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue