playwright: [BUG] Can't run Playwright in Nix

Context:

  • Playwright Version: 1.8.0
  • Operating System: Linux
  • Node.js version: 12
  • Browser: All

Describe the bug

Running Playwright in Nix Shell throws missing dependencies error. Even after explicitly adding the missing dependencies suggested by “launch doctor”, the same error is thrown.

Error: browserType.launch: Host system is missing dependencies!
--
  | Missing libraries we didn't find packages for:
  | libexpat.so.1
  | libxshmfence.so.1

Probably, I’m hitting this error from here. Nix has it’s own ldconfig and it’s not found in sbin.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 23
  • Comments: 32 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Playwright package is now available in unstable channel. I got it working with nix flake by using following flake.nix and then running nix develop

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
  };
 
  outputs = { self, nixpkgs }: with nixpkgs.legacyPackages.x86_64-linux; {
    devShell.x86_64-linux = mkShell {
      buildInputs = [
        playwright
        playwright.browsers
      ];
      shellHook = ''
        export PLAYWRIGHT_BROWSERS_PATH=${playwright.browsers}
        export PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=true
      '';
    };
  };
}
`flake.lock`
{
  "nodes": {
    "nixpkgs": {
      "locked": {
        "lastModified": 1666926733,
        "narHash": "sha256-+gYfOEnQVISPDRNoWm2VJD5OEuTUySt48RchLpvm61o=",
        "owner": "nixos",
        "repo": "nixpkgs",
        "rev": "f44ba1be526c8da9e79a5759feca2365204003f6",
        "type": "github"
      },
      "original": {
        "owner": "nixos",
        "ref": "nixpkgs-unstable",
        "repo": "nixpkgs",
        "type": "github"
      }
    },
    "root": {
      "inputs": {
        "nixpkgs": "nixpkgs"
      }
    }
  },
  "root": "root",
  "version": 7
}

We can make it agnostic to nix: look if ldconfig is on PATH, if not try /sbin/ldconfig, if that doesn’t exist either fail?

@olebedev , would you mind sharing a repo link (or gist) for the Nix derivation you use to wrap playwright’s browser binaries as referenced in #5806? Or are you using the one linked above?

Apologies for commenting on an old issue; I considered sending an email, but figured asking in a public forum might help others with the same question.

Since the PR for skipping the host validation got merged and there won’t be official supported added we will close it for now, feel free to reopen / upvote it when there is a higher demand.

Did you try it, @ml21?

@pbek I haven’t tried devbox yet because I want to use as few additional utilities as possible. I simply specify the version (found on nixhub) of the nixpkgs that has the version of playwright I need.

For example, I need playwright v1.34:

{
  description = "dev env";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    nixpkgs-pw-v1_34.url = "github:NixOS/nixpkgs/dfcffbd74fd6f0419370d8240e445252a39f4d10";
  };

  outputs = { self, nixpkgs, nixpkgs-pw-v1_34 }: let
    system = "x86_64-linux";
    pkgs = import nixpkgs { inherit system; };
    pkgs-pw-v1_34 = import nixpkgs-pw-v1_34 { inherit system; };
  in {
      devShells.${system}.default = pkgs.mkShell {
      packages = [
        pkgs.nodejs_18
        pkgs.dotnet-sdk_8
        pkgs.process-compose
        pkgs-pw-v1_34.playwright
      ];

      PLAYWRIGHT_NODEJS_PATH = "${pkgs.nodejs}/bin/node";
      PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS = true;
      PLAYWRIGHT_BROWSERS_PATH="${pkgs-pw-v1_34.playwright-driver.browsers}";

      shellHook = ''
        echo "dev env started"
        fish && echo "exit dev env" && exit
      '';
    };
  };
}

@olebedev , would you mind sharing a repo link (or gist) for the Nix derivation you use to wrap playwright’s browser binaries as referenced in #5806? Or are you using the one linked above?

@kylegentle, no, I use my own-written Nix derivation. It’s not just a code snippet so I need to go through the OSS request process within the company I work for, to make it public. I will keep you posted here.

A system can have multiple nix profiles. Which one is used is dependent on the environment started. These virtualizations only affect the PATH (and a few other things like env) but not the file system, so changing a symlink is not really idiomatic as you would suddenly bind the static filesystem to a specific, arbitrary nix profile. On CI (where we intend to run this) it would mean that the symlink would point to a random profile or possibly nothing at all. I understand that an env var driving this might be an API we don’t want to introduce but what do you think about falling back from the entry in PATH to the hardcoded location or skipping use of ldconfig altogether like @uri-canva suggested?

@ml2, yes that’s what I would have been done too. Meanwhile, I tried devbox, works great too to pin the version.

Applying a 3000 or 23000 line patch is guaranteed to break at some point. Unless those patches get split up into smaller ones, we cannot maintainable apply them to a source build unless we fetch the same exact version.

One more note when using our browser bundle on Linux instead of the Playwright-provided one: this does mean that you won’t be getting any Playwright-specific browser patches. Maybe someone from the Playwright team could weigh in on the implications here?

playwright install does download the 3 different browser drivers, with a warning BEWARE: your OS is not officially supported by Playwright; downloading fallback build.

The Playwright package splits the browser bundle depending on the platform. For Linux, playwright.browsers is a derivation that emulates what you get when running playwright install, but with the Nixpkgs version of each browser. As @aabccd021 mentioned, you will need to set PLAYWRIGHT_BROWSERS_PATH to this derivation, instead of having Playwright download the precompiled browsers. Try something like this:

export PLAYWRIGHT_BROWSERS_PATH=$(nix build --print-out-paths nixpkgs#playwright.browsers)
nix shell nixpkgs#playwright
playwright open nixos.org

(Add --no-link to the nix build step if you don’t wan’t the result symlink to the browsers derivation). I just tested this using NixOS/nixpkgs@5a350a8f31bb7ef0c6e79aea3795a890cf7743d4 and it seems to work:

grafik

If you’re on Darwin, playwright.browsers will be the same bundle you get when running playwright install (the pre-built binaries by the Playwright team, including their patches), just as an FOD so we get the reproducibility. On Mac, using Playwright’s install script as @adam248 described should work as well, but playwright.browsers in combination with the environment variable will keep everything in the Nix store.

playwright.browsers

I have 22.11 with playwright installed through config… that may work for python, but packages that depend on playwright like Node Slidev still don’t work and you’ll get the dreaded message:

Host system is missing dependencies to run browsers. ║
║ Missing libraries:                                   ║
║     libgobject-2.0.so.0                              ║
║     libglib-2.0.so.0                                 ║
║     libnss3.so                                       ║
║     libnssutil3.so                                   ║
║     libsmime3.so                                     ║
║     libnspr4.so                                      ║
║     libatk-1.0.so.0                                  ║
║     libatk-bridge-2.0.so.0                           ║
║     libcups.so.2                                     ║
║     libgio-2.0.so.0                                  ║
║     libdrm.so.2                                      ║
║     libdbus-1.so.3                                   ║
║     libatspi.so.0                                    ║
║     libX11.so.6                                      ║
║     libXcomposite.so.1                               ║
║     libXdamage.so.1                                  ║
║     libXext.so.6                                     ║
║     libXfixes.so.3                                   ║
║     libXrandr.so.2                                   ║
║     libgbm.so.1                                      ║
║     libexpat.so.1                                    ║
║     libxcb.so.1                                      ║
║     libxkbcommon.so.0                                ║
║     libpango-1.0.so.0                                ║
║     libcairo.so.2                                    ║
║     libasound.so.2

The browsers require a wrapper around them to launch on nix: https://github.com/ludios/nixos-playwright

@uri-canva impressive!

So to double-check: the ldconfig is the last thing that stops you folks from using playwright on nix?

Would you be open to send a PR that detects *NIX system and disables launch doctor there? I cannot guarantee we land it, but we’ll discuss it. I’d appreciate if it’s as non-invasive as possible.

P.S. I apologize for all the questions - I have zero experience with NIX systems, and playing with it in docker was painful - there’s even no bash 🤷‍♂️

Is it possible to make the linked workaround configurable: https://github.com/microsoft/playwright/blob/a9faa9c941fa0b0383d68dff2fb7ef825bd258ea/src/server/validateDependencies.ts#L247-L250?

Note the commit message of https://github.com/aslushnikov/playwright/commit/aa3019bc8225abd463602481e3362fc61ea33b40

To the best of my knowledge, ldconfig is always located at /sbin.

This is one case where it’s not located there, and loading it from there leads to broken behaviour.

@SamBreaksThings we don’t support Nix at the moment and this is the first request we’ve received regarding it so far.

So until there’s a huge demand for Nix support, this will be low-pri for us. Is there something easy we can do to enable you use Playwright on Nix?