-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelease.nix
92 lines (78 loc) · 1.97 KB
/
release.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
{ nixpkgs ? import ./nix/pinnedNix.nix { } }:
let
inherit (nixpkgs) pkgs;
pg = pkgs.postgresql_12;
sqitch = pkgs.sqitchPg;
haskellPkgs = pkgs.haskell.packages.ghc8104;
hello = haskellPkgs.callCabal2nix "hello" ./. { };
envs = ''
export SOCKET_DIR=/tmp/socket_dir_hello_db
export PGDATABASE=dev
export PGUSER=dev
export PGPASSWORD=pass
export PGPORT=5444
export PGDATA=$SOCKET_DIR/pg
export PGSOCK=$SOCKET_DIR/.s.PGSQL.$PGPORT
export TPG_DB=$PGDATABASE
export TPG_USER=$PGUSER
export TPG_PASS=$PGPASSWORD
export TPG_PORT=$PGPORT
export TPG_SOCK=$PGSOCK
#export TPG_DEBUG=true
'';
migrations = pkgs.stdenv.mkDerivation {
name = "migrations";
buildInputs = [];
src = ./migrations;
installPhase = ''
mkdir -p $out/migrations
cp . $out/migrations -r
'';
};
in
pkgs.haskell.lib.overrideCabal hello (old: {
buildDepends = (old.buildDepends or []) ++ [pg migrations sqitch];
preBuild = ''
${old.preBuild or ""}
${envs}
mkdir $SOCKET_DIR
echo debuh=$TPG_DEBUG
echo "PREBVUILD"
echo debuh=$TPG_DEBUG
echo "Init DB"
initdb $PGDATA
echo "Start DB"
postgres -D $PGDATA -k $SOCKET_DIR &
echo "Waiting..."
while [[ ! -e $PGSOCK ]]; do
echo "wait"
sleep 0.1;
done
sleep 1
echo "Create user"
createuser -h $SOCKET_DIR -U $(whoami) -s $PGUSER
echo "Create db"
createdb -h $SOCKET_DIR -O $PGUSER $PGDATABASE
echo "DB Created"
sleep 1
echo "apply migrations"
cd ${migrations}/migrations
ls
sqitch deploy
# very important
cd -
'';
postInstall = ''
${old.postInstall or ""}
${envs}
echo "Kill Postgres"
pg_ctl stop || true
echo "Waiting"
while [[ -e $PGSOCK ]]; do
echo "wait"
sleep 0.1;
done
echo "Delete socket dir"
rm -rf $SOCKET_DIR
'';
})