Commit 70519782 authored by ZHAO Jin-Xiang's avatar ZHAO Jin-Xiang
Browse files

affine: cleaning

parent a6015afd
Loading
Loading
Loading
Loading
+201 −203
Original line number Diff line number Diff line
{
  cacert,
  cargo,
  copyDesktopItems,
  fetchFromGitHub,
  fetchurl,
  findutils,
  jq,
  lib,
  makeDesktopItem,
  makeWrapper,
  rsync,
  rustPlatform,
  rustc,
  stdenv,
  stdenvNoCC,
  yarn-berry,
  zip,
  fetchFromGitHub,
  rustPlatform,
  electron_33,
  nodejs_20,
  yarn-berry,
  cacert,
  writableTmpDirAsHomeHook,
  cargo,
  rustc,
  findutils,
  zip,
  rsync,
  jq,
  copyDesktopItems,
  makeWrapper,
  makeDesktopItem,
  nix-update-script,
  buildType ? "stable",
  commandLineArgs ? "",
}:
@@ -34,88 +35,64 @@ let
  electron = electron_33;
  nodejs = nodejs_20;
  yarn = yarn-berry.override { inherit nodejs; };
  productName = if buildType != "stable" then "AFFiNE-${buildType}" else "AFFiNE";
  binName = lib.toLower productName;
in
stdenv.mkDerivation (
  finalAttrs:
  (
    {
      productName = if buildType == "stable" then "AFFiNE" else "AFFiNE-" + buildType;
      binName = lib.toLower finalAttrs.productName;
      pname = finalAttrs.binName;
stdenv.mkDerivation (finalAttrs: {
  pname = binName;

      # https://github.com/toeverything/AFFiNE/releases/tag/v0.18.1
  version = "0.18.1";
      GITHUB_SHA = "8b066a4b398aace25a20508a8e3c1a381721971f";
  src = fetchFromGitHub {
    owner = "toeverything";
    repo = "AFFiNE";
        rev = finalAttrs.GITHUB_SHA;
    tag = "v${finalAttrs.version}";
    hash = "sha256-TWwojG3lqQlQFX3BKoFjJ27a3T/SawXgNDO6fP6gW4k=";
  };

      meta =
        {
          description = "Workspace with fully merged docs, whiteboards and databases";
          longDescription = ''
            AFFiNE is an open-source, all-in-one workspace and an operating
            system for all the building blocks that assemble your knowledge
            base and much more -- wiki, knowledge management, presentation
            and digital assets
          '';
          homepage = "https://affine.pro/";
          license = lib.licenses.mit;
          maintainers = with lib.maintainers; [ xiaoxiangmoe ];
          platforms = [
            "aarch64-darwin"
            "aarch64-linux"
            "x86_64-darwin"
            "x86_64-linux"
          ];
          sourceProvenance = [ lib.sourceTypes.fromSource ];
        }
        // lib.optionalAttrs hostPlatform.isLinux {
          mainProgram = finalAttrs.binName;
        };

      env = {
        BUILD_TYPE = buildType;
      };
  cargoDeps = rustPlatform.fetchCargoVendor {
        src = finalAttrs.src;
    inherit (finalAttrs) pname version src;
    hash = "sha256-5s/X9CD/H9rSn7SqMHioLg1KRP7y9fsozdFRY3hNiP8=";
  };
  yarnOfflineCache = stdenvNoCC.mkDerivation {
    name = "yarn-offline-cache";
        src = finalAttrs.src;
    inherit (finalAttrs) src;
    nativeBuildInputs = [
      yarn
      cacert
      writableTmpDirAsHomeHook
    ];
    # force yarn install run in CI mode
    env.CI = "1";
    buildPhase =
      let
        supportedArchitectures = builtins.toJSON {
          os = [
            "darwin"
            "linux"
          ];
          cpu = [
            "arm64"
            "x64"
            "ia32"
            "arm64"
          ];
          libc = [
            "glibc"
            "musl"
          ];
        };
        buildPhase = ''
          export HOME="$NIX_BUILD_TOP"
          export CI=1
      in
      ''
        runHook preBuild

        mkdir -p $out
        yarn config set enableTelemetry false
        yarn config set cacheFolder $out
        yarn config set enableGlobalCache false
          yarn config set supportedArchitectures --json "$supportedArchitectures"
        yarn config set supportedArchitectures --json '${supportedArchitectures}'

        yarn install --immutable --mode=skip-build

        runHook postBuild
      '';
    dontInstall = true;
    outputHashMode = "recursive";
@@ -131,12 +108,23 @@ stdenv.mkDerivation (
      zip
      jq
      rsync
      writableTmpDirAsHomeHook
    ]
    ++ lib.optionals hostPlatform.isLinux [
      copyDesktopItems
      makeWrapper
    ];

  # force yarn install run in CI mode
  env.CI = "1";

  # Remove code under The AFFiNE Enterprise Edition (EE) license.
  # Keep file package.json for `yarn install --immutable` lockfile check.
  postPatch = ''
    BACKEND_SERVER_PACKAGE_JSON="$(jq 'del(.scripts.postinstall)' packages/backend/server/package.json)"
    rm -rf packages/backend/server/{.*,*}
    echo "$BACKEND_SERVER_PACKAGE_JSON" > packages/backend/server/package.json
  '';
  patchPhase = ''
    runHook prePatchPhase

@@ -145,17 +133,9 @@ stdenv.mkDerivation (
    runHook postPatchPhase
  '';

      configurePhase =
        let
          electronContentPath =
            electron + (if hostPlatform.isLinux then "/libexec/electron/" else "/Applications/");
        in
        ''
  configurePhase = ''
    runHook preConfigurePhase

          export HOME="$NIX_BUILD_TOP"
          export CI=1

    # cargo config
    mkdir -p .cargo
    cat $cargoDeps/.cargo/config.toml >> .cargo/config.toml
@@ -168,7 +148,7 @@ stdenv.mkDerivation (

    # electron config
    ELECTRON_VERSION_IN_LOCKFILE=$(yarn why electron --json | tail --lines 1 | jq --raw-output '.children | to_entries | first | .key ' | cut -d : -f 2)
          rsync --archive --chmod=u+w ${electronContentPath} $HOME/.electron-prebuilt-zip-tmp
    rsync --archive --chmod=u+w "${electron.dist}/" $HOME/.electron-prebuilt-zip-tmp
    export ELECTRON_FORGE_ELECTRON_ZIP_DIR=$PWD/.electron_zip_dir
    mkdir -p $ELECTRON_FORGE_ELECTRON_ZIP_DIR
    (cd $HOME/.electron-prebuilt-zip-tmp && zip --recurse-paths - .) > $ELECTRON_FORGE_ELECTRON_ZIP_DIR/electron-v$ELECTRON_VERSION_IN_LOCKFILE-${nodePlatform}-${nodeArch}.zip
@@ -176,13 +156,14 @@ stdenv.mkDerivation (

    runHook postConfigurePhase
  '';

  buildPhase = ''
    runHook preBuild

    # first build
    yarn workspaces focus @affine/electron @affine/monorepo
    CARGO_NET_OFFLINE=true yarn workspace @affine/native build
        BUILD_TYPE=${buildType} SKIP_NX_CACHE=1 yarn workspace @affine/electron generate-assets
    GITHUB_SHA=ffffffffffffffffffffffffffffffffffffffff BUILD_TYPE=${buildType} SKIP_NX_CACHE=1 yarn workspace @affine/electron generate-assets

    # second build
    yarn config set nmMode classic
@@ -193,10 +174,8 @@ stdenv.mkDerivation (

    runHook postBuild
  '';

  installPhase =
        let
          inherit (finalAttrs) binName productName;
        in
    if hostPlatform.isDarwin then
      ''
        runHook preInstall
@@ -214,7 +193,7 @@ stdenv.mkDerivation (
        mv packages/frontend/apps/electron/out/${buildType}/${productName}-linux-${nodeArch}/{resources,LICENSE*} $out/lib/${binName}/
        install -Dm644 packages/frontend/apps/electron/resources/icons/icon_${buildType}_64x64.png $out/share/icons/hicolor/64x64/apps/${binName}.png

            makeWrapper "${electron}/bin/electron" $out/bin/${binName} \
        makeWrapper "${lib.getExe electron}" $out/bin/${binName} \
          --inherit-argv0 \
          --add-flags $out/lib/${binName}/resources/app.asar \
          --add-flags "\''${NIXOS_OZONE_WL:+\''${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime=true}}" \
@@ -222,13 +201,8 @@ stdenv.mkDerivation (

        runHook postInstall
      '';
    }
    // (lib.optionalAttrs hostPlatform.isLinux {
      desktopItems =
        let
          inherit (finalAttrs) binName productName;
        in
        [

  desktopItems = [
    (makeDesktopItem {
      name = binName;
      desktopName = productName;
@@ -241,6 +215,30 @@ stdenv.mkDerivation (
      mimeTypes = [ "x-scheme-handler/${binName}" ];
    })
  ];

  passthru.updateScript = nix-update-script {
    extraArgs = [
      "--version-regex=^v(\\d+\\.\\d+\\.\\d+)$"
    ];
  };

  meta = {
    description = "Workspace with fully merged docs, whiteboards and databases";
    longDescription = ''
      AFFiNE is an open-source, all-in-one workspace and an operating
      system for all the building blocks that assemble your knowledge
      base and much more -- wiki, knowledge management, presentation
      and digital assets
    '';
    homepage = "https://affine.pro/";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [ xiaoxiangmoe ];
    platforms = [
      "aarch64-darwin"
      "aarch64-linux"
      "x86_64-darwin"
      "x86_64-linux"
    ];
    sourceProvenance = [ lib.sourceTypes.fromSource ];
  };
})
  )
)