build.nix

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