1{
2 pkgs,
3 system,
4 lib,
5 stdenv,
6
7 apple-sdk_15,
8 darwin,
9 darwinMinVersionHook,
10
11 cargo-about,
12 cargo-bundle,
13 crane,
14 rustPlatform,
15 rustToolchain,
16
17 copyDesktopItems,
18 envsubst,
19 fetchFromGitHub,
20 makeFontsConf,
21 makeWrapper,
22
23 alsa-lib,
24 cmake,
25 curl,
26 fontconfig,
27 freetype,
28 git,
29 glib,
30 libdrm,
31 libgbm,
32 libgit2,
33 libglvnd,
34 libva,
35 libxcomposite,
36 libxdamage,
37 libxext,
38 libxfixes,
39 libxkbcommon,
40 libxrandr,
41 nodejs_22,
42 openssl,
43 perl,
44 pkg-config,
45 protobuf,
46 sqlite,
47 vulkan-loader,
48 wayland,
49 xorg,
50 zlib,
51 zstd,
52
53 withGLES ? false,
54 profile ? "release",
55 commitSha ? null,
56}:
57assert withGLES -> stdenv.hostPlatform.isLinux;
58let
59 mkIncludeFilter =
60 root': path: type:
61 let
62 # note: under lazy-trees this introduces an extra copy
63 root = toString root' + "/";
64 relPath = lib.removePrefix root path;
65 topLevelIncludes = [
66 "crates"
67 "assets"
68 "extensions"
69 "script"
70 "tooling"
71 "Cargo.toml"
72 ".config" # nextest?
73 ".cargo"
74 ];
75 firstComp = builtins.head (lib.path.subpath.components relPath);
76 in
77 builtins.elem firstComp topLevelIncludes;
78
79 craneLib = crane.overrideToolchain rustToolchain;
80 gpu-lib = if withGLES then libglvnd else vulkan-loader;
81 commonArgs =
82 let
83 zedCargoLock = builtins.fromTOML (builtins.readFile ../crates/zed/Cargo.toml);
84 stdenv' = stdenv;
85 in
86 rec {
87 pname = "zed-editor";
88 version =
89 zedCargoLock.package.version
90 + "-nightly"
91 + lib.optionalString (commitSha != null) "+${builtins.substring 0 7 commitSha}";
92 src = builtins.path {
93 path = ../.;
94 filter = mkIncludeFilter ../.;
95 name = "source";
96 };
97
98 cargoLock = ../Cargo.lock;
99
100 nativeBuildInputs = [
101 cmake
102 copyDesktopItems
103 curl
104 perl
105 pkg-config
106 protobuf
107 # Pin cargo-about to 0.8.2. Newer versions don't work with the current license identifiers
108 # See https://github.com/zed-industries/zed/pull/44012
109 (cargo-about.overrideAttrs (
110 new: old: rec {
111 version = "0.8.2";
112
113 src = fetchFromGitHub {
114 owner = "EmbarkStudios";
115 repo = "cargo-about";
116 tag = version;
117 sha256 = "sha256-cNKZpDlfqEXeOE5lmu79AcKOawkPpk4PQCsBzNtIEbs=";
118 };
119
120 cargoHash = "sha256-NnocSs6UkuF/mCM3lIdFk+r51Iz2bHuYzMT/gEbT/nk=";
121
122 # NOTE: can drop once upstream uses `finalAttrs` here:
123 # https://github.com/NixOS/nixpkgs/blob/10214747f5e6e7cb5b9bdf9e018a3c7b3032f5af/pkgs/build-support/rust/build-rust-package/default.nix#L104
124 #
125 # See (for context): https://github.com/NixOS/nixpkgs/pull/382550
126 cargoDeps = rustPlatform.fetchCargoVendor {
127 inherit (new) src;
128 hash = new.cargoHash;
129 patches = new.cargoPatches or [ ];
130 name = new.cargoDepsName or new.finalPackage.name;
131 };
132 }
133 ))
134 rustPlatform.bindgenHook
135 ]
136 ++ lib.optionals stdenv'.hostPlatform.isLinux [ makeWrapper ]
137 ++ lib.optionals stdenv'.hostPlatform.isDarwin [
138 (cargo-bundle.overrideAttrs (
139 new: old: {
140 version = "0.6.1-zed";
141 src = fetchFromGitHub {
142 owner = "zed-industries";
143 repo = "cargo-bundle";
144 rev = "2be2669972dff3ddd4daf89a2cb29d2d06cad7c7";
145 hash = "sha256-cSvW0ND148AGdIGWg/ku0yIacVgW+9f1Nsi+kAQxVrI=";
146 };
147 cargoHash = "sha256-urn+A3yuw2uAO4HGmvQnKvWtHqvG9KHxNCCWTiytE4k=";
148
149 # NOTE: can drop once upstream uses `finalAttrs` here:
150 # https://github.com/NixOS/nixpkgs/blob/10214747f5e6e7cb5b9bdf9e018a3c7b3032f5af/pkgs/build-support/rust/build-rust-package/default.nix#L104
151 #
152 # See (for context): https://github.com/NixOS/nixpkgs/pull/382550
153 cargoDeps = rustPlatform.fetchCargoVendor {
154 inherit (new) src;
155 hash = new.cargoHash;
156 patches = new.cargoPatches or [ ];
157 name = new.cargoDepsName or new.finalPackage.name;
158 };
159 }
160 ))
161 ];
162
163 buildInputs = [
164 curl
165 fontconfig
166 freetype
167 # TODO: need staticlib of this for linking the musl remote server.
168 # should make it a separate derivation/flake output
169 # see https://crane.dev/examples/cross-musl.html
170 libgit2
171 openssl
172 sqlite
173 zlib
174 zstd
175 ]
176 ++ lib.optionals stdenv'.hostPlatform.isLinux [
177 alsa-lib
178 glib
179 libva
180 libxkbcommon
181 wayland
182 gpu-lib
183 xorg.libX11
184 xorg.libxcb
185 libdrm
186 libgbm
187 libva
188 libxcomposite
189 libxdamage
190 libxext
191 libxfixes
192 libxrandr
193 ]
194 ++ lib.optionals stdenv'.hostPlatform.isDarwin [
195 apple-sdk_15
196 (darwinMinVersionHook "10.15")
197 ];
198
199 cargoExtraArgs = "-p zed -p cli --locked --features=gpui_platform/runtime_shaders";
200
201 stdenv =
202 pkgs:
203 let
204 base = pkgs.llvmPackages.stdenv;
205 addBinTools = old: {
206 cc = old.cc.override {
207 inherit (pkgs.llvmPackages) bintools;
208 };
209 };
210 custom = lib.pipe base [
211 (stdenv: stdenv.override addBinTools)
212 pkgs.stdenvAdapters.useMoldLinker
213 ];
214 in
215 if stdenv'.hostPlatform.isLinux then custom else base;
216
217 env = {
218 ZSTD_SYS_USE_PKG_CONFIG = true;
219 FONTCONFIG_FILE = makeFontsConf {
220 fontDirectories = [
221 ../assets/fonts/lilex
222 ../assets/fonts/ibm-plex-sans
223 ];
224 };
225 ZED_UPDATE_EXPLANATION = "Zed has been installed using Nix. Auto-updates have thus been disabled.";
226 RELEASE_VERSION = version;
227 ZED_COMMIT_SHA = lib.optionalString (commitSha != null) "${commitSha}";
228 LK_CUSTOM_WEBRTC = pkgs.callPackage ./livekit-libwebrtc/package.nix { };
229 PROTOC = "${protobuf}/bin/protoc";
230
231 CARGO_PROFILE = profile;
232 # need to handle some profiles specially https://github.com/rust-lang/cargo/issues/11053
233 TARGET_DIR = "target/" + (if profile == "dev" then "debug" else profile);
234
235 # for some reason these deps being in buildInputs isn't enough, the only thing
236 # about them that's special is that they're manually dlopened at runtime
237 NIX_LDFLAGS = lib.optionalString stdenv'.hostPlatform.isLinux "-rpath ${
238 lib.makeLibraryPath [
239 gpu-lib
240 wayland
241 libva
242 ]
243 }";
244
245 NIX_OUTPATH_USED_AS_RANDOM_SEED = "norebuilds";
246 };
247
248 # prevent nix from removing the "unused" wayland/gpu-lib rpaths
249 dontPatchELF = stdenv'.hostPlatform.isLinux;
250
251 # TODO: try craneLib.cargoNextest separate output
252 # for now we're not worried about running our test suite (or tests for deps) in the nix sandbox
253 doCheck = false;
254
255 cargoVendorDir = craneLib.vendorCargoDeps {
256 inherit src cargoLock;
257 overrideVendorGitCheckout =
258 let
259 hasWebRtcSys = builtins.any (crate: crate.name == "webrtc-sys");
260 # we can't set $RUSTFLAGS because that clobbers the cargo config
261 # see https://github.com/rust-lang/cargo/issues/5376#issuecomment-2163350032
262 glesConfig = builtins.toFile "config.toml" ''
263 [target.'cfg(all())']
264 rustflags = ["--cfg", "gles"]
265 '';
266
267 # `webrtc-sys` expects a staticlib; nixpkgs' `livekit-webrtc` has been patched to
268 # produce a `dylib`... patching `webrtc-sys`'s build script is the easier option
269 # TODO: send livekit sdk a PR to make this configurable
270 postPatch = ''
271 substituteInPlace webrtc-sys/build.rs --replace-fail \
272 "cargo:rustc-link-lib=static=webrtc" "cargo:rustc-link-lib=dylib=webrtc"
273
274 substituteInPlace webrtc-sys/build.rs --replace-fail \
275 'add_gio_headers(&mut builder);' \
276 'for lib_name in ["glib-2.0", "gio-2.0"] {
277 if let Ok(lib) = pkg_config::Config::new().cargo_metadata(false).probe(lib_name) {
278 for path in lib.include_paths {
279 builder.include(&path);
280 }
281 }
282 }'
283 ''
284 + lib.optionalString withGLES ''
285 cat ${glesConfig} >> .cargo/config/config.toml
286 '';
287 in
288 crates: drv:
289 if hasWebRtcSys crates then
290 drv.overrideAttrs (o: {
291 postPatch = (o.postPatch or "") + postPatch;
292 })
293 else
294 drv;
295 };
296 };
297 cargoArtifacts = craneLib.buildDepsOnly commonArgs;
298in
299craneLib.buildPackage (
300 lib.recursiveUpdate commonArgs {
301 inherit cargoArtifacts;
302
303 dontUseCmakeConfigure = true;
304
305 # without the env var generate-licenses fails due to crane's fetchCargoVendor, see:
306 # https://github.com/zed-industries/zed/issues/19971#issuecomment-2688455390
307 # TODO: put this in a separate derivation that depends on src to avoid running it on every build
308 preBuild = ''
309 ALLOW_MISSING_LICENSES=yes bash script/generate-licenses
310 echo nightly > crates/zed/RELEASE_CHANNEL
311 '';
312
313 installPhase =
314 if stdenv.hostPlatform.isDarwin then
315 ''
316 runHook preInstall
317
318 pushd crates/zed
319 sed -i "s/package.metadata.bundle-nightly/package.metadata.bundle/" Cargo.toml
320 export CARGO_BUNDLE_SKIP_BUILD=true
321 app_path="$(cargo bundle --profile $CARGO_PROFILE | xargs)"
322 popd
323
324 mkdir -p $out/Applications $out/bin
325 # Zed expects git next to its own binary
326 ln -s ${git}/bin/git "$app_path/Contents/MacOS/git"
327 mv $TARGET_DIR/cli "$app_path/Contents/MacOS/cli"
328 mv "$app_path" $out/Applications/
329
330 # Physical location of the CLI must be inside the app bundle as this is used
331 # to determine which app to start
332 ln -s "$out/Applications/Zed Nightly.app/Contents/MacOS/cli" $out/bin/zed
333
334 runHook postInstall
335 ''
336 else
337 ''
338 runHook preInstall
339
340 mkdir -p $out/bin $out/libexec
341 cp $TARGET_DIR/zed $out/libexec/zed-editor
342 cp $TARGET_DIR/cli $out/bin/zed
343 ln -s $out/bin/zed $out/bin/zeditor # home-manager expects the CLI binary to be here
344
345
346 install -D "crates/zed/resources/app-icon-nightly@2x.png" \
347 "$out/share/icons/hicolor/1024x1024@2x/apps/zed.png"
348 install -D crates/zed/resources/app-icon-nightly.png \
349 $out/share/icons/hicolor/512x512/apps/zed.png
350
351 # TODO: icons should probably be named "zed-nightly"
352 (
353 export DO_STARTUP_NOTIFY="true"
354 export APP_CLI="zed"
355 export APP_ICON="zed"
356 export APP_NAME="Zed Nightly"
357 export APP_ARGS="%U"
358 mkdir -p "$out/share/applications"
359 ${lib.getExe envsubst} < "crates/zed/resources/zed.desktop.in" > "$out/share/applications/dev.zed.Zed-Nightly.desktop"
360 chmod +x "$out/share/applications/dev.zed.Zed-Nightly.desktop"
361 )
362
363 runHook postInstall
364 '';
365
366 # TODO: why isn't this also done on macOS?
367 postFixup = lib.optionalString stdenv.hostPlatform.isLinux ''
368 wrapProgram $out/libexec/zed-editor --suffix PATH : ${lib.makeBinPath [ nodejs_22 ]}
369 '';
370
371 meta = {
372 description = "High-performance, multiplayer code editor from the creators of Atom and Tree-sitter";
373 homepage = "https://zed.dev";
374 changelog = "https://zed.dev/releases/preview";
375 license = lib.licenses.gpl3Only;
376 mainProgram = "zed";
377 platforms = lib.platforms.linux ++ lib.platforms.darwin;
378 };
379 }
380)