Skip to content

Commit 72d22cd

Browse files
committed
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
1 parent 9eb88f0 commit 72d22cd

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

flake.nix

+25-36
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,46 @@
1616

1717
# Read versions from package.json
1818
packageJson = builtins.fromJSON (builtins.readFile ./package.json);
19-
versions = {
19+
versions = let
20+
# Extract version from packageManager string (e.g., "pnpm@9.12.3+sha512...")
21+
pnpmFull = packageJson.packageManager or "pnpm@9.12.3";
22+
pnpmVersion = builtins.head (builtins.match "pnpm@([^+]+).*" pnpmFull);
23+
in {
2024
nodejs = builtins.replaceStrings ["^" "~"] ["" ""] packageJson.engines.node;
21-
pnpm = builtins.replaceStrings ["^" "~"] ["" ""] packageJson.engines.pnpm;
25+
pnpm = pnpmVersion;
2226
};
2327

24-
# Function to fetch and hash a URL
25-
fetchUrlHash = url: let
26-
file = builtins.fetchurl url;
27-
hash = builtins.hashFile "sha256" file;
28-
in "sha256-${hash}";
29-
30-
# Generate hashes for Node.js and pnpm
31-
nodeHashes = {
32-
darwin-x64 = fetchUrlHash "https://nodejs.org/dist/v${versions.nodejs}/node-v${versions.nodejs}-darwin-x64.tar.gz";
33-
darwin-arm64 = fetchUrlHash "https://nodejs.org/dist/v${versions.nodejs}/node-v${versions.nodejs}-darwin-arm64.tar.gz";
34-
linux-x64 = fetchUrlHash "https://nodejs.org/dist/v${versions.nodejs}/node-v${versions.nodejs}-linux-x64.tar.gz";
35-
linux-arm64 = fetchUrlHash "https://nodejs.org/dist/v${versions.nodejs}/node-v${versions.nodejs}-linux-arm64.tar.gz";
36-
};
28+
# Function to fetch Node.js tarball with hash
29+
fetchNodeJs = version: platform: arch:
30+
pkgs.fetchurl {
31+
url = "https://nodejs.org/dist/v${version}/node-v${version}-${platform}-${arch}.tar.gz";
32+
hash = null; # Nix will provide the correct hash when it fails
33+
};
3734

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

4042
# Define specific Node.js version
4143
nodejs = pkgs.stdenv.mkDerivation rec {
4244
pname = "nodejs";
4345
version = versions.nodejs;
4446

45-
src = pkgs.fetchurl {
46-
url = "https://nodejs.org/dist/v${version}/node-v${version}-${
47+
src =
48+
fetchNodeJs version
49+
(
4750
if pkgs.stdenv.isDarwin
4851
then "darwin"
4952
else "linux"
50-
}-${
53+
)
54+
(
5155
if pkgs.stdenv.isx86_64
5256
then "x64"
5357
else "arm64"
54-
}.tar.gz";
55-
hash =
56-
nodeHashes
57-
."${
58-
if pkgs.stdenv.isDarwin
59-
then "darwin"
60-
else "linux"
61-
}-${
62-
if pkgs.stdenv.isx86_64
63-
then "x64"
64-
else "arm64"
65-
}";
66-
};
58+
);
6759

6860
installPhase = ''
6961
mkdir -p $out
@@ -81,10 +73,7 @@
8173
name = "pnpm";
8274
version = versions.pnpm;
8375

84-
src = pkgs.fetchurl {
85-
url = "https://registry.npmjs.org/pnpm/-/pnpm-${versions.pnpm}.tgz";
86-
hash = pnpmHash;
87-
};
76+
src = fetchPnpm versions.pnpm;
8877

8978
buildInputs = [nodejs];
9079

0 commit comments

Comments
 (0)