-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflake.nix
188 lines (146 loc) · 5.55 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
crane.url = "github:ipetkov/crane";
};
outputs =
{
self,
flake-utils,
crane,
nixpkgs,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
};
craneLib = crane.mkLib pkgs;
inherit (pkgs) lib;
queryFilter = path: _type: null != builtins.match ".*/query-.*\.json$" path;
sqlFilter = path: _type: null != builtins.match ".*\.sql$" path;
sqlOrQueryOrCargo =
path: type:
(queryFilter path type) || (sqlFilter path type) || (craneLib.filterCargoSources path type);
src = lib.cleanSourceWith {
src = craneLib.path ./.;
filter = sqlOrQueryOrCargo;
};
# Common arguments can be set here to avoid repeating them later
commonArgs = {
inherit src;
strictDeps = true;
buildInputs = lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.Security
];
};
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Build the actual crate itself, reusing the dependency
# artifacts from above.
website-server = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
doCheck = false;
}
);
in
{
checks = {
inherit website-server;
website-server-tests = craneLib.mkCargoDerivation (
commonArgs
// {
inherit cargoArtifacts;
pnameSuffix = "-test";
buildPhaseCargoCommand = ''
TEMP_DIR=$(mktemp -d)
DATA_DIR=$TEMP_DIR/data
SOCKET_DIR="$TEMP_DIR/sockets"
SOCKET_URL="$(echo $SOCKET_DIR | sed 's/\//%2f/g')"
export DATABASE_URL="postgresql://$SOCKET_URL:5432/postgres"
mkdir -p "$DATA_DIR" "$SOCKET_DIR"
${pkgs.postgresql}/bin/initdb -D "$DATA_DIR" --locale=C.utf8
${pkgs.postgresql}/bin/pg_ctl -D $DATA_DIR -o "-k $SOCKET_DIR -h \"\"" start
${pkgs.sqlx-cli}/bin/sqlx migrate run
cargoWithProfile test --locked
${pkgs.postgresql}/bin/pg_ctl -D "$DATA_DIR" stop
'';
}
);
};
formatter = pkgs.alejandra;
defaultPackage = website-server;
packages = rec {
default = website-server;
database = pkgs.writeScriptBin "run.sh" ''
#!/usr/bin/env bash
DATA_DIR="$PWD/db/data"
SOCKET_DIR="$PWD/db/sockets"
SOCKET_URL="$(echo $SOCKET_DIR | sed 's/\//%2f/g')"
export DATABASE_URL="postgresql://$SOCKET_URL:5432/postgres"
mkdir -p "$DATA_DIR" "$SOCKET_DIR"
echo Initializing the Database
${pkgs.postgresql}/bin/initdb -D "$DATA_DIR" --locale=C.utf8
${pkgs.postgresql}/bin/pg_ctl -D $DATA_DIR -o "-k $SOCKET_DIR" start
trap "${pkgs.postgresql}/bin/pg_ctl -D $DATA_DIR stop; exit" SIGINT
${pkgs.sqlx-cli}/bin/sqlx migrate run --source ./migrations --database-url $DATABASE_URL
read -p "Press enter to stop the database"
${pkgs.postgresql}/bin/pg_ctl -D "$DATA_DIR" stop
'';
full = pkgs.writeScriptBin "run.sh" ''
#!/usr/bin/env bash
DATA_DIR="$PWD/db/data"
SOCKET_DIR="$PWD/db/sockets"
SOCKET_URL="$(echo $SOCKET_DIR | sed 's/\//%2f/g')"
export DATABASE_URL="postgresql://$SOCKET_URL:5432/postgres"
mkdir -p "$DATA_DIR" "$SOCKET_DIR"
${pkgs.postgresql}/bin/initdb -D "$DATA_DIR" --locale=C.utf8
${pkgs.postgresql}/bin/pg_ctl -D $DATA_DIR status
ALREADY_RUNNING=$?
if [ ! "$ALREADY_RUNNING" -eq 0 ]; then
echo Initializing the Database
${pkgs.postgresql}/bin/pg_ctl -D $DATA_DIR -o "-k $SOCKET_DIR -h \"\"" start
trap "${pkgs.postgresql}/bin/pg_ctl -D $DATA_DIR stop; exit" SIGINT
fi
echo Starting the server
${default}/bin/fscs-website-backend \
--database-url $DATABASE_URL \
--content-dir test \
--auth-url https://auth.inphima.de/application/o/authorize/ \
--token-url https://auth.inphima.de/application/o/token/ \
--user-info https://auth.inphima.de/application/o/userinfo/ \
$@
if [ ! "$ALREADY_RUNNING" -eq 0 ]; then
echo Stopping the Database
${pkgs.postgresql}/bin/pg_ctl -D "$DATA_DIR" stop
fi
'';
};
apps.default = flake-utils.lib.mkApp {
drv = self.packages.${system}.full;
exePath = "/bin/run.sh";
};
apps.database = flake-utils.lib.mkApp {
drv = self.packages.${system}.database;
exePath = "/bin/run.sh";
};
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
cargo
rustc
rustfmt
clippy
sqlx-cli
postgresql
];
};
}
);
}