Skip to content

Commit

Permalink
feat(build): #834 npm registry tokens
Browse files Browse the repository at this point in the history
- allow passing tokens for private registries

Signed-off-by: David Acevedo <[email protected]>
  • Loading branch information
dacevedo12 committed Jul 14, 2023
1 parent 2244b2b commit 2f7356b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
80 changes: 80 additions & 0 deletions docs/src/api/extensions/node.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Types:
- shouldIgnoreScripts (`bool`): Optional.
Enable to propagate the `--ignore-scripts true` flag to npm.
Defaults to `false`.
- registryTokens (`attrsOf str`): Optional.
Tokens for dependencies to be fetched from private NPM registries.
Defaults to `{ }`.

Example:

Expand Down Expand Up @@ -130,6 +133,83 @@ Example:
hello-world-npm
```

Example with private registries:

=== "package.json"

```json
# /path/to/my/project/makes/example/package.json
{
"dependencies": {
"@fortawesome/fontawesome-pro": "*"
}
}
```

=== "package-lock.json"

```json
# /path/to/my/project/makes/example/package-lock.json
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"@fortawesome/fontawesome-pro": {
"version": "6.4.0",
"resolved": "https://npm.fontawesome.com/@fortawesome/fontawesome-pro/-/6.4.0/fontawesome-pro-6.4.0.tgz",
"integrity": "sha512-VtoAOuV0KAjdO979RHGko5krp3UsKMnXH1SaHnQvlz4PcgErcsk5ZPugoMhc3sW5lkrRl8NnaGwkGzB3gzVSxQ=="
}
}
}
```

=== "main.nix"

```nix
# /path/to/my/project/main.nix
{
makeNodeJsModules,
makeScript,
projectPath,
...
}:
let
secrets = secretsForEnvFromSops = {
example = {
manifest = "/path/to/my/project/secrets.yaml";
vars = [ "FONTAWESOME_PRO_TOKEN" ];
};
};
fontawesome = makeNodeJsModules {
name = "fontawesome-pro-example";
nodeJsVersion = "18";
packageJson = projectPath "/path/to/my/project/package.json";
packageLockJson = projectPath "/path/to/my/project/package-lock.json";
registryTokens = {
"@fortawesome/fontawesome-pro": "FONTAWESOME_PRO_TOKEN"
};
searchPaths.source = [ secrets.example ]
};
in
makeScript {
replace = {
__argFontawesome__ = fontawesome;
};
entrypoint = ''
ls __argFontawesome__
'';
name = "example";
}
```

=== "Invocation"

```bash
$ m . /example

@fortawesome
```

## makeNodeJsEnvironment

Setup a `makeNodeJsModules` in the environment
Expand Down
9 changes: 9 additions & 0 deletions src/args/make-node-js-modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
nodeJsVersion,
packageJson,
packageLockJson,
registryTokens ? {},
searchPaths ? {},
shouldIgnoreScripts ? false,
}: let
Expand All @@ -22,6 +23,10 @@
collectDependencies = deps:
builtins.foldl'
(all: name: let
registryToken =
if builtins.hasAttr name registryTokens
then builtins.getEnv registryTokens.${name}
else null;
tarball = __nixpkgs__.fetchurl {
hash = depAttrs.integrity;
url =
Expand All @@ -33,6 +38,10 @@
then depAttrs.version
# Something pending to implement?
else abort "Unable to fetch: ${name}";
curlOptsList =
if registryToken == null
then []
else ["--header" "Authorization: Bearer ${registryToken}"];
};
dep =
depAttrs
Expand Down

0 comments on commit 2f7356b

Please sign in to comment.