btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / development / compilers / zig / generic.nix
blob3da36c083bd3b159029d87d8e147863518270825
1 { lib
2 , stdenv
3 , fetchFromGitHub
4 , cmake
5 , llvmPackages
6 , libxml2
7 , zlib
8 , coreutils
9 , callPackage
10 , ...
13 args:
15 stdenv.mkDerivation (finalAttrs: {
16   pname = "zig";
18   src = fetchFromGitHub {
19     owner = "ziglang";
20     repo = "zig";
21     rev = finalAttrs.version;
22     inherit (args) hash;
23   };
25   nativeBuildInputs = [
26     cmake
27     llvmPackages.llvm.dev
28   ];
30   buildInputs = [
31     libxml2
32     zlib
33   ] ++ (with llvmPackages; [
34     libclang
35     lld
36     llvm
37   ]);
39   # On Darwin, Zig calls std.zig.system.darwin.macos.detect during the build,
40   # which parses /System/Library/CoreServices/SystemVersion.plist and
41   # /System/Library/CoreServices/.SystemVersionPlatform.plist to determine the
42   # OS version. This causes the build to fail during stage 3 with
43   # OSVersionDetectionFail when the sandbox is enabled.
44   __impureHostDeps = lib.optionals stdenv.hostPlatform.isDarwin [
45     "/System/Library/CoreServices/.SystemVersionPlatform.plist"
46     "/System/Library/CoreServices/SystemVersion.plist"
47   ];
49   preBuild = ''
50     export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-cache";
51   '';
53   # Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't
54   # work in Nix's sandbox. Use env from our coreutils instead.
55   postPatch = if lib.versionAtLeast args.version "0.12" then ''
56     substituteInPlace lib/std/zig/system.zig \
57       --replace "/usr/bin/env" "${coreutils}/bin/env"
58   '' else ''
59     substituteInPlace lib/std/zig/system/NativeTargetInfo.zig \
60       --replace-fail "/usr/bin/env" "${coreutils}/bin/env"
61   '';
63   doInstallCheck = true;
64   installCheckPhase = ''
65     runHook preInstallCheck
67     $out/bin/zig test -I $src/test $src/test/behavior.zig
69     runHook postInstallCheck
70   '';
72   passthru = {
73     hook = callPackage ./hook.nix {
74       zig = finalAttrs.finalPackage;
75     };
76     cc = callPackage ./cc.nix {
77       zig = finalAttrs.finalPackage;
78     };
79     stdenv = callPackage ./stdenv.nix {
80       zig = finalAttrs.finalPackage;
81     };
82   };
84   meta = {
85     description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
86     homepage = "https://ziglang.org/";
87     changelog = "https://ziglang.org/download/${finalAttrs.version}/release-notes.html";
88     license = lib.licenses.mit;
89     maintainers = with lib.maintainers; [ andrewrk ] ++ lib.teams.zig.members;
90     mainProgram = "zig";
91     platforms = lib.platforms.unix;
92   };
93 } // removeAttrs args [ "hash" ])