-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzig.nix
149 lines (124 loc) · 4.01 KB
/
zig.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
lib
, installDocs ? false
, zigSystem
, zigHook
, version
, release
, stdenv
, stdenvNoCC
, callPackage
, fetchurl
, zig-shell-completions
, cmake
, llvmPackages_19
, libxml2
, zlib
, coreutils
, bubblewrap
, writeScriptBin
}:
with builtins;
with lib;
let
meta-for = release: {
homepage = "https://ziglang.org/";
description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
license = licenses.mit;
platforms = platforms.unix;
maintainers = []; # needed by the setup hook
mainProgram = "zig";
} // optionalAttrs (release ? notes) {
changelog = release.notes;
};
in {
src = with llvmPackages_19; stdenv.mkDerivation (finalAttrs: {
pname = "zig";
inherit version;
outputs = [ "out" ] ++ optionals (installDocs) [ "doc" ];
src = fetchurl {
url = release.src.tarball;
sha256 = release.src.shasum;
};
nativeBuildInputs = [ cmake llvm.dev ];
buildInputs = [ libxml2 zlib libclang lld llvm ];
cmakeFlags = [
# file RPATH_CHANGE could not write new RPATH
"-DCMAKE_SKIP_BUILD_RPATH=ON"
# always link against static build of LLVM
"-DZIG_STATIC_LLVM=ON"
# ensure determinism in the compiler build
"-DZIG_TARGET_MCPU=baseline"
];
env.ZIG_GLOBAL_CACHE_DIR = "$TMPDIR/zig-cache";
# Zig's build looks at /usr/bin/env to find dynamic linking info. This doesn't
# work in Nix's sandbox. Use env from our coreutils instead.
postPatch = ''
substituteInPlace lib/std/zig/system/NativeTargetInfo.zig --replace "/usr/bin/env" "${coreutils}/bin/env" || true
substituteInPlace lib/std/zig/system.zig --replace "/usr/bin/env" "${coreutils}/bin/env" || true
'';
postBuild = optionalString (installDocs) ''
stage3/bin/zig run --cache-dir "$TMPDIR/zig-test-cache" ../tools/docgen.zig -- ../doc/langref.html.in langref.html --zig $PWD/stage3/bin/zig
'';
postInstall = optionalString (installDocs) ''
install -Dm444 -t $doc/share/doc/zig-$version/html langref.html
'';
doInstallCheck = true;
installCheckPhase = ''
runHook preInstallCheck
$out/bin/zig test --cache-dir "$TMPDIR/zig-test-cache" -I ../test ../test/behavior.zig
runHook postInstallCheck
'';
passthru = {
inherit (release) date notes stdDocs docs src;
hook = callPackage zigHook {
zig = finalAttrs.finalPackage;
};
};
meta = meta-for release;
});
has-bin = release ? ${zigSystem};
bin = if release ? ${zigSystem} then stdenvNoCC.mkDerivation (finalAttrs: {
pname = "zig";
inherit version;
outputs = [ "out" ] ++ optionals (installDocs) [ "doc" ];
src = fetchurl {
url = release.${zigSystem}.tarball;
sha256 = release.${zigSystem}.shasum;
};
phases = [ "unpackPhase" "installPhase" ];
installPhase = ''
mkdir -p $out/{bin,lib}
cp -r lib/* $out/lib
install -Dm755 zig $out/bin/zig
install -m644 LICENSE $out/LICENSE
'' + optionalString (installDocs) ''
mkdir -p $out/doc
if [[ -d docs ]]; then
cp -r docs $out/doc
else
cp -r doc $out/doc
fi
'';
passthru = {
inherit (release) date notes stdDocs docs src;
inherit (release.${zigSystem}) size;
hook = callPackage zigHook {
zig = if (stdenvNoCC.isLinux) then
# Wrap binary package zig on linux so /usr/bin/env can be found inside a sandbox
writeScriptBin "zig" ''
args=()
for d in /*; do
args+=("--dev-bind" "$d" "$d")
done
${bubblewrap}/bin/bwrap "''${args[@]}" \
--bind ${coreutils} /usr \
-- ${finalAttrs.finalPackage}/bin/zig "$@"
''
else finalAttrs.finalPackage;
};
};
meta = meta-for release;
}) else throw "There is no zig-${version} binary available for ${zigSystem}, use .src to compile from source";
shell-completions = zig-shell-completions;
}