Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
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 patches ? [ ], 17 overrideCC, 18 wrapCCWith, 19 wrapBintoolsWith, 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 # 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 postPatch = 87 let 88 zigSystemPath = 89 if lib.versionAtLeast finalAttrs.version "0.12" then 90 "lib/std/zig/system.zig" 91 else 92 "lib/std/zig/system/NativeTargetInfo.zig"; 93 in 94 '' 95 substituteInPlace ${zigSystemPath} \ 96 --replace-fail "/usr/bin/env" "${lib.getExe' coreutils "env"}" 97 '' 98 # Zig tries to access xcrun and xcode-select at the absolute system path to query the macOS SDK 99 # location, which does not work in the darwin sandbox. 100 # Upstream issue: https://github.com/ziglang/zig/issues/22600 101 # Note that while this fix is already merged upstream and will be included in 0.14+, 102 # we can't fetchpatch the upstream commit as it won't cleanly apply on older versions, 103 # so we substitute the paths instead. 104 + lib.optionalString (stdenv.hostPlatform.isDarwin && lib.versionOlder finalAttrs.version "0.14") '' 105 substituteInPlace lib/std/zig/system/darwin.zig \ 106 --replace-fail /usr/bin/xcrun xcrun \ 107 --replace-fail /usr/bin/xcode-select xcode-select 108 ''; 109 110 postBuild = 111 if lib.versionAtLeast finalAttrs.version "0.14" then 112 '' 113 stage3/bin/zig build langref --zig-lib-dir $(pwd)/stage3/lib/zig 114 '' 115 else if lib.versionAtLeast finalAttrs.version "0.13" then 116 '' 117 stage3/bin/zig build langref 118 '' 119 else 120 '' 121 stage3/bin/zig run ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig 122 ''; 123 124 postInstall = 125 if lib.versionAtLeast finalAttrs.version "0.13" then 126 '' 127 install -Dm444 ../zig-out/doc/langref.html -t $doc/share/doc/zig-${finalAttrs.version}/html 128 '' 129 else 130 '' 131 install -Dm444 langref.html -t $doc/share/doc/zig-${finalAttrs.version}/html 132 ''; 133 134 doInstallCheck = true; 135 installCheckPhase = '' 136 runHook preInstallCheck 137 138 $out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I $src/test $src/test/behavior.zig 139 140 runHook postInstallCheck 141 ''; 142 143 passthru = import ./passthru.nix { 144 inherit 145 lib 146 stdenv 147 callPackage 148 wrapCCWith 149 wrapBintoolsWith 150 overrideCC 151 targetPackages 152 ; 153 zig = finalAttrs.finalPackage; 154 }; 155 156 meta = { 157 description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software"; 158 homepage = "https://ziglang.org/"; 159 changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html"; 160 license = lib.licenses.mit; 161 maintainers = with lib.maintainers; [ andrewrk ]; 162 teams = [ lib.teams.zig ]; 163 mainProgram = "zig"; 164 platforms = lib.platforms.unix; 165 }; 166})