1{
2 lib,
3 makeSetupHook,
4 zig,
5 stdenv,
6 xcbuild,
7}:
8
9makeSetupHook {
10 name = "zig-hook";
11
12 propagatedBuildInputs =
13 [ zig ]
14 # while xcrun is already included in the darwin stdenv, Zig also needs
15 # xcode-select (provided by xcbuild) for SDK detection
16 ++ lib.optionals stdenv.hostPlatform.isDarwin [ xcbuild ];
17
18 substitutions = {
19 # This zig_default_flags below is meant to avoid CPU feature impurity in
20 # Nixpkgs. However, this flagset is "unstable": it is specifically meant to
21 # be controlled by the upstream development team - being up to that team
22 # exposing or not that flags to the outside (especially the package manager
23 # teams).
24
25 # Because of this hurdle, @andrewrk from Zig Software Foundation proposed
26 # some solutions for this issue. Hopefully they will be implemented in
27 # future releases of Zig. When this happens, this flagset should be
28 # revisited accordingly.
29
30 # Below are some useful links describing the discovery process of this 'bug'
31 # in Nixpkgs:
32
33 # https://github.com/NixOS/nixpkgs/issues/169461
34 # https://github.com/NixOS/nixpkgs/issues/185644
35 # https://github.com/NixOS/nixpkgs/pull/197046
36 # https://github.com/NixOS/nixpkgs/pull/241741#issuecomment-1624227485
37 # https://github.com/ziglang/zig/issues/14281#issuecomment-1624220653
38
39 zig_default_flags =
40 let
41 releaseType =
42 if lib.versionAtLeast zig.version "0.12" then
43 "--release=safe"
44 else if lib.versionAtLeast zig.version "0.11" then
45 "-Doptimize=ReleaseSafe"
46 else
47 "-Drelease-safe=true";
48 in
49 [
50 "-Dcpu=baseline"
51 releaseType
52 ];
53 };
54
55 passthru = { inherit zig; };
56
57 meta = {
58 description = "Setup hook for using the Zig compiler in Nixpkgs";
59 inherit (zig.meta) maintainers platforms broken;
60 };
61} ./setup-hook.sh