build.nix

  1{
  2  lib,
  3  crane,
  4  rustToolchain,
  5  fetchpatch,
  6  clang,
  7  cmake,
  8  copyDesktopItems,
  9  curl,
 10  perl,
 11  pkg-config,
 12  protobuf,
 13  fontconfig,
 14  freetype,
 15  libgit2,
 16  openssl,
 17  sqlite,
 18  zlib,
 19  zstd,
 20  alsa-lib,
 21  libxkbcommon,
 22  wayland,
 23  libglvnd,
 24  xorg,
 25  stdenv,
 26  makeFontsConf,
 27  vulkan-loader,
 28  envsubst,
 29  cargo-about,
 30  cargo-bundle,
 31  git,
 32  apple-sdk_15,
 33  darwinMinVersionHook,
 34  makeWrapper,
 35  nodejs_22,
 36  nix-gitignore,
 37
 38  withGLES ? false,
 39}:
 40
 41assert withGLES -> stdenv.hostPlatform.isLinux;
 42
 43let
 44  includeFilter =
 45    path: type:
 46    let
 47      baseName = baseNameOf (toString path);
 48      parentDir = dirOf path;
 49      inRootDir = type == "directory" && parentDir == ../.;
 50    in
 51    !(
 52      inRootDir
 53      && (baseName == "docs" || baseName == ".github" || baseName == ".git" || baseName == "target")
 54    );
 55  craneLib = crane.overrideToolchain rustToolchain;
 56  commonSrc = lib.cleanSourceWith {
 57    src = nix-gitignore.gitignoreSource [ ] ../.;
 58    filter = includeFilter;
 59    name = "source";
 60  };
 61  commonArgs = rec {
 62    pname = "zed-editor";
 63    version = "nightly";
 64
 65    src = commonSrc;
 66
 67    nativeBuildInputs =
 68      [
 69        clang
 70        cmake
 71        copyDesktopItems
 72        curl
 73        perl
 74        pkg-config
 75        protobuf
 76        cargo-about
 77      ]
 78      ++ lib.optionals stdenv.hostPlatform.isLinux [ makeWrapper ]
 79      ++ lib.optionals stdenv.hostPlatform.isDarwin [ cargo-bundle ];
 80
 81    buildInputs =
 82      [
 83        curl
 84        fontconfig
 85        freetype
 86        libgit2
 87        openssl
 88        sqlite
 89        zlib
 90        zstd
 91      ]
 92      ++ lib.optionals stdenv.hostPlatform.isLinux [
 93        alsa-lib
 94        libxkbcommon
 95        wayland
 96        xorg.libxcb
 97      ]
 98      ++ lib.optionals stdenv.hostPlatform.isDarwin [
 99        apple-sdk_15
100        (darwinMinVersionHook "10.15")
101      ];
102
103    env = {
104      ZSTD_SYS_USE_PKG_CONFIG = true;
105      FONTCONFIG_FILE = makeFontsConf {
106        fontDirectories = [
107          "${src}/assets/fonts/plex-mono"
108          "${src}/assets/fonts/plex-sans"
109        ];
110      };
111      ZED_UPDATE_EXPLANATION = "Zed has been installed using Nix. Auto-updates have thus been disabled.";
112      RELEASE_VERSION = version;
113    };
114  };
115  cargoArtifacts = craneLib.buildDepsOnly commonArgs;
116in
117craneLib.buildPackage (
118  commonArgs
119  // rec {
120    inherit cargoArtifacts;
121
122    patches =
123      [
124        # Zed uses cargo-install to install cargo-about during the script execution.
125        # We provide cargo-about ourselves and can skip this step.
126        # Until https://github.com/zed-industries/zed/issues/19971 is fixed,
127        # we also skip any crate for which the license cannot be determined.
128        (fetchpatch {
129          url = "https://raw.githubusercontent.com/NixOS/nixpkgs/1fd02d90c6c097f91349df35da62d36c19359ba7/pkgs/by-name/ze/zed-editor/0001-generate-licenses.patch";
130          hash = "sha256-cLgqLDXW1JtQ2OQFLd5UolAjfy7bMoTw40lEx2jA2pk=";
131        })
132      ]
133      ++ lib.optionals stdenv.hostPlatform.isDarwin [
134        # Livekit requires Swift 6
135        # We need this until livekit-rust sdk is used
136        (fetchpatch {
137          url = "https://raw.githubusercontent.com/NixOS/nixpkgs/1fd02d90c6c097f91349df35da62d36c19359ba7/pkgs/by-name/ze/zed-editor/0002-disable-livekit-darwin.patch";
138          hash = "sha256-whZ7RaXv8hrVzWAveU3qiBnZSrvGNEHTuyNhxgMIo5w=";
139        })
140      ];
141
142    cargoExtraArgs = "--package=zed --package=cli --features=gpui/runtime_shaders";
143
144    dontUseCmakeConfigure = true;
145    preBuild = ''
146      bash script/generate-licenses
147    '';
148
149    postFixup = lib.optionalString stdenv.hostPlatform.isLinux ''
150      patchelf --add-rpath ${gpu-lib}/lib $out/libexec/*
151      patchelf --add-rpath ${wayland}/lib $out/libexec/*
152      wrapProgram $out/libexec/zed-editor --suffix PATH : ${lib.makeBinPath [ nodejs_22 ]}
153    '';
154
155    RUSTFLAGS = if withGLES then "--cfg gles" else "";
156    gpu-lib = if withGLES then libglvnd else vulkan-loader;
157
158    preCheck = ''
159      export HOME=$(mktemp -d);
160    '';
161
162    cargoTestExtraArgs =
163      "-- "
164      + lib.concatStringsSep " " (
165        [
166          # Flaky: unreliably fails on certain hosts (including Hydra)
167          "--skip=zed::tests::test_window_edit_state_restoring_enabled"
168        ]
169        ++ lib.optionals stdenv.hostPlatform.isLinux [
170          # Fails on certain hosts (including Hydra) for unclear reason
171          "--skip=test_open_paths_action"
172        ]
173      );
174
175    installPhase =
176      if stdenv.hostPlatform.isDarwin then
177        ''
178          runHook preInstall
179
180          # cargo-bundle expects the binary in target/release
181          mv target/release/zed target/release/zed
182
183          pushd crates/zed
184
185          # Note that this is GNU sed, while Zed's bundle-mac uses BSD sed
186          sed -i "s/package.metadata.bundle-stable/package.metadata.bundle/" Cargo.toml
187          export CARGO_BUNDLE_SKIP_BUILD=true
188          app_path=$(cargo bundle --release | xargs)
189
190          # We're not using the fork of cargo-bundle, so we must manually append plist extensions
191          # Remove closing tags from Info.plist (last two lines)
192          head -n -2 $app_path/Contents/Info.plist > Info.plist
193          # Append extensions
194          cat resources/info/*.plist >> Info.plist
195          # Add closing tags
196          printf "</dict>\n</plist>\n" >> Info.plist
197          mv Info.plist $app_path/Contents/Info.plist
198
199          popd
200
201          mkdir -p $out/Applications $out/bin
202          # Zed expects git next to its own binary
203          ln -s ${git}/bin/git $app_path/Contents/MacOS/git
204          mv target/release/cli $app_path/Contents/MacOS/cli
205          mv $app_path $out/Applications/
206
207          # Physical location of the CLI must be inside the app bundle as this is used
208          # to determine which app to start
209          ln -s $out/Applications/Zed.app/Contents/MacOS/cli $out/bin/zed
210
211          runHook postInstall
212        ''
213      else
214        ''
215          runHook preInstall
216
217          mkdir -p $out/bin $out/libexec
218          cp target/release/zed $out/libexec/zed-editor
219          cp target/release/cli $out/bin/zed
220
221          install -D ${commonSrc}/crates/zed/resources/app-icon@2x.png $out/share/icons/hicolor/1024x1024@2x/apps/zed.png
222          install -D ${commonSrc}/crates/zed/resources/app-icon.png $out/share/icons/hicolor/512x512/apps/zed.png
223
224          # extracted from https://github.com/zed-industries/zed/blob/v0.141.2/script/bundle-linux (envsubst)
225          # and https://github.com/zed-industries/zed/blob/v0.141.2/script/install.sh (final desktop file name)
226          (
227            export DO_STARTUP_NOTIFY="true"
228            export APP_CLI="zed"
229            export APP_ICON="zed"
230            export APP_NAME="Zed"
231            export APP_ARGS="%U"
232            mkdir -p "$out/share/applications"
233            ${lib.getExe envsubst} < "crates/zed/resources/zed.desktop.in" > "$out/share/applications/dev.zed.Zed.desktop"
234          )
235
236          runHook postInstall
237        '';
238
239    meta = {
240      description = "High-performance, multiplayer code editor from the creators of Atom and Tree-sitter";
241      homepage = "https://zed.dev";
242      changelog = "https://zed.dev/releases/preview";
243      license = lib.licenses.gpl3Only;
244      mainProgram = "zed";
245      platforms = lib.platforms.linux ++ lib.platforms.darwin;
246    };
247  }
248)