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
'';
}