Skip to content

Commit b7185b6

Browse files
committed
feat(nix): add development environment flake
- Add Nix flake for reproducible development environment - Auto-detect Node.js and pnpm versions from package.json - Configure development tools (gcc, python3, vips, etc.) - Add shell hook with helpful development commands
1 parent eecaa73 commit b7185b6

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

flake.nix

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
{
2+
description = "Eliza development environment";
3+
4+
inputs = {
5+
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = {
10+
self,
11+
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;
26+
};
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;
69+
};
70+
71+
# Define specific pnpm version
72+
pnpm = pkgs.stdenv.mkDerivation {
73+
name = "pnpm";
74+
version = versions.pnpm;
75+
76+
src = fetchPnpm versions.pnpm;
77+
78+
buildInputs = [nodejs];
79+
80+
installPhase = ''
81+
# Create directories
82+
mkdir -p $out/{lib,bin}
83+
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
93+
94+
chmod +x $out/bin/pnpm
95+
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
99+
'';
100+
101+
dontStrip = true;
102+
};
103+
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+
'';
152+
};
153+
154+
formatter = pkgs.nixpkgs-fmt;
155+
});
156+
}

0 commit comments

Comments
 (0)