Using Nix to Create a Custom pnpm Script and Integrate with direnv

Siva
3 min readMar 4, 2024

--

If you’re a developer who uses the pnpm package manager, you might have faced situations where you need to customize the behavior of the pnpm command or add additional functionality. One way to achieve this is by creating a custom script that wraps the pnpmcommand and extends its functionality.

In this blog post, we’ll explore how to use the Nix package manager to create a custom pnpmscript and seamlessly integrate it with direnv for a smooth developer experience.

Prerequisites

Before we dive into the details, make sure you have the following prerequisites installed:

  • Nix package manager
  • direnv

Step 1: Set up the Nix environment

First, we need to set up the Nix environment and install the required dependencies. Open a terminal and run the following command:

nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz -p direnv

This command will create a Nix shell environment and install direnv, a tool that helps manage environment variables on a per-directory basis.

Step 2: Create the Nix expression

Next, create a file named pnpm-shim.nix in your project directory with the following content:

{ pkgs ? import <nixpkgs> {}, nodejs ? pkgs.nodejs }:

let
pnpm-shim = pkgs.writeShellScriptBin "pnpm" ''
#!${pkgs.bash}/bin/bash
exec ${pkgs.lib.getBin nodejs}/bin/node ${pkgs.lib.getBin nodejs}/bin/corepack pnpm "$@"
'';

in pnpm-shim

This Nix expression defines a shell script named pnpm that executes the pnpm command using the specified Node.js and corepack binaries. The writeShellScriptBin function from the Nix package manager is used to create the script.

Step 3: Configure direnv

To integrate the custom pnpm script with direnv, create a .envrc file in the same directory with the following content:

use_nix() {
watch_file pnpm-shim.nix
local result=$(nix-build pnpm-shim.nix)
export PATH=”$result/bin:$PATH”
}

use_nix

This .envrc file defines a function use_nix that watches for changes in the pnpm-shim.nix file, builds the derivation, and adds the resulting `bin` directory to the PATHenvironment variable.

Step 4: Allow direnv to load the .envrc file

Run the following command in the same directory to allow direnv to load the .envrc file:

direnv allow

This command will execute the use_nix function and add the custom pnpm script to your PATH.

Step 5: Use the custom pnpm script

Now, you can use the custom pnpmscript just like you would use the regular pnpm command. For example, to install dependencies, run:

pnpm install

You can also pass additional arguments or commands to the custom pnpm script, just as you would with the regular pnpm command.

Conclusion

By following these steps, you’ve successfully created a custom pnpm script using the Nix package manager and integrated it with direnv. This approach allows you to easily customize the behavior of the pnpm command and seamlessly integrate it into your development workflow. With direnv, you don’t need to manually set up the environment every time you enter the project directory; it will automatically load the custom pnpmscript and make it available in your PATH.

Code Walk-Thru(Demo):

--

--