Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
276 lines (246 loc) · 9.61 KB
/
Copy pathflake.nix
File metadata and controls
276 lines (246 loc) · 9.61 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# note: the vast majority of the code in here has been adapted from
#
# https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/build-mozilla-mach/default.nix
#
# and adapted for our use case.
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
flake-utils.url = "github:numtide/flake-utils?ref=main";
};
outputs = {
nixpkgs,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (
system: let
inherit (lib) enableFeature;
inherit (pkgs) lib stdenv pkgsCross;
pkgs = import nixpkgs {inherit system;};
# Target the LLVM version that rustc is built with for LTO.
llvmPackages0 = pkgs.rustc.llvmPackages;
llvmPackagesBuildBuild0 = pkgs.pkgsBuildBuild.rustc.llvmPackages;
# Use the llvm toolchain to stay consistent with the upstream Firefox build setup (and for LTO).
llvmPackages = llvmPackages0.override {
bootBintoolsNoLibc = null;
bootBintools = null;
};
llvmPackagesBuildBuild = llvmPackagesBuildBuild0.override {
bootBintoolsNoLibc = null;
bootBintools = null;
};
buildStdenv = pkgs.overrideCC llvmPackages.stdenv (
llvmPackages.stdenv.cc.override {
bintools = pkgs.stdenv.cc.bintools;
}
);
searchfox-cli = pkgs.rustPlatform.buildRustPackage rec {
pname = "searchfox-cli";
version = "0.15.0";
src = pkgs.fetchFromGitHub {
owner = "padenot";
repo = "searchfox-cli";
tag = "v${version}";
hash = "sha256-5D28uoHJcuiaC/NbLEViJ6Z9waWmBDj3o4NRLXVAxuE=";
};
cargoHash = "sha256-/LYstGJV3YWq3qjHn8QbiELKv4kaT4FRADGzA0u944E=";
# The workspace also contains a pyo3-based crate that we don't need.
buildAndTestSubdir = "searchfox-cli";
doCheck = false;
};
# Firefox requires cbindgen >= 0.29.4, but nixpkgs only has 0.29.2 at the time of writing.
rust-cbindgen = pkgs.rustPlatform.buildRustPackage (finalAttrs: {
pname = "rust-cbindgen";
version = "0.29.4";
src = pkgs.fetchFromGitHub {
owner = "mozilla";
repo = "cbindgen";
rev = "v${finalAttrs.version}";
hash = "sha256-leeHOwpzXuzg2cTjXehBnCsS+dvU4eIIFtWKeCee20U=";
};
cargoHash = "sha256-f6YoDoiVoh0BVPYHFO1FsdI4OCsF+LY72QaD57StdIQ=";
doCheck = false;
});
# Firefox requires NSS >= 3.131, but nixpkgs only has 3.124 at the time of writing.
nss_latest = pkgs.nss_latest.overrideAttrs (old: rec {
version = "3.131";
src = pkgs.fetchFromGitHub {
owner = "nss-dev";
repo = "nss";
rev = "NSS_${lib.replaceStrings ["."] ["_"] version}_RTM";
hash = "sha256-bhhlw4Q4hs4CUWPI4m+cdOStSTKPrZSRqQfWbhfgFzM=";
};
postPatch = (old.postPatch or "") + ''
sed -i '/^generate_pkg_config$/d' build.sh
'';
});
# Compile the wasm32 sysroot to build the RLBox Sandbox
# https://hacks.mozilla.org/2021/12/webassembly-and-back-again-fine-grained-sandboxing-in-firefox-95/
# We only link c++ libs here, our compiler wrapper can find wasi libc and crt itself.
wasiSysRoot = pkgs.runCommand "wasi-sysroot" {} ''
mkdir -p $out/lib/wasm32-wasi
for lib in ${pkgsCross.wasi32.llvmPackages.libcxx}/lib/*; do
ln -s $lib $out/lib/wasm32-wasi
done
'';
# Specifying --(dis|en)able-elf-hack on a platform for which it's not implemented will give `--disable-elf-hack is not available in this configuration`
isElfhackPlatform = stdenv:
stdenv.hostPlatform.isElf
&& (
stdenv.hostPlatform.isi686
|| stdenv.hostPlatform.isx86_64
|| stdenv.hostPlatform.isAarch32
|| stdenv.hostPlatform.isAarch64
);
elfhackSupport =
isElfhackPlatform stdenv && !(stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64);
configureFlags =
[
"--with-libclang-path=${lib.getLib llvmPackagesBuildBuild.libclang}/lib"
"--with-wasi-sysroot=${wasiSysRoot}"
# for firefox, host is buildPlatform, target is hostPlatform
"--host=${buildStdenv.buildPlatform.config}"
"--target=${buildStdenv.hostPlatform.config}"
]
++ lib.optional (isElfhackPlatform stdenv) (enableFeature elfhackSupport "elf-hack")
++ lib.optionals (!stdenv.hostPlatform.isDarwin) [
# MacOS builds use bundled versions of libraries: https://bugzilla.mozilla.org/show_bug.cgi?id=1776255
"--enable-system-pixman"
"--with-system-ffi"
# Mozilla vendors 10+ patches and ICU upstream is very slow to adopt them
# "--with-system-icu"
"--with-system-jpeg"
"--with-system-libevent"
"--with-system-libvpx"
"--with-system-nspr"
"--with-system-nss"
"--with-system-png" # needs APNG support
"--with-system-webp"
"--with-system-zlib"
"--with-onnx-runtime=${lib.getLib pkgs.onnxruntime}/lib"
];
# note: unlike the nixpkgs build, we don't tell mach to use our python version as
# the firefox build system is not really set up for that, and I ran into a bunch
# of annoying bugs because of `MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system`.
shell =
''
# Set predictable directories for build and state
export MOZ_OBJDIR=$(pwd)/engine/objdir
export MOZBUILD_STATE_PATH=$TMPDIR/mozbuild
# Don't try to send libnotify notifications during build
export MOZ_NOSPAM=1
# AS=as in the environment causes build failure
# https://bugzilla.mozilla.org/show_bug.cgi?id=1497286
unset AS
# RBox WASM Sandboxing
export WASM_CC=${pkgsCross.wasi32.stdenv.cc}/bin/${pkgsCross.wasi32.stdenv.cc.targetPrefix}cc
export WASM_CXX=${pkgsCross.wasi32.stdenv.cc}/bin/${pkgsCross.wasi32.stdenv.cc.targetPrefix}c++
''
+ lib.optionalString pkgs.stdenv.hostPlatform.isMusl ''
# linking firefox hits the vm.max_map_count kernel limit with the default musl allocator
# TODO: Default vm.max_map_count has been increased, retest without this
export LD_PRELOAD=${pkgs.mimalloc}/lib/libmimalloc.so
''
+ ''
export GLIDE_MOZCONFIG_CONTENT="${builtins.concatStringsSep "\n" (map (s: "ac_add_options " + s) configureFlags)}"
'';
in {
devShells.default = pkgs.mkShell.override {stdenv = buildStdenv;} {
nativeBuildInputs = with pkgs;
[
(pnpm.override {nodejs = nodejs_24;})
nodejs_24
autoconf
cargo
gnum4
llvmPackagesBuildBuild.bintools
makeBinaryWrapper
perl
python3
rust-cbindgen
rustPlatform.bindgenHook
rustc
sccache
unzip
which
# crash reporter
dump_syms
patchelf
searchfox-cli
]
++ lib.optionals (!stdenv.hostPlatform.isDarwin) [
pkg-config
wrapGAppsHook3
]
++ lib.optionals stdenv.hostPlatform.isDarwin [rsync]
++ lib.optionals stdenv.hostPlatform.isx86 [nasm];
buildInputs = with pkgs;
[
bzip2
file
libGL
libGLU
libstartup_notification
perl
zip
libkrb5
gcc.cc.lib
]
++ lib.optionals stdenv.hostPlatform.isDarwin [
apple-sdk_26
cups
]
++ lib.optionals (!stdenv.hostPlatform.isDarwin) [
dbus
dbus-glib
fontconfig
freetype
glib
gtk3
libffi
libevent
libjpeg
libpng
libvpx
libwebp
nspr
pango
libx11
libxcursor
libxdamage
libxext
libxft
libxi
libxrender
libxt
libxtst
pixman
xorgproto
zlib
nss_latest
alsa-lib
libjack2
libpulseaudio # only headers are needed
sndio
libxkbcommon
libdrm
]
++ lib.optional (!stdenv.hostPlatform.isMusl) jemalloc;
env =
{
# if not explicitly set, wrong cc from buildStdenv would be used
HOST_CC = "${llvmPackagesBuildBuild.stdenv.cc}/bin/cc";
HOST_CXX = "${llvmPackagesBuildBuild.stdenv.cc}/bin/c++";
}
// lib.optionalAttrs stdenv.hostPlatform.isMusl {
# Firefox relies on nonstandard behavior of the glibc dynamic linker. It re-uses
# previously loaded libraries even though they are not in the rpath of the newly loaded binary.
# On musl we have to explicitly set the rpath to include these libraries.
LDFLAGS = "-Wl,-rpath,${placeholder "out"}/lib/glide";
};
shellHook = shell;
};
}
);
}