Skip to content

Commit 91a2c05

Browse files
committed
feat: nix dev shell
1 parent 3ccc108 commit 91a2c05

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ next-env.d.ts
4747
uploads*/
4848
*.crt
4949
*.key
50-
generated
50+
generated
51+
52+
# nix dev env
53+
/.psql_db/

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,24 @@ Create a pull request on GitHub. If your PR does not pass the action checks, the
198198

199199
Here's how to setup Zipline for development
200200

201+
#### Nix
202+
203+
If you have [Nix](https://nixos.org/) installed, you can use the provided dev shell to get started quickly. Just run:
204+
205+
```bash
206+
nix develop
207+
```
208+
209+
This will start a postgres server, and drop you into a shell with the necessary tools installed:
210+
211+
- nodejs
212+
- corepack
213+
- git
214+
- ffmpeg
215+
- postgres server
216+
217+
After hopping into the dev shell, you can follow the instructions below (skipping the prerequisites) to setup a configuration and start the server.
218+
201219
#### Prerequisites
202220

203221
- nodejs (lts -> 20.x, 22.x)

flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
description = "dev env for zipline";
3+
4+
inputs = {
5+
# node 24.4.1, postgres 17
6+
nixpkgs.url = "github:nixos/nixpkgs/b527e89270879aaaf584c41f26b2796be634bc9d";
7+
flake-utils.url = "github:numtide/flake-utils";
8+
};
9+
10+
outputs =
11+
{
12+
self,
13+
nixpkgs,
14+
flake-utils,
15+
}:
16+
flake-utils.lib.eachDefaultSystem (
17+
system:
18+
let
19+
pkgs = import nixpkgs {
20+
inherit system;
21+
};
22+
23+
nodejs = pkgs.nodejs_24;
24+
postgres = pkgs.postgresql;
25+
psqlDir = ".psql_db/data";
26+
27+
psqlUsername = "postgres";
28+
psqlPassword = "postgres";
29+
in
30+
{
31+
devShells.default = pkgs.mkShell {
32+
name = "zipline-dev";
33+
34+
buildInputs = [
35+
nodejs
36+
postgres
37+
38+
pkgs.git
39+
pkgs.corepack
40+
pkgs.ffmpeg
41+
];
42+
43+
shellHook = ''
44+
export PGDATA="$PWD/${psqlDir}"
45+
export PGUSER="${psqlUsername}"
46+
export PGPASSWORD="${psqlPassword}"
47+
export PGPORT=5432
48+
49+
if [ ! -d "$PGDATA" ]; then
50+
echo "Initializing PostgreSQL data directory at $PGDATA"
51+
initdb -D "$PGDATA" --username="$PGUSER" --pwfile=<(echo "$PGPASSWORD")
52+
fi
53+
54+
# listen on localhost
55+
echo "host all all 127.0.0.1/32 password" >> "$PGDATA/pg_hba.conf"
56+
echo "host all all ::1/128 password" >> "$PGDATA/pg_hba.conf"
57+
sed -i "s/^#\?listen_addresses.*/listen_addresses = 'localhost'/" "$PGDATA/postgresql.conf"
58+
59+
echo "Starting PostgreSQL..."
60+
pg_ctl -D "$PGDATA" -o "-p $PGPORT" -w start
61+
62+
echo -e "PostgreSQL is ready at postgresql://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres\n\n"
63+
64+
stop_postgres() {
65+
echo "Stopping PostgreSQL..."
66+
pg_ctl -D "$PGDATA" stop
67+
}
68+
69+
# trap pg to stop on exiting the dev shell
70+
trap stop_postgres EXIT
71+
72+
# use zsh if zsh is available
73+
if command -v zsh >/dev/null 2>&1; then
74+
zsh
75+
else
76+
$SHELL
77+
fi
78+
79+
exit
80+
'';
81+
};
82+
}
83+
);
84+
}

0 commit comments

Comments
 (0)