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