|
8 | 8 | # for the the extra dependencies (not included in the `package.json`) for |
9 | 9 | # `node` to execute. This will _not_ install the "transitive" dependencies. |
10 | 10 | # |
11 | | - # Loosely, this will `npm install` each of the tarballs in order so its |
12 | | - # important that |
13 | | - # |
14 | | - # - The dependencies are sorted topologically i.e., each tarball should |
15 | | - # _only_ depend on packages before it in the list. |
| 11 | + # Loosely, this will (in the order given) copy each tarball to a local |
| 12 | + # directory, call `npm cache` on the tarball, and finally call `npm install`. |
16 | 13 | # |
17 | 14 | # For example, if one wanted to include `typescript` as a dependency, then |
18 | 15 | # one could have |
|
57 | 54 | }: |
58 | 55 | pkgs.runCommand (name + "-node2nix") { buildInputs = [ pkgs.node2nix nodejs ]; } |
59 | 56 | '' |
| 57 | + # For some reason, npm likes to call `mkdir $HOME` (which is not |
| 58 | + # writeable when building derivations on nix), so we change `HOME` to a |
| 59 | + # writable directory |
| 60 | + mkdir -p $TMPDIR/home |
| 61 | + export HOME=$TMPDIR/home |
| 62 | +
|
| 63 | +
|
60 | 64 | mkdir -p "$out" |
61 | 65 |
|
62 | 66 | cd "$out" |
|
82 | 86 | chmod +777 package.json |
83 | 87 | chmod +777 package-lock.json |
84 | 88 |
|
85 | | - # `.nix-node-deps/` is the directory to save the tarball |
86 | | - # dependencies from `nix`. |
87 | | - mkdir .nix-node-deps/ |
| 89 | + mkdir -p $TMPDIR/cache |
| 90 | + npm config set cache $TMPDIR/cache |
| 91 | +
|
| 92 | + # Create a directory to store the node dependencies provided by nix |
| 93 | + NIX_NODE_DEPS_PATH=.nix-node-deps/ |
| 94 | + mkdir -p "$NIX_NODE_DEPS_PATH" |
| 95 | +
|
| 96 | + # Helper function to convert a package path to the path in |
| 97 | + # `NIX_NODE_DEPS_PATH` |
| 98 | + pkgPathToNixNodeDepsPath( ) { echo "$NIX_NODE_DEPS_PATH/$(basename "$1")"; } |
88 | 99 |
|
89 | 100 | ${ if dependencies == [] then "" else |
90 | 101 | '' |
|
94 | 105 | # Copying all `dependencies` into `.nix-nodes-deps/` i.e., |
95 | 106 | # we run: |
96 | 107 | # ``` |
97 | | - # echo "Copying <dependency1>" |
98 | | - # cp Copying <dependency1> .nix-node-deps/ |
99 | | - # echo "Copying <dependency2>" |
100 | | - # cp Copying <dependency2> .nix-node-deps/ |
| 108 | + # echo "Copying <dependency1> and adding it to npm's cache" |
| 109 | + # cp <dependency1> "$(pkgPathToNixNodeDepsPath <dependency1>)" |
| 110 | + # npm cache add <dependency1> |
| 111 | + # echo "Copying <dependency2> and adding it to npm's cache" |
| 112 | + # cp <dependency2> "$(pkgPathToNixNodeDepsPath <dependency2>)" |
| 113 | + # npm cache add <dependency2> |
101 | 114 | # ... |
102 | | - # echo "Copying <dependencyN>" |
103 | | - # cp Copying <dependencyN> .nix-node-deps/ |
| 115 | + # echo "Copying <dependencyN> and adding it to npm's cache" |
| 116 | + # cp <dependencyN> "$(pkgPathToNixNodeDepsPath <dependencyN>)" |
| 117 | + # npm cache add <dependencyN> |
104 | 118 | # ``` |
105 | 119 | ${builtins.concatStringsSep "\n" (builtins.map (pkgPath: |
106 | 120 | '' |
107 | | - echo "Copying ${pkgPath}..." |
108 | | - cp "${pkgPath}" .nix-node-deps/ |
| 121 | + echo "Copying ${pkgPath} and adding it to npm's cache..." |
| 122 | + cp ${pkgPath} "$(pkgPathToNixNodeDepsPath "${pkgPath}")" |
| 123 | + npm cache add --loglevel=verbose "$(pkgPathToNixNodeDepsPath "${pkgPath}")" |
109 | 124 | '' |
110 | 125 | ) dependencies)} |
111 | 126 |
|
|
114 | 129 | # npm install --save --package-lock-only <dependency1> <dependency2> ... <dependencyN> |
115 | 130 | # ``` |
116 | 131 | echo 'Running `npm install`...' |
117 | | - HOME=$TMPDIR npm install --save --package-lock-only ${builtins.concatStringsSep " " (builtins.map (pkgPath: ''".nix-node-deps/$(basename "${pkgPath}")"'') dependencies) } |
| 132 | + npm install --loglevel=verbose --save --package-lock-only ${builtins.concatStringsSep " " (builtins.map (pkgPath: ''$(pkgPathToNixNodeDepsPath "${pkgPath}")'') dependencies) } |
118 | 133 | '' |
119 | 134 | } |
120 | 135 |
|
|
0 commit comments