-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nix): add flake with auto-versioned Node.js and pnpm
- 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
1 parent
9eb88f0
commit 72d22cd
Showing
1 changed file
with
25 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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]; | ||
|
||
|