From 785ed11d0a71952c7a27fc66d7754bae1da09a64 Mon Sep 17 00:00:00 2001 From: Lily Foster Date: Wed, 9 Aug 2023 19:13:13 -0400 Subject: [PATCH 1/3] prefetch-npm-deps: fix error typo and unnecessary name qualifier --- pkgs/build-support/node/fetch-npm-deps/src/main.rs | 2 +- pkgs/build-support/node/fetch-npm-deps/src/parse/mod.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/node/fetch-npm-deps/src/main.rs b/pkgs/build-support/node/fetch-npm-deps/src/main.rs index 62e5752c74c0..9d86bd8091a7 100644 --- a/pkgs/build-support/node/fetch-npm-deps/src/main.rs +++ b/pkgs/build-support/node/fetch-npm-deps/src/main.rs @@ -108,7 +108,7 @@ fn fixup_lockfile( // Recursive helper to fixup v1 lockfile deps fn fixup_v1_deps( - dependencies: &mut serde_json::Map, + dependencies: &mut Map, cache: &Option>, fixed: &mut bool, ) { diff --git a/pkgs/build-support/node/fetch-npm-deps/src/parse/mod.rs b/pkgs/build-support/node/fetch-npm-deps/src/parse/mod.rs index e1b491cccea2..b37652ffdf82 100644 --- a/pkgs/build-support/node/fetch-npm-deps/src/parse/mod.rs +++ b/pkgs/build-support/node/fetch-npm-deps/src/parse/mod.rs @@ -139,9 +139,9 @@ impl Package { None => Specifics::Registry { integrity: pkg .integrity - .expect("non-git dependencies should have assosciated integrity") + .expect("non-git dependencies should have associated integrity") .into_best() - .expect("non-git dependencies should have non-empty assosciated integrity"), + .expect("non-git dependencies should have non-empty associated integrity"), }, }; From e2712661790665b4aa4223652ca8a182490a04ed Mon Sep 17 00:00:00 2001 From: Lily Foster Date: Mon, 11 Sep 2023 16:49:36 -0400 Subject: [PATCH 2/3] prefetch-npm-deps: add support for NIX_NPM_TOKENS env var --- pkgs/build-support/node/fetch-npm-deps/src/util.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkgs/build-support/node/fetch-npm-deps/src/util.rs b/pkgs/build-support/node/fetch-npm-deps/src/util.rs index a165461fa71a..7a220f681c0d 100644 --- a/pkgs/build-support/node/fetch-npm-deps/src/util.rs +++ b/pkgs/build-support/node/fetch-npm-deps/src/util.rs @@ -3,6 +3,7 @@ use isahc::{ config::{CaCertificate, Configurable, RedirectPolicy, SslOption}, Body, Request, RequestExt, }; +use serde_json::{Map, Value}; use std::{env, path::Path}; use url::Url; @@ -22,6 +23,18 @@ pub fn get_url(url: &Url) -> Result { } } + // Respect NIX_NPM_TOKENS environment variable, which should be a JSON mapping in the shape of: + // `{ "registry.example.com": "example-registry-bearer-token", ... }` + if let Some(host) = url.host_str() { + if let Ok(npm_tokens) = env::var("NIX_NPM_TOKENS") { + if let Ok(tokens) = serde_json::from_str::>(&npm_tokens) { + if let Some(token) = tokens.get(host).and_then(|val| val.as_str()) { + request = request.header("Authorization", format!("Bearer {token}")); + } + } + } + } + Ok(request.body(())?.send()?.into_body()) } From 7f76ac6e098c7d0b8793b829cf122da5901e130c Mon Sep 17 00:00:00 2001 From: Lily Foster Date: Mon, 11 Sep 2023 16:50:17 -0400 Subject: [PATCH 3/3] fetchNpmDeps: pass NIX_NPM_TOKENS as an impure env var --- pkgs/build-support/node/fetch-npm-deps/default.nix | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkgs/build-support/node/fetch-npm-deps/default.nix b/pkgs/build-support/node/fetch-npm-deps/default.nix index ac76758ba50e..67a4c337c0d2 100644 --- a/pkgs/build-support/node/fetch-npm-deps/default.nix +++ b/pkgs/build-support/node/fetch-npm-deps/default.nix @@ -165,7 +165,9 @@ dontInstall = true; - impureEnvVars = lib.fetchers.proxyImpureEnvVars; + # NIX_NPM_TOKENS environment variable should be a JSON mapping in the shape of: + # `{ "registry.example.com": "example-registry-bearer-token", ... }` + impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "NIX_NPM_TOKENS" ]; SSL_CERT_FILE = if (hash_.outputHash == "" || hash_.outputHash == lib.fakeSha256 || hash_.outputHash == lib.fakeSha512 || hash_.outputHash == lib.fakeHash) then "${cacert}/etc/ssl/certs/ca-bundle.crt"