-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.nix
478 lines (359 loc) · 15.5 KB
/
lib.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
{ pkgs
, osCrane
, self
, defaultMizeConfig
, mize_modules
, localSystem
, nixpkgs
, rust-overlay
, crane
, fenix
, stdenv
, ...
}:
rec {
getCrossSystem = system: let
crossSystem = (if builtins.isString system then (pkgs.lib.systems.parse.mkSystemFromString system) else system)
// rec {
name = if crossSystem.vendor != "unknown"
then "${crossSystem.cpu.name}-${crossSystem.kernel.name}-${crossSystem.abi.name}"
else "${crossSystem.cpu.name}-${crossSystem.vendor.name}-${crossSystem.kernel.name}-${crossSystem.abi.name}";
nameFull = "${crossSystem.cpu.name}-${crossSystem.vendor.name}-${crossSystem.kernel.name}-${crossSystem.abi.name}";
config = nameFull;
};
in crossSystem;
buildMizeForSystem = system: let
crossSystem = getCrossSystem system;
pkgsCross = import nixpkgs { inherit localSystem crossSystem; overlays = [ rust-overlay.overlays.default ]; };
pkgsNative = import nixpkgs { inherit localSystem; overlays = [ rust-overlay.overlays.default ]; };
# get a list of all modules
# aka folders with a mize_module.nix in them
module_list_drv = path: pkgs.stdenv.mkDerivation {
name = "mize_module_list";
dontUnpack = true;
configurePhase = "";
buildPhase = ''
mkdir -p $out
touch $out/modules_to_build
find ${path} -not -type d -name mize_module.nix | while read p; do
(echo "$p" >> $out/modules_to_build; echo "found module $p")
done
exit 0
'';
};
mize_module_path = builtins.getEnv "MIZE_MODULE_PATH";
dirs_from_path = pkgs.lib.lists.remove "" (pkgs.lib.strings.splitString ":" mize_module_path);
dirs_in_nix_store = map (path: builtins.fetchGit {
url = path;
}) dirs_from_path;
from_env_var = map findModules dirs_in_nix_store;
mizeMdules = let
from_mize_modules_repo = findModules mize_modules;
mize_module_no_repo = builtins.getEnv "MIZE_MODULE_NO_REPO";
in pkgs.lib.lists.flatten (from_env_var ++ (if mize_module_no_repo != "" then [] else from_mize_modules_repo));
findModules = path: let
mize_module_no_externals = builtins.getEnv "MIZE_MODULE_NO_Externals";
module_list_string = builtins.readFile "${module_list_drv path}/modules_to_build";
module_list = pkgs.lib.lists.remove "" (pkgs.lib.strings.splitString "\n" module_list_string);
getExternals = path: map findModules ((pkgs.callPackage ((import path).externals or (args: [])) {}));
in module_list ++ (if mize_module_no_externals != "" then [] else (map getExternals module_list));
craneLib =
if crossSystem.kernel.name == "windows"
then
(crane.mkLib pkgs).overrideToolchain (fenix.packages.${localSystem}.combine [
#fenix.packages.${localSystem}.minimal.rustc
#fenix.packages.${localSystem}.minimal.cargo
fenix.packages.${localSystem}.stable.toolchain
fenix.packages.${localSystem}.targets."${crossSystem.cpu.name}-pc-windows-gnu".stable.toolchain
])
else if crossSystem.cpu.name == "wasm32"
then
(crane.mkLib pkgs).overrideToolchain (fenix.packages.${localSystem}.combine [
fenix.packages.${localSystem}.targets.wasm32-unknown-unknown.latest.toolchain
fenix.packages.${localSystem}.latest.toolchain
])
else if crossSystem.name == "x86_64-linux-gnu"
then
builtins.trace "x86_64-linux-gnu hackfix...."
(crane.mkLib pkgs).overrideToolchain (fenix.packages.${localSystem}.combine [
fenix.packages.${localSystem}.stable.toolchain
])
else
(crane.mkLib pkgsCross).overrideToolchain (p: p.rust-bin.stable.latest.default)
;
toolchain_version_drv = pkgs.stdenv.mkDerivation {
name = "toolchain_version_drv";
dontUnpack = true;
configurePhase = "";
buildPhase = ''
mkdir -p $out
touch $out/rustc-version
echo hiiiiiiiiiiiiiiiiiiiiiiii
#echo 'rustc 1.80.1 (3f5fd8dd4 2024-08-06)' > $out/rustc-version
${craneLib.cargo}/bin/rustc --version > $out/rustc-version
exit 0
'';
};
mkSelString = attrs: builtins.toJSON (attrs // {
inherit toolchain_version;
system = crossSystem.nameFull;
mize_version = main-default.version;
});
toolchain_version = pkgs.lib.strings.removeSuffix "\n" (builtins.readFile "${toolchain_version_drv}/rustc-version");
mkMizeModule = attrs: pkgsCross.stdenv.mkDerivation ( rec {
name = attrs.modName;
inherit (attrs) modName;
selector_string = mkSelString (attrs.select or {} // {
inherit (attrs) modName;
});
hash = builtins.substring 0 32 (builtins.hashString "sha256" selector_string);
} // attrs);
########## build Rust Module
mkMizeRustModule = attrs: craneLib.buildPackage (attrs // {
MIZE_BUILD_CONFIG = mizeBuildConfigStr;
selector_string = mkSelString (attrs.select or {} // {
inherit (attrs) modName;
});
# rename the so library, in case the lib.name in your Cargo.toml is not like mize_module_mylib
postInstall = (attrs.postInstall or "") + ''
if [ ! -f "$out/lib/libmize_module_${attrs.modName}" ]; then
echo RENAMING $out/lib/${attrs.modName}.so to $out/lib/libmize_module_${attrs.modName}.so
mv $out/lib/${attrs.modName}.so to $out/lib/libmize_module_${attrs.modName}.so || true
echo RENAMING $out/lib/${attrs.modName}.a to $out/lib/libmize_module_${attrs.modName}.a
mv $out/lib/${attrs.modName}.a to $out/lib/libmize_module_${attrs.modName}.a && true # ignore if there is no .a file || true
echo "$FILE"
fi
'';
}
# linux specific stuff
// (if crossSystem.kernel.name == "linux" then {
"CARGO_TARGET_${builtins.replaceStrings ["-"] ["_"] (pkgsCross.lib.strings.toUpper crossSystem.nameFull)}_LINKER" = "${pkgsCross.stdenv.cc.targetPrefix}cc";
HOST_CC = "${pkgsCross.stdenv.cc.nativePrefix}cc";
TARGET_CC = "${pkgsCross.stdenv.cc.targetPrefix}cc";
CARGO_BUILD_TARGET = crossSystem.nameFull;
nativeBuildInputs = attrs.nativeBuildInputs or [] ++ [
pkgsCross.stdenv.cc
];
} else {})
# add wasm stuff
// (if crossSystem.cpu.name == "wasm32" then {
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
# checks fail on wasm
doCheck = false;
} else {})
# add windows stuff
// (if crossSystem.kernel.name == "windows" then {
strictDeps = true;
doCheck = false;
CARGO_BUILD_TARGET = "${crossSystem.cpu.name}-pc-windows-gnu";
# fixes issues related to libring
TARGET_CC = "${pkgs.pkgsCross.mingwW64.stdenv.cc}/bin/${pkgs.pkgsCross.mingwW64.stdenv.cc.targetPrefix}cc";
#fixes issues related to openssl
OPENSSL_DIR = "${pkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include/";
depsBuildBuild = [
pkgs.pkgsCross.mingwW64.stdenv.cc
pkgs.pkgsCross.mingwW64.windows.pthreads
];
} else {})
);
buildModule = path: extraArgs: pkgs.callPackage ((import path).module or (attrs: null)) ({
inherit buildMizeForSystem mizeBuildConfig mizeBuildConfigStr;
inherit mkSelString craneLib toolchain_version;
inherit mkMizeModule mkMizeRustModule buildModule findModules crossSystem pkgsCross pkgsNative mkMizeRustShell;
mize_version = main-default.version;
} // extraArgs );
buildLib = path: extraArgs: pkgs.callPackage ((import path).lib or (args: {})) {
inherit mkSelString craneLib toolchain_version;
inherit mkMizeModule mkMizeRustModule buildModule findModules crossSystem pkgsCross pkgsNative mkMizeRustShell;
mize_version = main-default.version;
};
mizeBuildConfig = {
namespace = "mize.buildtime.ns";
module_url = "c2vi.dev";
selector = builtins.fromJSON (mkSelString {});
};
mizeBuildConfigStr = let
settingsFormat = pkgs.formats.toml { };
in settingsFormat.generate "mize-build-config.toml" {
config = mizeBuildConfig;
};
####### build mize
main-default = craneLib.buildPackage ({
src = "${self}";
cargoExtraArgs = "--bin mize --features os-target";
# CARGO_PROFILE = "dev"; does not work...
strictDeps = true;
nativeBuildInputs = [
pkgsCross.buildPackages.pkg-config
#pkgsCross.buildPackages.nasm
#pkgsCross.buildPackages.cmake
];
buildInputs = [
(if crossSystem.nameFull == "x86_64-unknown-linux-gnu" then pkgs.openssl else pkgsCross.openssl)
];
MIZE_BUILD_CONFIG = mizeBuildConfigStr;
# patch the interpreter to run on most linux-gnu distros
postBuild =
if crossSystem.cpu.name == "x86_64"
then "patchelf --set-interpreter /lib64/ld-linux-x86-64.so.2 /build/source/target/release/mize"
else if crossSystem.cpu.name == "aarch64"
then "patchelf --set-interpreter /lib64/ld-linux-aarch64.so.2 /build/source/target/${crossSystem.nameFull}/release/mize"
else ""
;
}
// (if crossSystem.name == "x86_64-linux-gnu" then builtins.trace "another x86_64-linux hackfix..." {} else {
CARGO_BUILD_TARGET = crossSystem.nameFull;
"CARGO_TARGET_${builtins.replaceStrings ["-"] ["_"] (pkgsCross.lib.strings.toUpper crossSystem.nameFull)}_LINKER" = "${pkgsCross.stdenv.cc.targetPrefix}cc";
HOST_CC = "${pkgsCross.stdenv.cc.nativePrefix}cc";
TARGET_CC = "${pkgsCross.stdenv.cc.targetPrefix}cc";
})
);
main-win = craneLib.buildPackage {
src = craneLib.cleanCargoSource ./.;
#nativeBuildInputs = [ pkgsCross.buildPackages.nasm pkgsCross.buildPackages.cmake ];
strictDeps = true;
doCheck = false;
cargoExtraArgs = "--bin mize --features os-target";
CARGO_BUILD_TARGET = "${crossSystem.cpu.name}-pc-windows-gnu";
MIZE_BUILD_CONFIG = mizeBuildConfigStr;
# fixes issues related to libring
TARGET_CC = "${pkgs.pkgsCross.mingwW64.stdenv.cc}/bin/${pkgs.pkgsCross.mingwW64.stdenv.cc.targetPrefix}cc";
#fixes issues related to openssl
OPENSSL_DIR = "${pkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include/";
depsBuildBuild = [
pkgs.pkgsCross.mingwW64.stdenv.cc
pkgs.pkgsCross.mingwW64.windows.pthreads
];
};
# - https://github.com/ipetkov/crane/issues/362#issuecomment-1683220603
wasmArtifacts = craneLib.buildDepsOnly ({
src = self;
doCheck = false; # tests does not work in wasm
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
cargoExtraArgs = "--features wasm-target --no-default-features";
RUSTFLAGS="-C linker=wasm-ld";
buildInputs = with pkgs; [ cargo-binutils lld ];
});
main-wasm = craneLib.mkCargoDerivation {
name = "mize-npm-package";
# i can't use CARGO_BUILD_TARGET to set the target
# because then the cargo install run by wasm-pack tries to build the wasm-bindgen-cli for a wasm target...
src = self;
doInstallCargoArtifacts = false;
cargoArtifacts = wasmArtifacts;
doCheck = false;
buildPhaseCargoCommand = ''
mkdir -p $out/pkg
HOME=$(mktemp -d fake-homeXXXX) wasm-pack build --target no-modules --out-dir $out/pkg --scope=c2vi -- --features wasm-target --no-default-features
'';
postInstall = ''
rm -rf $out/target.tar.zst
'';
buildInputs = with pkgsNative; [ wasm-bindgen-cli binaryen wasm-pack ];
MIZE_BUILD_CONFIG = mizeBuildConfigStr;
};
################# dev Shels ###################
mkMizeRustShell = attrs: mkMizeModuleShell (attrs // {
#_shell_type = "rust";
nativeBuildInputs = attrs.nativeBuildInputs or [] ++ [
(fenix.packages."x86_64-linux".combine [
fenix.packages."x86_64-linux".stable.toolchain
fenix.packages."x86_64-linux".targets.wasm32-unknown-unknown.stable.toolchain
fenix.packages."x86_64-linux".stable.toolchain
])
# the shell script, to mk the dist folder, for a standard rust module
(pkgs.writeShellApplication {
name = "mkdist";
# should we use the folder, that cargo locate-project gives us??? or just cp ./target/debug/
text = ''
mkdir -p ./dist
mkdir -p ./dist/lib
cp ./target/debug/libmize_module*.so ./dist/lib
cp ./target/debug/libmize_module*.a ./dist/lib || true
'';
})
];
});
mkMizeModuleShell = attrs: pkgs.mkShell (attrs // {
MIZE_BUILD_CONFIG = mizeBuildConfigStr;
});
mainDevShell = pkgs.mkShell {
buildInputs = with pkgs; [
wasm-pack #pkg-config openssl #cargo rustc
cargo-generate
(fenix.packages.${localSystem}.combine [
fenix.packages.${system}.stable.toolchain
fenix.packages.${system}.targets.wasm32-unknown-unknown.stable.toolchain
fenix.packages.${system}.stable.toolchain
])
openssl
];
nativeBuildInputs = with pkgs; [
pkg-config
];
MIZE_BUILD_CONFIG = mizeBuildConfigStr;
shellHook = ''
export MIZE_CONFIG_FILE=${self}/test-config.toml
'';
};
################# output attrset ###################
in rec {
inherit toolchain_version_drv pkgsCross craneLib;
inherit mize_module_path dirs_from_path dirs_in_nix_store from_env_var;
devShell = mainDevShell;
main =
if crossSystem.kernel.name == "windows"
then main-win
else if crossSystem.cpu.name == "wasm32"
then main-wasm
else main-default
;
system = crossSystem;
modulesListDrv = module_list_drv mize_modules;
modulesFileList = mizeMdules;
modulesLibList = map (mod: buildLib mod {}) modulesFileList;
modulesLib = pkgs.lib.lists.foldr (a: b: b // a) {} modulesLibList;
modulesList = pkgs.lib.lists.remove null (map (mod: (buildModule mod modulesLib)) modulesFileList);
modules = builtins.listToAttrs ( map ( mod: { name = mod.modName; value = mod; } ) modulesList );
};
mkInstallPhase = mize: ''
# install mize
mkdir -p $out/mize/${mize.system.name}
cp -r ${mize.main}/* $out/mize/${mize.system.name}/
# intall the modules
${mkModulesInstallPhase mize.modulesList}
'';
mkModulesInstallPhase = modules: pkgs.lib.concatStringsSep "\n" (map mkModuleInstallPhase modules);
mkModuleInstallPhase = module: let
hash = module.hach;
in ''
echo got module: ${module.name}
echo out: $out
echo hash: ${hash}
cp --no-preserve=mode,ownership -r ${module}/* $out/mize/dist/${hash}-${module.modName}
${pkgs.gnutar}/bin/tar -czf $out/mize/dist/${hash}-${module.modName}.tar.gz -C ${module} .
echo '${module.selector_string}' > $out/mize/dist/${hash}-${module.modName}/selector
'';
webfiles = systems: stdenv.mkDerivation {
name = "mize-webfiles";
dontUnpack = true;
# so that those binaries run on average linux-gnu systems
dontPatchShebangs = true;
buildPhase = ''
'';
installPhase = ''
mkdir -p $out
mkdir -p $out/mize
mkdir -p $out/mize/dist
'' + pkgs.lib.concatStringsSep "\n" (map mkInstallPhase (map buildMizeForSystem systems));
nativeBuildInputs = [
];
};
moduleShells = system: let
mizeFor = buildMizeForSystem system;
in builtins.listToAttrs ( map ( mod: { name = mod.modName; value = (mod.devShell); } ) mizeFor.modulesList );
}