Skip to content

Commit f8905c7

Browse files
committed
separate concerns
1 parent b28c9b8 commit f8905c7

File tree

3 files changed

+120
-205
lines changed

3 files changed

+120
-205
lines changed

default.nix

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
pkgs ? import <nixpkgs> {},
3+
system ? builtins.currentSystem,
4+
sha256 ? null,
5+
}: let
6+
packageJson = builtins.fromJSON (builtins.readFile ./package.json);
7+
nodeVersion = builtins.replaceStrings ["^" "~"] ["" ""] packageJson.engines.node;
8+
pnpmVersion = builtins.head (builtins.match "pnpm@([0-9.]+).*" packageJson.packageManager);
9+
10+
getPlatformString = platform:
11+
if platform == "nodejs"
12+
then
13+
if pkgs.stdenv.isDarwin
14+
then
15+
if pkgs.stdenv.isAarch64
16+
then "darwin-arm64"
17+
else "darwin-x64"
18+
else if pkgs.stdenv.isAarch64
19+
then "linux-arm64"
20+
else "linux-x64"
21+
else if pkgs.stdenv.isDarwin
22+
then
23+
if pkgs.stdenv.isAarch64
24+
then "macos-arm64"
25+
else "macos-x64"
26+
else if pkgs.stdenv.isAarch64
27+
then "linux-arm64"
28+
else "linux-x64";
29+
in rec {
30+
buildNodePackages = pkgs: {
31+
nodejs = pkgs.stdenv.mkDerivation {
32+
name = "nodejs-${nodeVersion}";
33+
src = pkgs.fetchurl {
34+
url = "https://nodejs.org/dist/v${nodeVersion}/node-v${nodeVersion}-${getPlatformString "nodejs"}.tar.xz";
35+
inherit sha256;
36+
};
37+
buildInputs = [pkgs.python3];
38+
sourceRoot = ".";
39+
dontUnpack = false;
40+
dontBuild = true;
41+
installPhase = ''
42+
mkdir -p $out
43+
mv node-v${nodeVersion}-* node
44+
cp -r node/* $out/
45+
chmod +x $out/bin/node
46+
ln -s $out/bin/node $out/bin/nodejs
47+
'';
48+
};
49+
50+
pnpm = pkgs.stdenv.mkDerivation {
51+
name = "pnpm-${pnpmVersion}";
52+
src = pkgs.fetchurl {
53+
url = "https://github.com/pnpm/pnpm/releases/download/v${pnpmVersion}/pnpm-${getPlatformString "pnpm"}";
54+
inherit sha256;
55+
};
56+
dontUnpack = true;
57+
dontStrip = true;
58+
installPhase = ''
59+
mkdir -p $out/bin
60+
cp $src $out/bin/pnpm
61+
chmod +x $out/bin/pnpm
62+
'';
63+
};
64+
};
65+
}

flake.lock

+17-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+38-202
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
inputs = {
55
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6-
pnpm2nix = {
7-
url = "github:nzbr/pnpm2nix-nzbr";
8-
inputs.nixpkgs.follows = "nixpkgs";
9-
};
6+
pnpm2nix.url = "github:nzbr/pnpm2nix-nzbr";
107
};
118

129
outputs = {
@@ -16,212 +13,51 @@
1613
}: let
1714
systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
1815
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
19-
20-
# Read package.json content directly
21-
packageJson = builtins.fromJSON (builtins.readFile ./package.json);
22-
23-
# Extract versions directly
24-
nodeVersion = builtins.replaceStrings ["^" "~"] ["" ""] packageJson.engines.node;
25-
pnpmVersion = builtins.head (builtins.match "pnpm@([0-9.]+).*" packageJson.packageManager);
26-
27-
# Function to fetch Node.js for a specific system
28-
fetchNodejs = system: let
29-
pkgs = nixpkgs.legacyPackages.${system};
30-
platformMap = {
31-
"x86_64-linux" = "linux-x64";
32-
"aarch64-linux" = "linux-arm64";
33-
"x86_64-darwin" = "darwin-x64";
34-
"aarch64-darwin" = "darwin-arm64";
35-
};
36-
platform = platformMap.${system};
37-
in
38-
pkgs.stdenv.mkDerivation {
39-
pname = "nodejs";
40-
version = nodeVersion;
41-
42-
src = pkgs.fetchurl {
43-
urls = [
44-
"https://nodejs.org/dist/v${nodeVersion}/node-v${nodeVersion}-${platform}.tar.xz"
45-
"https://nodejs.org/download/release/v${nodeVersion}/node-v${nodeVersion}-${platform}.tar.xz"
46-
];
47-
hash = null;
48-
};
49-
50-
# Skip unnecessary phases
51-
dontBuild = true;
52-
dontConfigure = true;
53-
54-
installPhase = ''
55-
mkdir -p $out
56-
cp -r * $out/
57-
chmod +x $out/bin/*
58-
'';
59-
60-
# Add post-fixup phase for NixOS compatibility
61-
fixupPhase = ''
62-
patchShebangs $out/bin
63-
'';
64-
};
65-
66-
# Create pkgs with overlays
67-
pkgsFor = system: let
68-
pkgs = nixpkgs.legacyPackages.${system};
69-
nodejsCustom = pkgs.stdenv.mkDerivation {
70-
pname = "nodejs";
71-
version = nodeVersion;
72-
73-
src = fetchNodejs system;
74-
75-
# Skip phases that aren't needed for pre-built binaries
76-
dontBuild = true;
77-
dontConfigure = true;
78-
dontPatchELF = true;
79-
dontPatchShebangs = true;
80-
81-
installPhase = ''
82-
runHook preInstall
83-
84-
mkdir -p $out
85-
cp -R * $out/
86-
87-
# Ensure bin directory exists and files are executable
88-
mkdir -p $out/bin
89-
chmod +x $out/bin/*
90-
91-
# Make sure node is executable
92-
chmod +x $out/bin/node
93-
94-
runHook postInstall
95-
'';
96-
97-
# Only use autoPatchelfHook on Linux systems
98-
nativeBuildInputs = with pkgs;
99-
if pkgs.stdenv.isLinux
100-
then [autoPatchelfHook]
101-
else [];
102-
103-
# Only include C libraries on Linux
104-
buildInputs = with pkgs;
105-
if pkgs.stdenv.isLinux
106-
then [stdenv.cc.cc.lib]
107-
else [];
108-
109-
# Add post-fixup phase to ensure executables are properly handled
110-
postFixup = ''
111-
# Ensure all executables in bin are actually executable
112-
find $out/bin -type f -exec chmod +x {} +
113-
'';
114-
};
115-
116-
# Create a pnpm package directly from npm registry
117-
pnpmTarball = pkgs.fetchurl {
118-
urls = [
119-
"https://registry.npmjs.org/pnpm/-/pnpm-${pnpmVersion}.tgz"
120-
"https://registry.npmjs.com/pnpm/-/pnpm-${pnpmVersion}.tgz"
121-
];
122-
curlOpts = ''-L --retry 3 --retry-delay 3'';
123-
hash = null;
124-
};
125-
in
126-
import nixpkgs {
16+
in {
17+
devShells = forAllSystems (system: let
18+
pkgs = import nixpkgs {
12719
inherit system;
128-
overlays = [
129-
(final: prev: {
130-
nodejs = nodejsCustom;
131-
pnpm = prev.stdenv.mkDerivation {
132-
pname = "pnpm";
133-
version = pnpmVersion;
134-
135-
src = pnpmTarball;
136-
137-
nativeBuildInputs = with pkgs; [
138-
nodejsCustom
139-
makeWrapper
140-
];
141-
142-
installPhase = ''
143-
export HOME=$TMPDIR
144-
mkdir -p $out/bin $out/lib/node_modules/pnpm
145-
tar xf $src -C $out/lib/node_modules/pnpm --strip-components=1
146-
makeWrapper ${nodejsCustom}/bin/node $out/bin/pnpm \
147-
--add-flags $out/lib/node_modules/pnpm/bin/pnpm.cjs
148-
'';
149-
};
150-
})
151-
];
20+
overlays = [pnpm2nix.overlays.default];
15221
};
153-
in {
154-
packages = forAllSystems (system: {
155-
default = pnpm2nix.lib.${system}.mkPnpmPackage {
156-
src = ./.;
157-
nodejs = (pkgsFor system).nodejs;
158-
installInPlace = true;
159-
160-
installEnv = {
161-
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "1";
162-
};
163-
164-
script = "build";
16522

166-
extraBuildInputs = with (pkgsFor system); [
23+
nodePackages =
24+
(import ./default.nix {
25+
inherit pkgs system;
26+
sha256 = null;
27+
})
28+
.buildNodePackages
29+
pkgs;
30+
in {
31+
default = pkgs.mkShell {
32+
buildInputs = with pkgs; [
33+
nodePackages.nodejs
34+
nodePackages.pnpm
16735
python3
16836
pkg-config
169-
turbo
17037
];
17138
};
17239
});
17340

174-
devShells = forAllSystems (
175-
system: let
176-
pkgs = pkgsFor system;
177-
in {
178-
default = pkgs.mkShell {
179-
buildInputs = with pkgs; [
180-
# Use our custom nodejs and pnpm versions instead of system packages
181-
nodejs # This will use the custom nodejs defined in the overlay
182-
pnpm # This will use the custom pnpm defined in the overlay
183-
python3
184-
pkg-config
185-
186-
# Add canvas build dependencies
187-
cairo
188-
pango
189-
libpng
190-
libjpeg
191-
giflib
192-
librsvg
193-
pixman
194-
195-
# Build essentials
196-
gcc
197-
gnumake
198-
199-
# Optional but helpful
200-
vips
201-
];
202-
203-
shellHook = ''
204-
export PKG_CONFIG_PATH="${pkgs.cairo}/lib/pkgconfig:${pkgs.pango}/lib/pkgconfig:${pkgs.libpng}/lib/pkgconfig:$PKG_CONFIG_PATH"
205-
echo "🤖 Eliza development environment loaded 🚀"
206-
echo "------------------------------------------"
207-
echo "Using:"
208-
echo " - Node.js $(node --version)"
209-
echo " - pnpm $(pnpm --version)"
210-
211-
echo """
212-
🏗️ Quickstart Guide:
213-
------------------------
214-
┌─> 1. pnpm i (Install dependencies)
215-
│ 2. pnpm build (Build project)
216-
└─ 3. pnpm clean (Clear Artifacts, for a fresh start)
217-
4. pnpm test (Run tests)
218-
219-
For more commands, run: pnpm --help
220-
------------------------
221-
"""
222-
'';
223-
};
224-
}
225-
);
41+
lib = {
42+
getPlatformString = system: platform:
43+
if platform == "nodejs"
44+
then
45+
if builtins.match ".*darwin.*" system != null
46+
then
47+
if builtins.match ".*aarch64.*" system != null
48+
then "darwin-arm64"
49+
else "darwin-x64"
50+
else if builtins.match ".*aarch64.*" system != null
51+
then "linux-arm64"
52+
else "linux-x64"
53+
else if builtins.match ".*darwin.*" system != null
54+
then
55+
if builtins.match ".*aarch64.*" system != null
56+
then "macos-arm64"
57+
else "macos-x64"
58+
else if builtins.match ".*aarch64.*" system != null
59+
then "linux-arm64"
60+
else "linux-x64";
61+
};
22662
};
22763
}

0 commit comments

Comments
 (0)