nix-community / npmlock2nix

nixify npm based packages [maintainer=@andir]
Apache License 2.0
130 stars 42 forks source link

Unclear how to set environment variables in build & shell #156

Closed thomashoneyman closed 2 years ago

thomashoneyman commented 2 years ago

Hi all -- wonderful library!

I'm attempting to use playwright for browser automation on nodejs, but simply installing the library via npmlock2nix fails because it has an install script that will download bundled browsers (naturally, this isn't allowed in a Nix environment).

I can work around the install script by forking npmlock2nix and updating it to pass the --ignore-scripts flag to the npm install command on in internal.nix#L401. However, I'd prefer to use environment variables supported by Playwright to turn off installing browsers.

Outside of Nix I can do that like this:

$ PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm i --save playwright
...
Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set
...

However, I'm struggling with getting this environment variable to take when using npmlock2nix. I tried putting it my shell:

{
  shell = npmlock2nix.shell {
    # No effect as far as I can tell
    PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = 1;
    preConfigure = ''
      # No effect as far as I can tell
      export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
    '';
    shellHook = ''
      # Too late, installation has already occurred
      export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
    '';
  };
}

Most likely this is just my experience, but I'm not seeing how I can set environment variables here that NPM will pick up. Any advice is appreciated!

thomashoneyman commented 2 years ago

Actually, setting the environment variable in the shell derivation is working:

{
  shell = npmlock2nix.shell {
    PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = 1;
    shellHook = ''
      echo $PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD
    '';
}

Starting a Nix shell with the above code, with install scripts disabled, will print out 1 (or whatever you set the env var to) in the console. So clearly this env variable is around, but it doesn't appear to be taking effect when NPM is running the installation. Is it possible that it isn't propagating from the shell to the internal call to the node_modules function?

thomashoneyman commented 2 years ago

Ah! Ok, this is what get_node_modules_attrs must be for, then:

{
  shell = npmlock2nix.shell {
    node_modules_attrs = { PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = 1; };
  };
}

This behaves the way I would expect: even without install scripts disabled, this properly skips the browser download.