-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathflake.nix
235 lines (206 loc) · 8.02 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
{
description = "multicorn2 nix development environment";
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system: let
debugBuild = false;
multicornVersion = "3.1";
pkgs = nixpkgs.legacyPackages.${system};
requiredPythonPackages = ps: (
# Such that we pass the UNSUPPORTS_SQLALCHEMY check in Makefile and can run the SQLAlchemy tests
[ ps.sqlalchemy ] ++ ps.sqlalchemy.optional-dependencies.postgresql
);
devPostgresql = pkgs.postgresql_15.overrideAttrs (oldAttrs: {} // pkgs.lib.optionalAttrs debugBuild { dontStrip = true; }); # If debug symbols are needed.
devPython = pkgs.python310.withPackages (ps: (requiredPythonPackages ps));
testPythonVersions = with pkgs; [
python39
python310
python311
# python312 # tests are currently broken
# python313 # tests are currently broken
];
testPostgresVersions = with pkgs; [
postgresql_13
postgresql_14
postgresql_15
postgresql_16
postgresql_17
];
testVersionCombos = pkgs.lib.cartesianProductOfSets {
python = testPythonVersions;
postgres = testPostgresVersions;
};
makeTestSuiteName = { python, postgres }: let
pgMajorVersion = builtins.head (builtins.split "\\." postgres.version);
in
"test_pg${pgMajorVersion}_py${builtins.replaceStrings ["."] [""] python.pythonVersion}";
makeMulticornPostgresExtension = target_python: target_postgresql: pkgs.stdenv.mkDerivation {
pname = "multicorn2";
version = multicornVersion;
# Local source directory, but only including the files necessary for building the Postgres extension...
src = [
./src
./sql
./Makefile
./multicorn.control
];
unpackPhase = ''
for srcFile in $src; do
echo "cp $srcFile ..."
cp -r $srcFile $(stripHash $srcFile)
done
chmod -R +w .
'';
buildInputs = target_postgresql.buildInputs ++ [
target_postgresql
(target_python.withPackages (ps: (requiredPythonPackages ps)))
];
installPhase = ''
runHook preInstall
install -D multicorn${target_postgresql.dlSuffix} -t $out/lib/
install -D sql/multicorn--''${version#v}.sql -t $out/share/postgresql/extension
install -D multicorn.control -t $out/share/postgresql/extension
runHook postInstall
'';
separateDebugInfo = true;
};
makeMulticornPythonPackage = target_python: target_postgresql: target_python.pkgs.buildPythonPackage rec {
pname = "multicorn2-python";
version = multicornVersion;
# Local source directory, but only including the files necessary for building the Python extension...
src = [
./src/utils.c
./src/multicorn.h
./python
./setup.py
./pyproject.toml
./multicorn.control
];
unpackPhase = ''
for srcFile in $src; do
echo "cp $srcFile ..."
cp -r $srcFile $(stripHash $srcFile)
done
mkdir -p src
mv *.[ch] src/
chmod -R +w .
'';
nativeBuildInputs = [ target_postgresql ];
separateDebugInfo = true;
};
makePostgresWithPlPython = test_python: test_postgresql:
(test_postgresql.override {
# write_filesystem.sql uses plpython3u, so, we enable it... This causes 25 rebuilds of postgresql (each PG
# version * each Python version)... it might be worth either removing / rewriting this test suite,
# making it optional, or using at least one consistent version of python for the plpython3u build... or
# just using a new custom build cache to avoid rebuilds.
pythonSupport = true;
python3 = test_python;
});
makeTestSuite = test_python: test_postgresql: pkgs.stdenv.mkDerivation {
name = "multicorn2-python-test";
phases = [ "unpackPhase" "checkPhase" "installPhase" ];
doCheck = true;
# Local source directory, but only including the files necessary for running the regression tests...
src = [
./Makefile
./test-3.9
./test-3.10
./test-3.11
./test-common
];
unpackPhase = ''
for srcFile in $src; do
echo "cp $srcFile ..."
cp -r $srcFile $(stripHash $srcFile)
done
'';
nativeCheckInputs = [
(
(makePostgresWithPlPython test_python test_postgresql).withPackages (ps: [
(makeMulticornPostgresExtension test_python test_postgresql)
])
)
(test_python.withPackages (ps:
[(makeMulticornPythonPackage test_python test_postgresql)]
++
(requiredPythonPackages ps)
))
];
checkPhase = ''
runHook preCheck
python -c "import sqlalchemy;import psycopg2"
set +e
make easycheck
RESULT=$?
set -e
if [[ $RESULT -ne 0 ]]; then
echo "easycheck failed"
cat /build/regression.diffs
exit $RESULT
fi
runHook postCheck
'';
installPhase = "touch $out";
};
in {
devShells.default = pkgs.mkShell {
buildInputs = [
devPython
devPostgresql
];
};
packages = rec {
# Tests probably shouldn't be in "packages"? But flake schema doesn't seem to support a separate "tests"
# output...
# The true targets for tests are either for a specific PG & Python version, or for all of them.
# eg:
# nix build .#testSuites.test_pg12_py39
# nix build .#allTestSuites
testSuites = pkgs.lib.foldl' (acc: combo:
let
name = makeTestSuiteName combo;
testSuite = makeTestSuite combo.python combo.postgres;
in
acc // { ${name} = testSuite; }
) {} testVersionCombos;
allTestSuites = (pkgs.stdenv.mkDerivation {
name = "multicorn2-test-all";
buildInputs = builtins.attrValues testSuites;
phases = [ "installPhase" ];
installPhase = "touch $out";
});
# The postgresWithPlPython steps are used so that we can build Postgres instances that include the the
# plpython3u extension. This isn't the default in Nixpkgs, so we need to build it ourselves. But because we have
# to build a lot of them (one each for each version of python and postgresql), we create a separate target so that
# we can build them and then push them to a binary cache.
# eg.
# nix build .#postgresWithPlPython.postgres-test_pg12_py39
# nix build .#allPostgresWithPlPython
postgresWithPlPython = pkgs.lib.foldl' (acc: combo:
let
name = "postgres-${makeTestSuiteName combo}";
testSuite = makePostgresWithPlPython combo.python combo.postgres;
in
acc // { ${name} = testSuite; }
) {} testVersionCombos;
allPostgresWithPlPython = (pkgs.stdenv.mkDerivation {
name = "multicorn2-postgres-all";
buildInputs = builtins.attrValues postgresWithPlPython;
phases = [ "installPhase" ];
# In order to make sure that allPostgresWithPlPython can be pushed to a binary cache and include all the built
# Postgres instances, the installPhase creates symlinks to the built instances.
installPhase = ''
mkdir -p $out
${pkgs.lib.concatStringsSep "\n" (pkgs.lib.mapAttrsToList (name: outPath: ''
ln -s ${outPath} $out/${name}
'') postgresWithPlPython)}
touch $out/placeholder
'';
});
};
});
}