From af084f1eb4d56aab0368196140ebc1b51d2ba200 Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Sun, 1 Nov 2026 16:57:57 +0800 Subject: [PATCH] feat(npm): use npmmirror for npm fetches Set the user-level npm/pnpm registry to npmmirror, and make Nix builds use the same mirror through nixpkgs' npmRegistryOverrides and rewriteURL. The configured Nixpkgs policy is also applied to myNurPkgs and Colmena's nodeNixpkgs so NUR packages such as dsh use the mirror while preserving Colmena parity. --- home/base/core/npm.nix | 1 + modules/base/nix.nix | 3 +++ outputs/default.nix | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/home/base/core/npm.nix b/home/base/core/npm.nix index 7c3806eb..10486e2e 100644 --- a/home/base/core/npm.nix +++ b/home/base/core/npm.nix @@ -4,6 +4,7 @@ # npm - set min-release-age(in days) for supply-chain security home.file.".npmrc".text = '' prefix=${config.home.homeDirectory}/.npm + registry=https://registry.npmmirror.com min-release-age=2 ''; diff --git a/modules/base/nix.nix b/modules/base/nix.nix index 5550f565..6318b016 100644 --- a/modules/base/nix.nix +++ b/modules/base/nix.nix @@ -1,9 +1,12 @@ { pkgs, config, + nixpkgsConfig, ... }: { + nixpkgs.config = nixpkgsConfig; + nix.settings = { # enable flakes globally experimental-features = [ diff --git a/outputs/default.nix b/outputs/default.nix index 332bc81b..a7c937fe 100644 --- a/outputs/default.nix +++ b/outputs/default.nix @@ -9,6 +9,22 @@ let mylib = import ../lib { inherit lib; }; myvars = import ../vars { inherit lib; }; + # Keep npm fetches on the domestic mirror, including inside Nix builds. + # `rewriteURL` covers plain fetchurl calls; `npmRegistryOverrides` covers + # fetchNpmDeps (package-lock tarballs and registry packuments). + nixpkgsConfig = { + allowUnfree = true; + npmRegistryOverrides = { + "registry.npmjs.org" = "https://registry.npmmirror.com"; + }; + rewriteURL = + url: + if lib.hasPrefix "https://registry.npmjs.org/" url then + "https://registry.npmmirror.com/${lib.removePrefix "https://registry.npmjs.org/" url}" + else + url; + }; + # Add my custom lib, vars, nixpkgs instance, and all the inputs to specialArgs, # so that I can use them in all my nixos/home-manager/darwin modules. genSpecialArgs = @@ -29,6 +45,7 @@ let inputs mylib myvars + nixpkgsConfig pkgs-stable ; @@ -58,7 +75,14 @@ let }; # NUR packages for this system; modules use `myNurPkgs.dsh` instead of # repeating `nur-ryan4yin.packages.${pkgs.stdenv.hostPlatform.system}`. - myNurPkgs = inputs.nur-ryan4yin.packages.${system}; + # Instantiate them with the same Nixpkgs policy as the host so npm + # fetchers inside NUR packages also use the mirror. + myNurPkgs = import "${inputs.nur-ryan4yin}" { + pkgs = import inputs.nixpkgs { + inherit system; + config = nixpkgsConfig; + }; + }; }; # This is the args for all the haumea modules in this folder. @@ -106,7 +130,11 @@ let meta = { nixpkgs = import nixpkgs { system = "x86_64-linux"; }; nodeNixpkgs = builtins.mapAttrs ( - name: _: import nixpkgs { inherit (nixosConfigurations.${name}.pkgs.stdenv.hostPlatform) system; } + name: _: + import nixpkgs { + inherit (nixosConfigurations.${name}.pkgs.stdenv.hostPlatform) system; + config = nixpkgsConfig; + } ) colmenaDeployments; nodeSpecialArgs = builtins.mapAttrs ( name: _: nixosConfigurations.${name}._module.specialArgs