Unverified Commit e09ce286 authored by Connor Baker's avatar Connor Baker Committed by GitHub
Browse files

config, fetchNpmDeps: add npmRegistryOverrides (#447451)

parents b4191b81 712a4af3
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -262,6 +262,8 @@

- Added `gitConfig` and `gitConfigFile` option to the nixpkgs `config`, to allow for setting a default `gitConfigFile` for all `fetchgit` invocations.

- Added `npmRegistryOverrides` and `npmRegistryOverridesString` option to the nixpkgs `config`, to allow for setting a default `npmRegistryOverridesString` for all `fetchNpmDeps` invocations.

- The `dockerTools.streamLayeredImage` builder now uses a better algorithm for generating layered docker images, such that much more sharing is possible when the number of store paths exceeds the layer limit. It gives each of the largest store paths its own layer and adds dependencies to those layers when they aren't used elsewhere.

- The systemd initrd will now respect `x-systemd.wants` and `x-systemd.requires` for reliably unlocking multi-disk bcachefs volumes.
@@ -304,6 +306,8 @@

- `nix-prefetch-git`: Added a `--no-add-path` argument to disable adding the path to the store; this is useful when working with a [read-only store](https://nix.dev/manual/nix/2.28/command-ref/new-cli/nix3-help-stores#store-experimental-local-overlay-store-read-only).

- `fetchNpmDeps`: Add `npmRegistryOverridesString` argument to pass NPM registry overrides to the fetcher.

- `sftpman` has been updated to version 2, a rewrite in Rust which is mostly backward compatible but does include some changes to the CLI.
  For more information, [check the project's README](https://github.com/spantaleev/sftpman-rs#is-sftpman-v2-compatible-with-sftpman-v1).

+25 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
  cacert,
  prefetch-npm-deps,
  fetchNpmDeps,
  config,
}:

{
@@ -68,6 +69,7 @@
            hash,
            forceGitDeps ? false,
            forceEmptyCache ? false,
            npmRegistryOverridesString ? "{}",
          }:
          testers.invalidateFetcherByDrvHash fetchNpmDeps {
            inherit
@@ -75,6 +77,7 @@
              hash
              forceGitDeps
              forceEmptyCache
              npmRegistryOverridesString
              ;

            src = makeTestSrc { inherit name src; };
@@ -175,6 +178,23 @@

          hash = "sha256-FhxlJ0HdJMPiWe7+n1HaGLWOr/2HJEPwiS65uqXZM8Y=";
        };

        # Test that npmRegistryOverrides work
        npmRegistryOverrides = makeTest {
          name = "npm-registry-overrides";

          src = fetchurl {
            url = "https://cyberchaos.dev/yuka/trainsearch/-/raw/e3cba6427e8ecfd843d0f697251ddaf5e53c2327/package-lock.json";
            postFetch = "sed -i 's/registry.npmjs.org/broken.link/' $out";
            hash = "sha256-Qo24ei1d9Ql4zCLjQJ04zVgS4qhBUpew9NZrhrsBds4=";
          };

          npmRegistryOverridesString = builtins.toJSON {
            "broken.link" = "https://registry.npmjs.org";
          };

          hash = "sha256-QGObVDd9qVtf/U78+ayP6RHVWsU+HXhg70BFblQ1PZs=";
        };
      };

    meta = with lib; {
@@ -192,6 +212,9 @@
      forceGitDeps ? false,
      forceEmptyCache ? false,
      nativeBuildInputs ? [ ],
      # A string with a JSON attrset specifying registry mirrors, for example
      #   {"registry.example.org": "my-mirror.local/registry.example.org"}
      npmRegistryOverridesString ? config.npmRegistryOverridesString,
      ...
    }@args:
    let
@@ -243,6 +266,8 @@
        # `{ "registry.example.com": "example-registry-bearer-token", ... }`
        impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "NIX_NPM_TOKENS" ];

        NIX_NPM_REGISTRY_OVERRIDES = npmRegistryOverridesString;

        SSL_CERT_FILE =
          if
            (
+16 −0
Original line number Diff line number Diff line
@@ -18,6 +18,22 @@ use std::{
use url::Url;

pub fn get_url(url: &Url) -> Result<Body, anyhow::Error> {
    let url_ = url.clone();
    let mut url = url.clone();
    // Respect NIX_NPM_REGISTRY_OVERRIDES environment variable, which should be a JSON mapping in the shape of:
    // `{ "registry.example.com": "my-registry.local", ... }`
    if let Some(host) = url.host_str() {
        if let Ok(npm_mirrors) = env::var("NIX_NPM_REGISTRY_OVERRIDES") {
            if let Ok(mirrors) = serde_json::from_str::<Map<String, Value>>(&npm_mirrors) {
                if let Some(mirror) = mirrors.get(host).and_then(serde_json::Value::as_str) {
                    let mirror_url = Url::parse(mirror)?;
                    url.set_path(&(mirror_url.path().to_owned() + url.path()));
                    url.set_host(Some(mirror_url.host_str().expect(format!("Mirror URL without host part: {mirror_url}").as_str())))?;
                    eprintln!("Replaced URL {url_} with {url}");
                }
            }
        }
    }
    let mut request = Request::get(url.as_str()).redirect_policy(RedirectPolicy::Limit(10));

    // Respect SSL_CERT_FILE if environment variable exists
+33 −0
Original line number Diff line number Diff line
@@ -139,6 +139,39 @@ let
          null;
    };

    npmRegistryOverrides = mkOption {
      type = types.attrsOf types.str;
      description = ''
        The default NPM registry overrides for all `fetchNpmDeps` calls, as an attribute set.

        For each attribute, all files fetched from the host corresponding to the name will instead be fetched from the host (and sub-path) specified in the value.

        For example, an override like `"registry.npmjs.org" = "my-mirror.local/registry.npmjs.org"` will replace a URL like `https://registry.npmjs.org/foo.tar.gz` with `https://my-mirror.local/registry.npmjs.org/foo.tar.gz`.

        To set the string directly, see [`npmRegistryOverridesString`](#opt-npmRegistryOverridesString).
      '';
      default = { };
      example = {
        "registry.npmjs.org" = "my-mirror.local/registry.npmjs.org";
      };
    };

    npmRegistryOverridesString = mkOption {
      type = types.addCheck types.str (
        s:
        let
          j = builtins.fromJSON s;
        in
        lib.isAttrs j && lib.all builtins.isString (builtins.attrValues j)
      );
      description = ''
        A string containing a string with a JSON representation of NPM registry overrides for `fetchNpmDeps`.

        This overrides the [`npmRegistryOverrides`](#opt-npmRegistryOverrides) option, see its documentation for more details.
      '';
      default = builtins.toJSON config.npmRegistryOverrides;
    };

    doCheckByDefault = mkMassRebuild {
      feature = "run `checkPhase` by default";
    };