Skip to content

Lightpanda: the headless browser designed for AI and automation

4.1 KB
Raw Blame
{
  src,
  lib,
  pkgs,
  stdenv,
  fetchurl,
  zig,
  craneLib,
  version,
  buildInputs ? [ ],
}:
let
  # Generated by nix/update-v8-deps.sh from the defaults in
  # .github/actions/install/action.yml — rerun that script when those change.
  v8Deps = import ./v8-deps.nix;
  v8Archive = v8Deps.archives.${stdenv.hostPlatform.system};

  v8 = fetchurl {
    url = "https://github.com/lightpanda-io/zig-v8-fork/releases/download/${v8Deps.tag}/libc_v8_${v8Deps.version}_${v8Archive.platform}.a";
    hash = v8Archive.hash;
  };

  rustLib = craneLib.buildPackage {
    pname = "lightpanda-ffi";
    inherit version;
    src = src + "/src/rust";
    cargoExtraArgs = "-p lightpanda-ffi";
    doCheck = false;
  };
  rustFfiLib = "${rustLib}/lib/liblightpanda_ffi.a";

  # Generated by nix/update-zig-deps-hash.sh — rerun it when build.zig.zon changes.
  zigDepsHash = (import ./zig-deps.nix).hash;

  zigDeps =
    pkgs.runCommand "lightpanda-zig-deps"
      {
        nativeBuildInputs = [ zig ];
        outputHash = zigDepsHash;
        outputHashMode = "recursive";
      }
      ''
        cp -r --no-preserve=mode ${src} source
        cd source

        export ZIG_GLOBAL_CACHE_DIR="$PWD/zig-cache"
        export ZIG_LOCAL_CACHE_DIR="$ZIG_GLOBAL_CACHE_DIR"

        TERM=dumb zig build --fetch=all

        mkdir -p "$ZIG_GLOBAL_CACHE_DIR/p"
        cp -r "$ZIG_GLOBAL_CACHE_DIR/p" "$out"
      '';

  # Zig's os tag is "macos", not nixpkgs' "darwin", and Darwin targets carry no abi component.
  zigTargetTriple =
    if stdenv.isDarwin then
      "${stdenv.hostPlatform.parsed.cpu.name}-macos-none"
    else
      "${stdenv.hostPlatform.parsed.cpu.name}-${stdenv.hostPlatform.parsed.kernel.name}-${stdenv.hostPlatform.parsed.abi.name}.${pkgs.glibc.version}";

  mkZigDerivation =
    extraArgs:
    stdenv.mkDerivation (
      {
        inherit src;

        configurePhase = ''
          runHook preConfigure

          export HOME="$TMPDIR/home"
          mkdir -p "$HOME"

          export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-global-cache"
          export ZIG_LOCAL_CACHE_DIR="$ZIG_GLOBAL_CACHE_DIR"
          mkdir -p "$ZIG_GLOBAL_CACHE_DIR"
          cp -r --no-preserve=mode ${zigDeps}/. "$ZIG_GLOBAL_CACHE_DIR/p"

          runHook postConfigure
        '';
      }
      // extraArgs
      // {
        nativeBuildInputs = buildInputs ++ (extraArgs.nativeBuildInputs or [ ]);
      }
    );

  snapshot = mkZigDerivation {
    pname = "lightpanda-snapshot";
    version = "${version}-nix";

    nativeBuildInputs = lib.optionals stdenv.isLinux [ pkgs.patchelf ];

    buildPhase = ''
      runHook preBuild

      TERM=dumb zig build extras \
        -Doptimize=ReleaseFast \
        -Dtarget=${zigTargetTriple} \
        -Dprebuilt_v8_path=${v8} \
        -Dprebuilt_rust_path=${rustFfiLib}

      ${lib.optionalString stdenv.isLinux ''
        # Zig's ELF binaries don't carry nix's glibc interpreter path by default
        patchelf --set-interpreter "$(cat "$NIX_CC/nix-support/dynamic-linker")" zig-out/bin/lightpanda-snapshot-creator
      ''}
      ./zig-out/bin/lightpanda-snapshot-creator snapshot.bin

      runHook postBuild
    '';

    installPhase = ''
      runHook preInstall

      mkdir -p $out
      cp snapshot.bin $out/snapshot.bin

      runHook postInstall
    '';
  };
in
mkZigDerivation {
  pname = "lightpanda";
  version = "${version}-nix";

  nativeBuildInputs = lib.optionals stdenv.isLinux [ pkgs.autoPatchelfHook ];

  buildPhase = ''
    runHook preBuild

    cp ${snapshot}/snapshot.bin src/snapshot.bin

    TERM=dumb zig build \
      -Doptimize=ReleaseFast \
      -Dtarget=${zigTargetTriple} \
      -Dprebuilt_v8_path=${v8} \
      -Dprebuilt_rust_path=${rustFfiLib} \
      -Dsnapshot_path=../../snapshot.bin \
      -Dversion=${version}

    runHook postBuild
  '';

  installPhase = ''
    runHook preInstall

    mkdir -p $out/bin
    cp zig-out/bin/lightpanda $out/bin/lightpanda

    runHook postInstall
  '';

  meta = {
    description = "Headless browser designed for AI and automation";
    homepage = "https://lightpanda.io";
    license = lib.licenses.agpl3Only;
    mainProgram = "lightpanda";
    platforms = [
      "x86_64-linux"
      "aarch64-linux"
    ];
  };
}