Skip to content

Commit 2c78149

Browse files
committed
add overlays
1 parent c66d65b commit 2c78149

File tree

1 file changed

+150
-130
lines changed

1 file changed

+150
-130
lines changed

flake.nix

+150-130
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,176 @@
11
{
2-
description = "Eliza development environment";
2+
description = "Eliza Development Environment";
33

44
inputs = {
5-
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
6-
flake-utils.url = "github:numtide/flake-utils";
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
pnpm2nix = {
7+
url = "github:nzbr/pnpm2nix-nzbr";
8+
inputs.nixpkgs.follows = "nixpkgs";
9+
};
710
};
811

912
outputs = {
1013
self,
1114
nixpkgs,
12-
flake-utils,
13-
}:
14-
flake-utils.lib.eachDefaultSystem (system: let
15-
pkgs = import nixpkgs {inherit system;};
16-
17-
# Read versions from package.json
18-
packageJson = builtins.fromJSON (builtins.readFile ./package.json);
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 {
24-
nodejs = builtins.replaceStrings ["^" "~"] ["" ""] packageJson.engines.node;
25-
pnpm = pnpmVersion;
15+
pnpm2nix,
16+
}: let
17+
systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
18+
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
19+
20+
# Read and parse package.json
21+
packageJson = builtins.fromJSON (builtins.readFile ./package.json);
22+
23+
# Extract versions
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";
2635
};
27-
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-
};
34-
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-
};
41-
42-
# Define specific Node.js version
43-
nodejs = pkgs.stdenv.mkDerivation rec {
44-
pname = "nodejs";
45-
version = versions.nodejs;
46-
47-
src =
48-
fetchNodeJs version
49-
(
50-
if pkgs.stdenv.isDarwin
51-
then "darwin"
52-
else "linux"
53-
)
54-
(
55-
if pkgs.stdenv.isx86_64
56-
then "x64"
57-
else "arm64"
58-
);
59-
60-
installPhase = ''
61-
mkdir -p $out
62-
cp -r ./* $out/
63-
chmod +x $out/bin/node
64-
chmod +x $out/bin/npm
65-
chmod +x $out/bin/npx
66-
'';
67-
68-
dontStrip = true;
36+
platform = platformMap.${system};
37+
in
38+
pkgs.fetchurl {
39+
url = "https://nodejs.org/dist/v${nodeVersion}/node-v${nodeVersion}-${platform}.tar.xz";
40+
# Let Nix calculate the hash on first run
41+
hash = null;
6942
};
7043

71-
# Define specific pnpm version
72-
pnpm = pkgs.stdenv.mkDerivation {
73-
name = "pnpm";
74-
version = versions.pnpm;
44+
# Create pkgs with overlays
45+
pkgsFor = system: let
46+
pkgs = nixpkgs.legacyPackages.${system};
47+
nodejsCustom = pkgs.stdenv.mkDerivation {
48+
pname = "nodejs";
49+
version = nodeVersion;
7550

76-
src = fetchPnpm versions.pnpm;
51+
src = fetchNodejs system;
7752

78-
buildInputs = [nodejs];
53+
# Skip phases that aren't needed for pre-built binaries
54+
dontBuild = true;
55+
dontConfigure = true;
56+
dontPatchELF = true;
57+
dontPatchShebangs = true;
7958

8059
installPhase = ''
81-
# Create directories
82-
mkdir -p $out/{lib,bin}
60+
runHook preInstall
8361
84-
# Extract tarball
85-
cd $out/lib
86-
tar xzf $src --strip-components=1
87-
88-
# Create the executable
89-
cat > $out/bin/pnpm << EOF
90-
#!${nodejs}/bin/node
91-
require(process.env.PNPM_DIST || require('path').resolve(__dirname, '../lib/dist/pnpm.cjs'))
92-
EOF
62+
mkdir -p $out
63+
cp -R * $out/
9364
94-
chmod +x $out/bin/pnpm
65+
mkdir -p $out/bin
66+
chmod +x $out/bin/*
9567
96-
# Export the dist path
97-
mkdir -p $out/nix-support
98-
echo "export PNPM_DIST=\"$out/lib/dist/pnpm.cjs\"" >> $out/nix-support/setup-hook
68+
runHook postInstall
9969
'';
10070

101-
dontStrip = true;
71+
# Skip unnecessary fixup steps
72+
dontFixup = true;
73+
74+
# Add standard C libraries to rpath for binary compatibility
75+
nativeBuildInputs = with pkgs; [autoPatchelfHook];
76+
buildInputs = with pkgs; [stdenv.cc.cc.lib];
10277
};
10378

104-
# Define development tools separately
105-
devTools = with pkgs; [
106-
nixpkgs-fmt
107-
remarshal
108-
gcc
109-
gnumake
110-
python3
111-
vips
112-
libopus
113-
rabbitmq-server
114-
pkg-config
115-
pnpm # Our specific pnpm version
116-
];
117-
in {
118-
devShells.default = pkgs.mkShell {
119-
buildInputs = devTools;
120-
121-
shellHook = ''
122-
echo "Welcome to Eliza development environment"
123-
124-
# Ensure project-specific Node.js takes precedence
125-
export PATH="${nodejs}/bin:$PATH"
126-
127-
# Set up pnpm environment
128-
export PNPM_HOME="$HOME/.local/share/pnpm"
129-
export PATH="$PNPM_HOME:$PATH"
130-
131-
# Ensure PNPM_HOME exists
132-
mkdir -p "$PNPM_HOME"
133-
134-
# Set up development environment variables
135-
export NODE_ENV="development"
136-
export VERBOSE="true"
137-
export DEBUG="eliza:*"
138-
139-
# Print available commands
140-
echo "Available commands:"
141-
echo " pnpm i - Install dependencies"
142-
echo " pnpm build - Build the project"
143-
echo " pnpm dev - Start development server"
144-
echo " pnpm test - Run tests"
145-
146-
# Print actual environment versions
147-
node_version=$(${nodejs}/bin/node --version)
148-
pnpm_version=$(pnpm --version)
149-
echo "Node.js version: $node_version"
150-
echo "pnpm version: $pnpm_version"
151-
'';
79+
# Create a pnpm package directly from npm registry
80+
pnpmTarball = pkgs.fetchurl {
81+
url = "https://registry.npmjs.org/pnpm/-/pnpm-${pnpmVersion}.tgz";
82+
hash = null; # Will fail with correct hash on first run
83+
};
84+
in
85+
import nixpkgs {
86+
inherit system;
87+
overlays = [
88+
(final: prev: {
89+
nodejs = nodejsCustom;
90+
pnpm = prev.stdenv.mkDerivation {
91+
pname = "pnpm";
92+
version = pnpmVersion;
93+
94+
src = pnpmTarball;
95+
96+
nativeBuildInputs = with pkgs; [
97+
nodejsCustom
98+
makeWrapper
99+
];
100+
101+
installPhase = ''
102+
export HOME=$TMPDIR
103+
mkdir -p $out/bin $out/lib/node_modules/pnpm
104+
tar -xzf $src -C $out/lib/node_modules/pnpm --strip-components=1
105+
makeWrapper ${nodejsCustom}/bin/node $out/bin/pnpm \
106+
--add-flags $out/lib/node_modules/pnpm/bin/pnpm.cjs
107+
'';
108+
};
109+
})
110+
];
152111
};
112+
in {
113+
packages = forAllSystems (system: {
114+
default = pnpm2nix.lib.${system}.mkPnpmPackage {
115+
src = ./.;
116+
nodejs = (pkgsFor system).nodejs;
117+
installInPlace = true;
118+
119+
installEnv = {
120+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "1";
121+
};
153122

154-
formatter = pkgs.nixpkgs-fmt;
123+
script = "build";
124+
125+
extraBuildInputs = with (pkgsFor system); [
126+
python3
127+
pkg-config
128+
turbo
129+
];
130+
};
155131
});
132+
133+
devShells = forAllSystems (
134+
system: let
135+
pkgs = pkgsFor system;
136+
in {
137+
default = pkgs.mkShell {
138+
buildInputs = with pkgs; [
139+
nodejs
140+
pnpm
141+
python3
142+
pkg-config
143+
144+
# Add canvas build dependencies
145+
cairo
146+
pango
147+
libpng
148+
libjpeg
149+
giflib
150+
librsvg
151+
pixman
152+
153+
# Build essentials
154+
gcc
155+
gnumake
156+
157+
# Optional but helpful
158+
vips
159+
];
160+
161+
# Network access
162+
__noChroot = true;
163+
__impureHostDeps = ["/etc/resolv.conf"];
164+
165+
# Add pkg-config path
166+
shellHook = ''
167+
export PKG_CONFIG_PATH="${pkgs.cairo}/lib/pkgconfig:${pkgs.pango}/lib/pkgconfig:${pkgs.libpng}/lib/pkgconfig:$PKG_CONFIG_PATH"
168+
echo "Entering Eliza development environment with:"
169+
echo "Node.js $(node --version)"
170+
echo "pnpm $(pnpm --version)"
171+
'';
172+
};
173+
}
174+
);
175+
};
156176
}

0 commit comments

Comments
 (0)