-
Notifications
You must be signed in to change notification settings - Fork 1
/
flake.nix
172 lines (154 loc) · 4.64 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{
description = "virtual environments";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
devshell = {
url = "github:numtide/devshell";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
};
outputs = {
self,
flake-utils,
devshell,
nixpkgs,
}:
flake-utils.lib.eachDefaultSystem (system: {
devShell = let
pkgs = import nixpkgs {
inherit system;
overlays = [devshell.overlay];
};
pkgWithCategory = category: package: {inherit package category;};
start_pg = pkgs.writeScript "start-pg" ''
#!${pkgs.stdenv.shell}
pg_pid=""
set -euo pipefail
export PGHOST=$PANDORA_DB_HOST
export PGPORT=$PANDORA_DB_PORT
export PGDATABASE=$PANDORA_DB_DATABASE
# TODO: port
pg_ctl -D "$HOME/.pgdata" -w start || (echo pg_ctl failed; exit 1)
until psql postgres -c "SELECT 1" > /dev/null 2>&1 ; do
echo waiting for pg
sleep 0.5
done
'';
init_pg = pkgs.writeScript "init-pg" ''
#!${pkgs.stdenv.shell}
pg_pid=""
set -euo pipefail
export PGHOST=$PANDORA_DB_HOST
export PGPORT=$PANDORA_DB_PORT
export PGDATABASE=$PANDORA_DB_DATABASE
initdb -D $HOME/.pgdata
echo "unix_socket_directories = '$(mktemp -d)'" >> $HOME/.pgdata/postgresql.conf
# TODO: port
pg_ctl -D "$HOME/.pgdata" -w start || (echo pg_ctl failed; exit 1)
until psql postgres -c "SELECT 1" > /dev/null 2>&1 ; do
echo waiting for pg
sleep 0.5
done
psql postgres -w -c "CREATE DATABASE $PGDATABASE"
psql postgres -w -c "CREATE ROLE $PANDORA_DB_USER WITH LOGIN PASSWORD '$PANDORA_DB_PASSWORD'"
psql postgres -w -c "GRANT ALL PRIVILEGES ON DATABASE $PGDATABASE TO $PANDORA_DB_USER"
'';
stop_pg = pkgs.writeScript "stop-pg" ''
#!${pkgs.stdenv.shell}
pg_pid=""
set -euo pipefail
pg_ctl -D $HOME/.pgdata -w -m immediate stop
'';
in
pkgs.devshell.mkShell {
name = "cquest";
commands = [
{
name = "pginit";
help = "init psql service";
category = "database";
command = "${init_pg}";
}
{
name = "pgstart";
help = "start psql service";
category = "database";
command = "${start_pg}";
}
{
name = "pgstop";
help = "stop psql service";
category = "database";
command = "${stop_pg}";
}
{
name = "migrate";
help = "migrate database";
category = "database";
command = "yarn workspace @cquest/db migrate:up -d $PRJ_ROOT/shared/db/ormconfig.ts";
}
{
name = "watch";
help = "watch for code changes";
category = "dev";
command = "yarn watch";
}
{
name = "clean";
help = "clean compiled code";
category = "dev";
command = "yarn clean";
}
{
name = "start-pandora";
help = "start-pandora";
category = "dev";
command = "yarn workspace @cquest/pandora dev";
}
];
env = [
{
name = "PANDORA_PREFIX";
value = "!";
}
{
name = "PANDORA_URL_IMAGES_PREFIX";
value = "https://data.fenrir.moe/";
}
{
name = "PANDORA_IMAGES_SUFFIX";
value = ".png";
}
{
name = "PANDORA_DB_HOST";
value = "localhost";
}
{
name = "PANDORA_DB_PORT";
value = "5432";
}
{
name = "PANDORA_DB_DATABASE";
value = "cquest";
}
{
name = "PANDORA_DB_SCHEMA";
value = "public";
}
{
name = "PANDORA_DB_USER";
value = "pandora";
}
{
name = "PANDORA_DB_PASSWORD";
value = "pandora";
}
];
packages = with pkgs; [nodejs-18_x yarn postgresql_12];
};
});
}