nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 fetchFromCodeberg,
5 cmake,
6 llvmPackages,
7 xcbuild,
8 libxml2,
9 ninja,
10 zlib,
11 coreutils,
12 callPackage,
13 version,
14 hash,
15 overrideCC,
16 wrapCCWith,
17 wrapBintoolsWith,
18 ...
19}@args:
20
21stdenv.mkDerivation (finalAttrs: {
22 pname = "zig";
23 inherit version;
24
25 src = fetchFromCodeberg {
26 owner = "ziglang";
27 repo = "zig";
28 rev = finalAttrs.version;
29 inherit hash;
30 };
31
32 patches = args.patches or [ ];
33
34 nativeBuildInputs = [
35 cmake
36 (lib.getDev llvmPackages.llvm.dev)
37 ninja
38 ]
39 ++ lib.optionals stdenv.hostPlatform.isDarwin [
40 # provides xcode-select, which is required for SDK detection
41 xcbuild
42 ];
43
44 buildInputs = [
45 libxml2
46 zlib
47 ]
48 ++ (with llvmPackages; [
49 libclang
50 lld
51 llvm
52 ]);
53
54 cmakeFlags = [
55 # file RPATH_CHANGE could not write new RPATH
56 (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
57 # ensure determinism in the compiler build
58 (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline")
59 # always link against static build of LLVM
60 (lib.cmakeBool "ZIG_STATIC_LLVM" true)
61 ];
62
63 outputs = [
64 "out"
65 "doc"
66 ];
67
68 strictDeps = true;
69
70 # On Darwin, Zig calls std.zig.system.darwin.macos.detect during the build,
71 # which parses /System/Library/CoreServices/SystemVersion.plist and
72 # /System/Library/CoreServices/.SystemVersionPlatform.plist to determine the
73 # OS version. This causes the build to fail during stage 3 with
74 # OSVersionDetectionFail when the sandbox is enabled.
75 __impureHostDeps = lib.optionals stdenv.hostPlatform.isDarwin [
76 "/System/Library/CoreServices/.SystemVersionPlatform.plist"
77 ];
78
79 preBuild = ''
80 export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-cache";
81 '';
82
83 postPatch =
84 # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't
85 # work in Nix's sandbox. Use env from our coreutils instead.
86 ''
87 substituteInPlace lib/std/zig/system.zig \
88 --replace-fail "/usr/bin/env" "${lib.getExe' coreutils "env"}"
89 ''
90 # Zig tries to access xcrun and xcode-select at the absolute system path to query the macOS SDK
91 # location, which does not work in the darwin sandbox.
92 # Upstream issue: https://github.com/ziglang/zig/issues/22600
93 # Note that while this fix is already merged upstream and will be included in 0.14+,
94 # we can't fetchpatch the upstream commit as it won't cleanly apply on older versions,
95 # so we substitute the paths instead.
96 + lib.optionalString (stdenv.hostPlatform.isDarwin && lib.versionOlder finalAttrs.version "0.14") ''
97 substituteInPlace lib/std/zig/system/darwin.zig \
98 --replace-fail /usr/bin/xcrun xcrun \
99 --replace-fail /usr/bin/xcode-select xcode-select
100 '';
101
102 postBuild =
103 if lib.versionAtLeast finalAttrs.version "0.14" then
104 ''
105 stage3/bin/zig build langref --zig-lib-dir $(pwd)/stage3/lib/zig
106 ''
107 else
108 ''
109 stage3/bin/zig build langref
110 '';
111
112 postInstall = ''
113 install -Dm444 ../zig-out/doc/langref.html -t $doc/share/doc/zig-${finalAttrs.version}/html
114 '';
115
116 doInstallCheck = true;
117 installCheckPhase = ''
118 runHook preInstallCheck
119
120 $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig
121
122 runHook postInstallCheck
123 '';
124
125 passthru = import ./passthru.nix {
126 inherit
127 stdenv
128 callPackage
129 wrapCCWith
130 wrapBintoolsWith
131 overrideCC
132 ;
133 zig = finalAttrs.finalPackage;
134 };
135
136 env = {
137 # This zig_default_optimize_flag below is meant to avoid CPU feature impurity in
138 # Nixpkgs. However, this flagset is "unstable": it is specifically meant to
139 # be controlled by the upstream development team - being up to that team
140 # exposing or not that flags to the outside (especially the package manager
141 # teams).
142
143 # Because of this hurdle, @andrewrk from Zig Software Foundation proposed
144 # some solutions for this issue. Hopefully they will be implemented in
145 # future releases of Zig. When this happens, this flagset should be
146 # revisited accordingly.
147
148 # Below are some useful links describing the discovery process of this 'bug'
149 # in Nixpkgs:
150
151 # https://github.com/NixOS/nixpkgs/issues/169461
152 # https://github.com/NixOS/nixpkgs/issues/185644
153 # https://github.com/NixOS/nixpkgs/pull/197046
154 # https://github.com/NixOS/nixpkgs/pull/241741#issuecomment-1624227485
155 # https://github.com/ziglang/zig/issues/14281#issuecomment-1624220653
156 zig_default_cpu_flag = "-Dcpu=baseline";
157
158 zig_default_optimize_flag =
159 if lib.versionAtLeast finalAttrs.version "0.12" then
160 "--release=safe"
161 else if lib.versionAtLeast finalAttrs.version "0.11" then
162 "-Doptimize=ReleaseSafe"
163 else
164 "-Drelease-safe=true";
165 };
166
167 setupHook = ./setup-hook.sh;
168
169 # while xcrun is already included in the darwin stdenv, Zig also needs
170 # xcode-select (provided by xcbuild) for SDK detection
171 propagatedNativeBuildInputs = lib.optionals stdenv.hostPlatform.isDarwin [ xcbuild ];
172
173 meta = {
174 description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
175 homepage = "https://ziglang.org/";
176 changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html";
177 license = lib.licenses.mit;
178 maintainers = with lib.maintainers; [ andrewrk ];
179 teams = [ lib.teams.zig ];
180 mainProgram = "zig";
181 platforms = lib.platforms.unix;
182 };
183})