Installing a Specific Version of Node in a Nix Flake
Recently, I adopted nix
as my go-to solution for managing all kinds of settings. I even rewrote all of my dotfiles using nix. You can find it here.
One of the things I am using nix for is a local flake.nix
file in most projects. Setting Node/Python/Go versions with some more dependencies.
Recently, I needed a specific Node version in a project. 18.15.0
. Using the default nix pacakge nodejs-18_x
installed 18.14.1
. I submitted a PR to upgrade it here: https://github.com/NixOS/nixpkgs/pull/222603
Until that PR gets merged (the repo has 3.4K PRs, could take time), I needed a local solution for the project I am working on.
After a bunch of fiddling around (ChatGPT did not help). I found the solution:
{
description = "Description for the project";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [ ];
systems = [ "aarch64-darwin" ];
perSystem = { config, self', inputs', pkgs, system, ... }: {
packages = {
nodejs18 = pkgs.stdenv.mkDerivation {
name = "nodejs18.15.0";
src = pkgs.fetchurl {
url = "https://nodejs.org/dist/v18.15.0/node-v18.15.0-darwin-arm64.tar.gz";
sha256 = "sha256-vTAqaJw8NOK2HYa5feZtJqM1iBoXrwm2oKS7EBnfVuQ=";
};
installPhase = ''
echo "installing nodejs"
mkdir -p $out
cp -r ./ $out/
'';
};
};
devShells.default = pkgs.mkShell {
buildInputs = [
pkgs.postgresql_14
pkgs.gnused
pkgs.yarn
self'.packages.nodejs18
pkgs.nodePackages.node-gyp-build
];
};
};
flake = {
};
};
}
This installs node in pretty much any version you want.
I also tried creating an overlay that overrides the attributes of the nodejs-18_x
version but it did not work, I am not sure if that package allows for that.
Happy Hacking!