Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.7 KB
Raw Blame
{
  lib,
  stdenvNoCC,
  bun,
  coreutils,
  findutils,
  git,
  src ? ../.,
  hash ? (
    (builtins.fromJSON (builtins.readFile ./node_modules-hashes.json))
    .nodeModules.${stdenvNoCC.hostPlatform.system} or lib.fakeHash
  ),
}:
let
  packageJson = builtins.fromJSON (builtins.readFile ../apps/web/package.json);
  platform = stdenvNoCC.hostPlatform;
  bunCpu =
    if platform.isAarch64 then
      "arm64"
    else if platform.isx86_64 then
      "x64"
    else
      throw "unsupported Bun CPU for ${platform.system}";
  bunOs =
    if platform.isLinux then
      "linux"
    else if platform.isDarwin then
      "darwin"
    else
      throw "unsupported Bun OS for ${platform.system}";

  nodeModulesSrc = lib.cleanSourceWith {
    inherit src;
    filter =
      path: type:
      let
        rel = lib.removePrefix ((toString src) + "/") (toString path);
      in
      if type == "directory" then
        !(lib.hasInfix "/node_modules" rel)
        && !(lib.hasInfix "/dist" rel)
        && !(lib.hasInfix "/target" rel)
        && !(lib.hasInfix "/.direnv" rel)
        && rel != "node_modules"
        && rel != "dist"
        && rel != "target"
        && rel != ".direnv"
      else
        rel == "bun.lock"
        || rel == "packages/sdk/bun.lock"
        || rel == "package.json"
        || lib.hasSuffix "/package.json" rel
        || lib.hasPrefix "apps/web/patches/" rel;
  };
in
stdenvNoCC.mkDerivation {
  pname = "macro-js-node-modules";
  version = packageJson.version;

  src = nodeModulesSrc;

  impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
    "GIT_PROXY_COMMAND"
    "SOCKS_SERVER"
  ];

  nativeBuildInputs = [
    bun
    coreutils
    findutils
    git
  ];

  dontConfigure = true;

  buildPhase = ''
    runHook preBuild

    export HOME="$TMPDIR/home"
    export BUN_INSTALL_CACHE_DIR="$TMPDIR/bun-cache"
    mkdir -p "$HOME" "$BUN_INSTALL_CACHE_DIR"

    # packages/sdk is a standalone package with its own lockfile; the
    # code-mode SDK bundle resolves its dependencies from there.
    for project in . packages/sdk; do
      (
        cd "$project"
        bun install \
          --cpu="${bunCpu}" \
          --os="${bunOs}" \
          --linker=hoisted \
          --frozen-lockfile \
          --ignore-scripts \
          --no-progress
      )
    done

    runHook postBuild
  '';

  installPhase = ''
    runHook preInstall
    mkdir -p "$out"
    find . -type d -name node_modules -exec cp -R --parents {} "$out" \;
    runHook postInstall
  '';

  dontFixup = true;

  outputHashAlgo = "sha256";
  outputHashMode = "recursive";
  outputHash = hash;

  meta.platforms = builtins.attrNames (
    (builtins.fromJSON (builtins.readFile ./node_modules-hashes.json)).nodeModules
  );
}