Skip to content

Commit

Permalink
python3Packages.playwright: Add PLAYWRIGHT_BROWSERS_PATH to build env…
Browse files Browse the repository at this point in the history
…ironment

This allows writing self-contained Python scripts (using a nix-shell
shebang), without requiring a separate shell.nix which sets
PLAYWRIGHT_BROWSERS_PATH to pkgs.playwright-driver.browsers.
  • Loading branch information
the-sun-will-rise-tomorrow committed Dec 27, 2024
1 parent 682e3b7 commit a1952a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkgs/development/python-modules/playwright/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
setuptools-scm,
playwright-driver,
nixosTests,
writeText,
runCommand,
pythonPackages,
nodejs,
}:

Expand Down Expand Up @@ -86,6 +89,16 @@ buildPythonPackage rec {
pyee
];

setupHook = writeText "setupHook.sh" ''
addBrowsersPath () {
if [[ ! -v PLAYWRIGHT_BROWSERS_PATH ]] ; then
export PLAYWRIGHT_BROWSERS_PATH="${playwright-driver.browsers}"
fi
}
addEnvHooks "$targetOffset" addBrowsersPath
'';

postInstall = ''
ln -s ${driver} $out/${python.sitePackages}/playwright/driver
'';
Expand All @@ -101,6 +114,9 @@ buildPythonPackage rec {
{
driver = playwright-driver;
browsers = playwright-driver.browsers;
env = runCommand "playwright-env-test" {
buildInputs = [ pythonPackages.playwright ];
} "python ${./test.py}";
}
// lib.optionalAttrs stdenv.hostPlatform.isLinux {
inherit (nixosTests) playwright-python;
Expand Down
10 changes: 10 additions & 0 deletions pkgs/development/python-modules/playwright/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import sys

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
browser = p.chromium.launch()
context = browser.new_context()
with open(os.environ["out"], "w") as f:
f.write("OK")

0 comments on commit a1952a6

Please sign in to comment.