A one-liner for getting the exact versions of applications

Author's image
Tamás Sallai
1 min
Photo by Aaron Burden on Unsplash

For a project I needed to use the exact versions of node and pnpm to build and deploy the app. There are a few ways to do that but since I'm getting familiar with Nix I decided to use that for this.

The magic is to fetch specific versions of nixpkgs that contain the version needed for each program and then have a shell with the combination.

nix-shell --pure --keep AWS_REGION --keep AWS_ACCESS_KEY_ID --keep AWS_SECRET_ACCESS_KEY --keep AWS_SESSION_TOKEN -E '
let
  pkgsA = (import (builtins.fetchTarball https://github.com/NixOS/nixpkgs/archive/0cb2fd7c59fed0cd82ef858cbcbdb552b9a33465.tar.gz) {});
  pkgsB = (import (builtins.fetchTarball https://github.com/NixOS/nixpkgs/archive/a9858885e197f984d92d7fe64e9fff6b2e488d40.tar.gz) {});
in
pkgsA.mkShell {
  buildInputs = [
    pkgsA.nodejs_20
    pkgsB.pnpm
	pkgsB.procps pkgsB.cacert
  ];
}'

This starts a shell with node v20.15.1 and pnpm v9.1.1:

[nix-shell:~]$ node --version
v20.15.1

[nix-shell:~]$ npm --version
10.7.0

[nix-shell:~]$ pnpm --version
9.1.1

How to know which archive to fetch?

There is a super helpful website for this: Nixhub. Here, you can search for a package and get a list of available versions with a Nixpkgs reference. For example, searching for pnpm:

Version 9.1.1

Nixpkgs Reference a9858885e197f984d92d7fe64e9fff6b2e488d40

Then import this tarball (import (builtins.fetchTarball https://github.com/NixOS/nixpkgs/archive/<reference>.tar.gz) {}), and finally add the package to the buildInputs (pkgsB.pnpm).

February 14, 2025

Free PDF guide

Sign up to our newsletter and download the "How Cognito User Pools work" guide.