Skip to content

Commit

Permalink
feat(nix): add flake with auto-versioned Node.js and pnpm
Browse files Browse the repository at this point in the history
- Add Nix flake for development environment
- Auto-detect Node.js and pnpm versions from package.json
- Configure development tools and environment variables
- Add shell hook with helpful commands
  • Loading branch information
lessuselesss committed Dec 18, 2024
1 parent 9eb88f0 commit 72d22cd
Showing 1 changed file with 25 additions and 36 deletions.
61 changes: 25 additions & 36 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,46 @@

# Read versions from package.json
packageJson = builtins.fromJSON (builtins.readFile ./package.json);
versions = {
versions = let
# Extract version from packageManager string (e.g., "[email protected]+sha512...")
pnpmFull = packageJson.packageManager or "[email protected]";
pnpmVersion = builtins.head (builtins.match "pnpm@([^+]+).*" pnpmFull);
in {
nodejs = builtins.replaceStrings ["^" "~"] ["" ""] packageJson.engines.node;
pnpm = builtins.replaceStrings ["^" "~"] ["" ""] packageJson.engines.pnpm;
pnpm = pnpmVersion;
};

# Function to fetch and hash a URL
fetchUrlHash = url: let
file = builtins.fetchurl url;
hash = builtins.hashFile "sha256" file;
in "sha256-${hash}";

# Generate hashes for Node.js and pnpm
nodeHashes = {
darwin-x64 = fetchUrlHash "https://nodejs.org/dist/v${versions.nodejs}/node-v${versions.nodejs}-darwin-x64.tar.gz";
darwin-arm64 = fetchUrlHash "https://nodejs.org/dist/v${versions.nodejs}/node-v${versions.nodejs}-darwin-arm64.tar.gz";
linux-x64 = fetchUrlHash "https://nodejs.org/dist/v${versions.nodejs}/node-v${versions.nodejs}-linux-x64.tar.gz";
linux-arm64 = fetchUrlHash "https://nodejs.org/dist/v${versions.nodejs}/node-v${versions.nodejs}-linux-arm64.tar.gz";
};
# Function to fetch Node.js tarball with hash
fetchNodeJs = version: platform: arch:
pkgs.fetchurl {
url = "https://nodejs.org/dist/v${version}/node-v${version}-${platform}-${arch}.tar.gz";
hash = null; # Nix will provide the correct hash when it fails
};

pnpmHash = fetchUrlHash "https://registry.npmjs.org/pnpm/-/pnpm-${versions.pnpm}.tgz";
# Function to fetch pnpm tarball with hash
fetchPnpm = version:
pkgs.fetchurl {
url = "https://registry.npmjs.org/pnpm/-/pnpm-${version}.tgz";
hash = null; # Nix will provide the correct hash when it fails
};

# Define specific Node.js version
nodejs = pkgs.stdenv.mkDerivation rec {
pname = "nodejs";
version = versions.nodejs;

src = pkgs.fetchurl {
url = "https://nodejs.org/dist/v${version}/node-v${version}-${
src =
fetchNodeJs version
(
if pkgs.stdenv.isDarwin
then "darwin"
else "linux"
}-${
)
(
if pkgs.stdenv.isx86_64
then "x64"
else "arm64"
}.tar.gz";
hash =
nodeHashes
."${
if pkgs.stdenv.isDarwin
then "darwin"
else "linux"
}-${
if pkgs.stdenv.isx86_64
then "x64"
else "arm64"
}";
};
);

installPhase = ''
mkdir -p $out
Expand All @@ -81,10 +73,7 @@
name = "pnpm";
version = versions.pnpm;

src = pkgs.fetchurl {
url = "https://registry.npmjs.org/pnpm/-/pnpm-${versions.pnpm}.tgz";
hash = pnpmHash;
};
src = fetchPnpm versions.pnpm;

buildInputs = [nodejs];

Expand Down

0 comments on commit 72d22cd

Please sign in to comment.