Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nixos/tests/all-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ in {
plasma6 = handleTest ./plasma6.nix {};
plasma5-systemd-start = handleTest ./plasma5-systemd-start.nix {};
plausible = handleTest ./plausible.nix {};
playwright-python = handleTest ./playwright-python.nix {};
please = handleTest ./please.nix {};
pleroma = handleTestOn [ "x86_64-linux" "aarch64-linux" ] ./pleroma.nix {};
plikd = handleTest ./plikd.nix {};
Expand Down
58 changes: 58 additions & 0 deletions nixos/tests/playwright-python.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import ./make-test-python.nix (
{ pkgs, ... }:
{
name = "playwright-python";

meta = with pkgs.lib.maintainers; {
maintainers = [ phaer ];
};

nodes.machine =
{ pkgs, ... }:
{
environment.variables = {
NIX_MANUAL_DOCROOT = "file://${pkgs.nix.doc}/share/doc/nix/manual/index.html";
PLAYWRIGHT_BROWSERS_PATH = pkgs.playwright-driver.browsers;
};
environment.systemPackages = [
(pkgs.writers.writePython3Bin "test_playwright"
{
libraries = [ pkgs.python3Packages.playwright ];
}
''
import sys
from playwright.sync_api import sync_playwright
from playwright.sync_api import expect

browsers = {
"chromium": ["--headless", "--disable-gpu"],
"firefox": [],
"webkit": []
}
if len(sys.argv) != 3 or sys.argv[1] not in browsers.keys():
print(f"usage: {sys.argv[0]} [{'|'.join(browsers.keys())}] <url>")
sys.exit(1)
browser_name = sys.argv[1]
url = sys.argv[2]
browser_args = browsers.get(browser_name)
print(f"Running test on {browser_name} {' '.join(browser_args)}")
with sync_playwright() as p:
browser = getattr(p, browser_name).launch(args=browser_args)
context = browser.new_context()
page = context.new_page()
page.goto(url)
expect(page.get_by_text("Nix Reference Manual")).to_be_visible()
''
)
];
};

testScript = ''
# FIXME: Webkit segfaults
for browser in ["firefox", "chromium"]:
with subtest(f"Render Nix Manual in {browser}"):
machine.succeed(f"test_playwright {browser} $NIX_MANUAL_DOCROOT")
'';

}
)
13 changes: 9 additions & 4 deletions pkgs/development/python-modules/playwright/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
setuptools,
setuptools-scm,
playwright-driver,
nixosTests,
nodejs,
}:

Expand Down Expand Up @@ -93,10 +94,14 @@ buildPythonPackage rec {

passthru = {
inherit driver;
tests = {
driver = playwright-driver;
browsers = playwright-driver.browsers;
};
tests =
{
driver = playwright-driver;
browsers = playwright-driver.browsers;
}
// lib.optionalAttrs stdenv.isLinux {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make this unconditional but whatever. Fine by me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it won't hurt to be explicit about supported platforms. :)

inherit (nixosTests) playwright-python;
};
updateScript = ./update.sh;
};

Expand Down
54 changes: 47 additions & 7 deletions pkgs/development/python-modules/playwright/update.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl gnused common-updater-scripts jq prefetch-npm-deps
#!nix-shell -i bash -p curl gnused common-updater-scripts jq prefetch-npm-deps unzip
set -euo pipefail

root="$(dirname "$(readlink -f "$0")")"
Expand All @@ -11,26 +11,67 @@ version=$(curl ${GITHUB_TOKEN:+" -u \":$GITHUB_TOKEN\""} -s https://api.github.c
setup_py_url="https://github.com/microsoft/playwright-python/raw/v${version}/setup.py"
driver_version=$(curl -Ls "$setup_py_url" | grep '^driver_version =' | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')

# TODO: skip if update-source-version reported the same version
update-source-version playwright-driver "$driver_version"
update-source-version python3Packages.playwright "$version"

# Update package-lock.json files for all npm deps that are built in playwright
# TODO: skip if update-source-version reported the same version
driver_file="$root/../../web/playwright/driver.nix"
playwright_dir="$root/../../web/playwright"
driver_file="$playwright_dir/driver.nix"
repo_url_prefix="https://github.com/microsoft/playwright/raw"
temp_dir=$(mktemp -d)

temp_dir=$(mktemp -d)
trap 'rm -rf "$temp_dir"' EXIT



# update binaries of browsers, used by playwright.
replace_sha() {
sed -i "s|$2 = \".\{44,52\}\"|$2 = \"$3\"|" "$1"
}

prefetch_browser() {
nix store prefetch-file --json --hash-type sha256 --unpack "$1" | jq -r .hash
}

update_browser() {
name="$1"
suffix="$2"
arm64_suffix="${3:-$2-arm64}"
revision="$(jq -r ".browsers.$name.revision" "$playwright_dir/browsers.json")"
replace_sha "$playwright_dir/$name.nix" "x86_64-linux" \
"$(prefetch_browser "https://playwright.azureedge.net/builds/$name/$revision/$name-$suffix.zip")"
replace_sha "$playwright_dir/$name.nix" "aarch64-linux" \
"$(prefetch_browser "https://playwright.azureedge.net/builds/$name/$revision/$name-$arm64_suffix.zip")"
}

curl -fsSl \
"https://raw.githubusercontent.com/microsoft/playwright/v${driver_version}/packages/playwright-core/browsers.json" \
| jq '
.comment = "This file is kept up to date via update.sh"
| .browsers |= (
[.[]
| select(.installByDefault) | del(.installByDefault)]
| map({(.name): . | del(.name)})
| add
)
' > "$playwright_dir/browsers.json"

# We currently use Chromium from nixpkgs, so we don't need to download it here
# Likewise, darwin can be ignored here atm as we are using an impure install anyway.
update_browser "firefox" "ubuntu-22.04"
update_browser "webkit" "ubuntu-22.04"
update_browser "ffmpeg" "linux"


# Update package-lock.json files for all npm deps that are built in playwright

# Function to download `package-lock.json` for a given source path and update hash
update_hash() {
local source_root_path="$1"
local existing_hash="$2"

# Formulate download URL
local download_url="${repo_url_prefix}/v${driver_version}${source_root_path}/package-lock.json"

# Download package-lock.json to temporary directory
curl -fsSL -o "${temp_dir}/package-lock.json" "$download_url"

Expand All @@ -45,7 +86,6 @@ update_hash() {
while IFS= read -r source_root_line; do
[[ "$source_root_line" =~ sourceRoot ]] || continue
source_root_path=$(echo "$source_root_line" | sed -e 's/^.*"${src.name}\(.*\)";.*$/\1/')

# Extract the current npmDepsHash for this sourceRoot
existing_hash=$(grep -A1 "$source_root_line" "$driver_file" | grep 'npmDepsHash' | sed -e 's/^.*npmDepsHash = "\(.*\)";$/\1/')

Expand Down
28 changes: 28 additions & 0 deletions pkgs/development/web/playwright/browsers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"comment": "This file is kept up to date via update.sh",
"browsers": {
"chromium": {
"revision": "1134",
"browserVersion": "129.0.6668.29"
},
"firefox": {
"revision": "1463",
"browserVersion": "130.0"
},
"webkit": {
"revision": "2070",
"revisionOverrides": {
"mac10.14": "1446",
"mac10.15": "1616",
"mac11": "1816",
"mac11-arm64": "1816",
"mac12": "2009",
"mac12-arm64": "2009"
},
"browserVersion": "18.0"
},
"ffmpeg": {
"revision": "1010"
}
}
}
29 changes: 29 additions & 0 deletions pkgs/development/web/playwright/chromium.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
runCommand,
makeWrapper,
makeFontsConf,
chromium,
...
}:
let
fontconfig = makeFontsConf {
fontDirectories = [ ];
};
in
runCommand "playwright-chromium"
{
nativeBuildInputs = [
makeWrapper
];
}
''
mkdir -p $out/chrome-linux

# See here for the Chrome options:
# https://github.com/NixOS/nixpkgs/issues/136207#issuecomment-908637738
# We add --disable-gpu to be able to run in gpu-less environments such
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is outdated but let's remove it in future PRs.

# as headless nixos test vms.
makeWrapper ${chromium}/bin/chromium $out/chrome-linux/chrome \
--set-default SSL_CERT_FILE /etc/ssl/certs/ca-bundle.crt \
--set-default FONTCONFIG_FILE ${fontconfig}
''
66 changes: 38 additions & 28 deletions pkgs/development/web/playwright/driver.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
jq,
nodejs,
fetchFromGitHub,
linkFarm,
callPackage,
makeFontsConf,
makeWrapper,
runCommand,
Expand All @@ -16,6 +18,14 @@ let
inherit (stdenv.hostPlatform) system;

throwSystem = throw "Unsupported system: ${system}";
suffix =
{
x86_64-linux = "linux";
aarch64-linux = "linux-arm64";
x86_64-darwin = "mac";
aarch64-darwin = "mac-arm64";
}
.${system} or throwSystem;

version = "1.47.0";

Expand Down Expand Up @@ -148,6 +158,7 @@ let
'';

passthru = {
browsersJSON = (lib.importJSON ./browsers.json).browsers;
browsers =
{
x86_64-linux = browsers-linux { };
Expand Down Expand Up @@ -209,37 +220,36 @@ let
browsers-linux =
{
withChromium ? true,
withFirefox ? true,
withWebkit ? true,
withFfmpeg ? true,
}:
let
fontconfig = makeFontsConf { fontDirectories = [ ]; };
browsers =
lib.optionals withChromium [ "chromium" ]
++ lib.optionals withFirefox [ "firefox" ]
++ lib.optionals withWebkit [ "webkit" ]
++ lib.optionals withFfmpeg [ "ffmpeg" ];
in
runCommand ("playwright-browsers" + lib.optionalString withChromium "-chromium")
{
nativeBuildInputs = [
makeWrapper
jq
];
}
(
''
BROWSERS_JSON=${playwright-core}/browsers.json
''
+ lib.optionalString withChromium ''
CHROMIUM_REVISION=$(jq -r '.browsers[] | select(.name == "chromium").revision' $BROWSERS_JSON)
mkdir -p $out/chromium-$CHROMIUM_REVISION/chrome-linux

# See here for the Chrome options:
# https://github.com/NixOS/nixpkgs/issues/136207#issuecomment-908637738
makeWrapper ${chromium}/bin/chromium $out/chromium-$CHROMIUM_REVISION/chrome-linux/chrome \
--set SSL_CERT_FILE /etc/ssl/certs/ca-bundle.crt \
--set FONTCONFIG_FILE ${fontconfig}
''
+ ''
FFMPEG_REVISION=$(jq -r '.browsers[] | select(.name == "ffmpeg").revision' $BROWSERS_JSON)
mkdir -p $out/ffmpeg-$FFMPEG_REVISION
ln -s ${ffmpeg}/bin/ffmpeg $out/ffmpeg-$FFMPEG_REVISION/ffmpeg-linux
''
);
linkFarm "playwright-browsers" (
lib.listToAttrs (
map (
name:
let
value = playwright-core.passthru.browsersJSON.${name};
in
lib.nameValuePair
# TODO check platform for revisionOverrides
"${name}-${value.revision}"
(
callPackage ./${name}.nix {
inherit suffix system throwSystem;
inherit (value) revision;
}
)
) browsers
)
);
in
{
playwright-core = playwright-core;
Expand Down
17 changes: 17 additions & 0 deletions pkgs/development/web/playwright/ffmpeg.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
fetchzip,
suffix,
revision,
system,
throwSystem,
}:
fetchzip {
url = "https://playwright.azureedge.net/builds/ffmpeg/${revision}/ffmpeg-${suffix}.zip";
stripRoot = false;
hash =
{
x86_64-linux = "sha256-FEm62UvMv0h6Sav93WmbPLw3CW1L1xg4nD26ca5ol38=";
aarch64-linux = "sha256-jtQ+NS++VHRiKoIV++PIxEnyVnYtVwUyNlSILKSH4A4=";
}
.${system} or throwSystem;
}
39 changes: 39 additions & 0 deletions pkgs/development/web/playwright/firefox.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
lib,
stdenv,
fetchzip,
firefox-bin,
suffix,
revision,
system,
throwSystem,
}:
let
suffix' =
if lib.hasPrefix "linux" suffix then "ubuntu-22.04" + (lib.removePrefix "linux" suffix) else suffix;
in
stdenv.mkDerivation {
name = "playwright-firefox";
src = fetchzip {
url = "https://playwright.azureedge.net/builds/firefox/${revision}/firefox-${suffix'}.zip";
hash =
{
x86_64-linux = "sha256-Hd9LlSRLW51gDoFyszqvg46Q/sMizLRsVKAN9atbwsw=";
aarch64-linux = "sha256-SEXH3gLOfNjOcnNWQjQ5gaaow47veVs0BoTYSgXw+24=";
}
.${system} or throwSystem;
};

inherit (firefox-bin.unwrapped)
nativeBuildInputs
buildInputs
runtimeDependencies
appendRunpaths
patchelfFlags
;

buildPhase = ''
mkdir -p $out/firefox
cp -R . $out/firefox
'';
}
Loading