at master 4.2 kB view raw
1{ 2 lib, 3 stdenv, 4 fetchFromGitHub, 5 cmake, 6 llvmPackages, 7 xcbuild, 8 targetPackages, 9 libxml2, 10 ninja, 11 zlib, 12 coreutils, 13 callPackage, 14 version, 15 hash, 16 overrideCC, 17 wrapCCWith, 18 wrapBintoolsWith, 19 ... 20}@args: 21 22stdenv.mkDerivation (finalAttrs: { 23 pname = "zig"; 24 inherit version; 25 26 src = fetchFromGitHub { 27 owner = "ziglang"; 28 repo = "zig"; 29 rev = finalAttrs.version; 30 inherit hash; 31 }; 32 33 patches = args.patches or [ ]; 34 35 nativeBuildInputs = [ 36 cmake 37 (lib.getDev llvmPackages.llvm.dev) 38 ninja 39 ] 40 ++ lib.optionals stdenv.hostPlatform.isDarwin [ 41 # provides xcode-select, which is required for SDK detection 42 xcbuild 43 ]; 44 45 buildInputs = [ 46 libxml2 47 zlib 48 ] 49 ++ (with llvmPackages; [ 50 libclang 51 lld 52 llvm 53 ]); 54 55 cmakeFlags = [ 56 # file RPATH_CHANGE could not write new RPATH 57 (lib.cmakeBool "CMAKE_SKIP_BUILD_RPATH" true) 58 # ensure determinism in the compiler build 59 (lib.cmakeFeature "ZIG_TARGET_MCPU" "baseline") 60 # always link against static build of LLVM 61 (lib.cmakeBool "ZIG_STATIC_LLVM" true) 62 ]; 63 64 outputs = [ 65 "out" 66 "doc" 67 ]; 68 69 strictDeps = true; 70 71 # On Darwin, Zig calls std.zig.system.darwin.macos.detect during the build, 72 # which parses /System/Library/CoreServices/SystemVersion.plist and 73 # /System/Library/CoreServices/.SystemVersionPlatform.plist to determine the 74 # OS version. This causes the build to fail during stage 3 with 75 # OSVersionDetectionFail when the sandbox is enabled. 76 __impureHostDeps = lib.optionals stdenv.hostPlatform.isDarwin [ 77 "/System/Library/CoreServices/.SystemVersionPlatform.plist" 78 ]; 79 80 preBuild = '' 81 export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-cache"; 82 ''; 83 84 postPatch = 85 # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't 86 # work in Nix's sandbox. Use env from our coreutils instead. 87 '' 88 substituteInPlace lib/std/zig/system.zig \ 89 --replace-fail "/usr/bin/env" "${lib.getExe' coreutils "env"}" 90 '' 91 # Zig tries to access xcrun and xcode-select at the absolute system path to query the macOS SDK 92 # location, which does not work in the darwin sandbox. 93 # Upstream issue: https://github.com/ziglang/zig/issues/22600 94 # Note that while this fix is already merged upstream and will be included in 0.14+, 95 # we can't fetchpatch the upstream commit as it won't cleanly apply on older versions, 96 # so we substitute the paths instead. 97 + lib.optionalString (stdenv.hostPlatform.isDarwin && lib.versionOlder finalAttrs.version "0.14") '' 98 substituteInPlace lib/std/zig/system/darwin.zig \ 99 --replace-fail /usr/bin/xcrun xcrun \ 100 --replace-fail /usr/bin/xcode-select xcode-select 101 ''; 102 103 postBuild = 104 if lib.versionAtLeast finalAttrs.version "0.14" then 105 '' 106 stage3/bin/zig build langref --zig-lib-dir $(pwd)/stage3/lib/zig 107 '' 108 else 109 '' 110 stage3/bin/zig build langref 111 ''; 112 113 postInstall = '' 114 install -Dm444 ../zig-out/doc/langref.html -t $doc/share/doc/zig-${finalAttrs.version}/html 115 ''; 116 117 doInstallCheck = true; 118 installCheckPhase = '' 119 runHook preInstallCheck 120 121 $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig 122 123 runHook postInstallCheck 124 ''; 125 126 passthru = import ./passthru.nix { 127 inherit 128 lib 129 stdenv 130 callPackage 131 wrapCCWith 132 wrapBintoolsWith 133 overrideCC 134 targetPackages 135 ; 136 zig = finalAttrs.finalPackage; 137 }; 138 139 meta = { 140 description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software"; 141 homepage = "https://ziglang.org/"; 142 changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html"; 143 license = lib.licenses.mit; 144 maintainers = with lib.maintainers; [ andrewrk ]; 145 teams = [ lib.teams.zig ]; 146 mainProgram = "zig"; 147 platforms = lib.platforms.unix; 148 # Zig 0.15.1 fails some tests on x86_64-darwin thus we mark it broken 149 # see https://github.com/ziglang/zig/issues/24974 150 broken = 151 stdenv.hostPlatform.system == "x86_64-darwin" && lib.versionAtLeast finalAttrs.version "0.15"; 152 }; 153})