For modern versions of Yarn, the preferred installation is through Corepack, and Corepack is included by default with Node.js installs (>=16.10). See Installation | Yarn.
This is what I’ve tried to do corepack enable in default.nix , but it doesn’t work.
We’ll need a bit more than “doesn’t work” Error messages/logs would be nice.
The preferred way with nix should probably be to use the package from nixpkgs, because it avoids any number of issues you’ll run into because other project’s package managers have no support for nixos: yarn.
If you really need to follow the upstream way, you’ll likely need to patchelf some things, and/or use buildFhsUserEnv[Bubblewrap]. Could you elaborate on why you need to do this? Maybe we can find some other solution.
You can then use that derivation as a build input for some other derivation, and the corepack shims will be available in $PATH.
Note that corepack downloads package managers to ~/.node/corepack, but $HOME is set to /homeless-shelter inside Nix builds. So when using the corepack shims, you may have to give it a writable home directory by doing something like: export HOME=$TMPDIR
EDIT: You can apparently also set COREPACK_HOME if you don’t want to modify HOME.
EDIT 2: Corepack downloading stuff also makes your build impure. Not sure what the best approach to using corepack is, really.
It might be best just to package a more recent version of yarn from source at that rate. I wonder why it isn’t packaged yet, a cursory glance shows no PRs for it.
This confused me as well at first, but then I read in the Yarn docs that recent versions of Yarn 1.x can install newer versions itself. So that might be a reason why there is only the 1.x package in nixpkgs.
Anyway, here’s what works for me in shell.nix:
let
pkgs = import (fetchTarball https://channels.nixos.org/nixpkgs-22.11-darwin/nixexprs.tar.xz) {};
nodejs = pkgs.nodejs-16_x;
yarn = pkgs.yarn.override { inherit nodejs; };
in pkgs.mkShell {
buildInputs = [
nodejs
yarn
];
shellHook = ''
yarn set version 3.x
'';
}
Since nixpkgs 23.11-beta there’s a corepack wrapper package which will make the relevant executables available on the path. You will probably also need the nodejs package, since neither includes the other AFAICT.