build.nix

  1{
  2  lib,
  3  craneLib,
  4  clang,
  5  llvmPackages_18,
  6  mold-wrapped,
  7  copyDesktopItems,
  8  curl,
  9  perl,
 10  pkg-config,
 11  protobuf,
 12  fontconfig,
 13  freetype,
 14  libgit2,
 15  openssl,
 16  sqlite,
 17  zlib,
 18  zstd,
 19  alsa-lib,
 20  libxkbcommon,
 21  wayland,
 22  libglvnd,
 23  xorg,
 24  makeFontsConf,
 25  vulkan-loader,
 26  envsubst,
 27  stdenvAdapters,
 28  nix-gitignore,
 29  withGLES ? false,
 30}: let
 31  includeFilter = path: type: let
 32    baseName = baseNameOf (toString path);
 33    parentDir = dirOf path;
 34    inRootDir = type == "directory" && parentDir == ../.;
 35  in
 36    !(inRootDir && (baseName == "docs" || baseName == ".github" || baseName == "script" || baseName == ".git" || baseName == "target"));
 37
 38  src = lib.cleanSourceWith {
 39    src = nix-gitignore.gitignoreSource [] ../.;
 40    filter = includeFilter;
 41    name = "source";
 42  };
 43
 44  stdenv = stdenvAdapters.useMoldLinker llvmPackages_18.stdenv;
 45
 46  commonArgs =
 47    craneLib.crateNameFromCargoToml {cargoToml = ../crates/zed/Cargo.toml;}
 48    // {
 49      inherit src stdenv;
 50
 51      buildInputs = [
 52        curl
 53        fontconfig
 54        freetype
 55        libgit2
 56        openssl
 57        sqlite
 58        zlib
 59        zstd
 60        alsa-lib
 61        libxkbcommon
 62        wayland
 63        xorg.libxcb
 64      ];
 65
 66      nativeBuildInputs = [
 67        clang
 68        copyDesktopItems
 69        curl
 70        mold-wrapped
 71        perl
 72        pkg-config
 73        protobuf
 74      ];
 75
 76      ZSTD_SYS_USE_PKG_CONFIG = true;
 77      FONTCONFIG_FILE = makeFontsConf {
 78        fontDirectories = [
 79          "../assets/fonts/zed-mono"
 80          "../assets/fonts/zed-sans"
 81        ];
 82      };
 83      ZED_UPDATE_EXPLANATION = "zed has been installed using nix. Auto-updates have thus been disabled.";
 84    };
 85
 86  cargoArtifacts = craneLib.buildDepsOnly commonArgs;
 87
 88  gpu-lib =
 89    if withGLES
 90    then libglvnd
 91    else vulkan-loader;
 92
 93  zed = craneLib.buildPackage (commonArgs
 94    // {
 95      inherit cargoArtifacts;
 96      cargoExtraArgs = "--package=zed --package=cli";
 97      buildFeatures = ["gpui/runtime_shaders"];
 98      doCheck = false;
 99
100      RUSTFLAGS =
101        if withGLES
102        then "--cfg gles"
103        else "";
104
105      postFixup = ''
106        patchelf --add-rpath ${gpu-lib}/lib $out/bin/*
107        patchelf --add-rpath ${wayland}/lib $out/bin/*
108      '';
109
110      postInstall = ''
111        mkdir -p $out/bin $out/libexec
112        mv $out/bin/zed $out/libexec/zed-editor
113        mv $out/bin/cli $out/bin/zed
114
115        install -D crates/zed/resources/app-icon@2x.png $out/share/icons/hicolor/1024x1024@2x/apps/zed.png
116        install -D crates/zed/resources/app-icon.png $out/share/icons/hicolor/512x512/apps/zed.png
117
118        export DO_STARTUP_NOTIFY="true"
119        export APP_CLI="zed"
120        export APP_ICON="zed"
121        export APP_NAME="Zed"
122        export APP_ARGS="%U"
123        mkdir -p "$out/share/applications"
124        ${lib.getExe envsubst} < "crates/zed/resources/zed.desktop.in" > "$out/share/applications/dev.zed.Zed.desktop"
125      '';
126    });
127in
128  zed
129  // {
130    meta = with lib; {
131      description = "High-performance, multiplayer code editor from the creators of Atom and Tree-sitter";
132      homepage = "https://zed.dev";
133      changelog = "https://zed.dev/releases/preview";
134      license = licenses.gpl3Only;
135      mainProgram = "zed";
136      platforms = platforms.linux;
137    };
138  }