-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.nix
163 lines (148 loc) · 4.82 KB
/
shell.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
{ isDevelopment ? true }:
let
# Update packages with `nixpkgs-update` command
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/7cc0bff31a3a705d3ac4fdceb030a17239412210.tar.gz") { };
pythonLibs = with pkgs; [
file.out
libxml2.out
zlib.out
stdenv.cc.cc.lib
];
python' = with pkgs; (symlinkJoin {
name = "python";
paths = [
# Enable compiler optimizations when in production
(if isDevelopment then python313 else python313.override { enableOptimizations = true; })
];
buildInputs = [ makeWrapper ];
postBuild = ''
wrapProgram "$out/bin/python3.13" --prefix LD_LIBRARY_PATH : "${lib.makeLibraryPath pythonLibs}"
'';
});
packages' = with pkgs; [
python'
uv
ruff
coreutils
(postgresql_17_jit.withPackages (ps: [ ps.postgis ]))
valkey
# Scripts
# -- Alembic
(writeShellScriptBin "alembic-migration" ''
set -e
name=$1
if [ -z "$name" ]; then
read -p "Database migration name: " name
fi
alembic -c config/alembic.ini revision --autogenerate --message "$name"
'')
(writeShellScriptBin "alembic-upgrade" "alembic -c config/alembic.ini upgrade head")
# -- Supervisor
(writeShellScriptBin "dev-start" ''
set -e
pid=$(cat data/supervisor/supervisord.pid 2> /dev/null || echo "")
if [ -n "$pid" ] && $(grep -q "supervisord" "/proc/$pid/cmdline" 2> /dev/null); then
echo "Supervisor is already running"
exit 0
fi
if [ ! -f data/postgres/PG_VERSION ]; then
initdb -D data/postgres \
--no-instructions \
--locale-provider=icu \
--icu-locale=und \
--no-locale \
--text-search-config=pg_catalog.simple \
--auth=trust \
--username=postgres
fi
mkdir -p /tmp/openaedmap-postgres data/supervisor
supervisord -c config/supervisord.conf
echo "Supervisor started"
echo "Waiting for Postgres to start..."
time_start=$(date +%s)
while ! pg_isready -q -h /tmp/openaedmap-postgres; do
elapsed=$(($(date +%s) - $time_start))
if [ $elapsed -gt 10 ]; then
tail -n 15 data/supervisor/supervisord.log data/supervisor/postgres.log
echo "Postgres startup timeout, see above logs for details"
dev-stop
exit 1
fi
sleep 0.1
done
echo "Postgres started, running migrations"
alembic-upgrade
'')
(writeShellScriptBin "dev-stop" ''
set -e
pid=$(cat data/supervisor/supervisord.pid 2> /dev/null || echo "")
if [ -n "$pid" ] && $(grep -q "supervisord" "/proc/$pid/cmdline" 2> /dev/null); then
kill -INT "$pid"
echo "Supervisor stopping..."
while $(kill -0 "$pid" 2> /dev/null); do sleep 0.1; done
echo "Supervisor stopped"
else
echo "Supervisor is not running"
fi
'')
(writeShellScriptBin "dev-restart" ''
set -ex
dev-stop
dev-start
'')
(writeShellScriptBin "dev-clean" ''
set -e
dev-stop
rm -rf data/postgres
'')
(writeShellScriptBin "dev-logs-postgres" "tail -f data/supervisor/postgres.log")
# -- Misc
(writeShellScriptBin "make-version" ''
sed -i -r "s|VERSION = '([0-9.]+)'|VERSION = '\1.$(date +%y%m%d)'|g" config.py
'')
(writeShellScriptBin "nixpkgs-update" ''
set -e
hash=$(
curl --silent --location \
https://prometheus.nixos.org/api/v1/query \
-d "query=channel_revision{channel=\"nixpkgs-unstable\"}" | \
grep --only-matching --extended-regexp "[0-9a-f]{40}")
sed -i -E "s|/nixpkgs/archive/[0-9a-f]{40}\.tar\.gz|/nixpkgs/archive/$hash.tar.gz|" shell.nix
echo "Nixpkgs updated to $hash"
'')
(writeShellScriptBin "docker-build" ''
set -e
if command -v podman &> /dev/null; then docker() { podman "$@"; } fi
docker load < "$(nix-build --no-out-link)"
'')
];
shell' = with pkgs; lib.optionalString isDevelopment ''
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
export PYTHONNOUSERSITE=1
export PYTHONPATH=""
export TZ=UTC
current_python=$(readlink -e .venv/bin/python || echo "")
current_python=''${current_python%/bin/*}
[ "$current_python" != "${python'}" ] && rm -rf .venv/
echo "Installing Python dependencies"
export UV_COMPILE_BYTECODE=1
export UV_PYTHON="${python'}/bin/python"
uv sync --frozen
echo "Activating Python virtual environment"
source .venv/bin/activate
if [ -f .env ]; then
echo "Loading .env file"
set -o allexport
source .env set
set +o allexport
else
echo "Skipped loading .env file (not found)"
fi
'' + lib.optionalString (!isDevelopment) ''
make-version
'';
in
pkgs.mkShell {
buildInputs = packages';
shellHook = shell';
}