Skip to content

Commit 7abb4fe

Browse files
committed
npm: add module
1 parent 27a6182 commit 7abb4fe

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

modules/programs/npm.nix

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/programs/npm.nix
2+
{
3+
config,
4+
lib,
5+
pkgs,
6+
...
7+
}:
8+
let
9+
cfg = config.programs.npm;
10+
in
11+
{
12+
meta.maintainers = with lib.maintainers; [ mirkolenz ];
13+
14+
options = {
15+
programs.npm = {
16+
enable = lib.mkEnableOption "{command}`npm` user config";
17+
18+
package = lib.mkPackageOption pkgs [ "nodePackages" "npm" ] {
19+
example = "nodePackages_13_x.npm";
20+
nullable = true;
21+
};
22+
23+
npmrc = lib.mkOption {
24+
type = lib.types.lines;
25+
description = ''
26+
The user-specific npm configuration.
27+
See <https://docs.npmjs.com/misc/config>.
28+
'';
29+
default = ''
30+
prefix = ''${HOME}/.npm
31+
'';
32+
example = ''
33+
prefix = ''${HOME}/.npm
34+
https-proxy=proxy.example.com
35+
init-license=MIT
36+
init-author-url=https://www.npmjs.com/
37+
color=true
38+
'';
39+
};
40+
};
41+
};
42+
config = lib.mkIf cfg.enable {
43+
home = {
44+
packages = lib.mkIf (cfg.package != null) [ cfg.package ];
45+
file.".npmrc" = lib.mkIf (cfg.npmrc != "") {
46+
text = cfg.npmrc;
47+
};
48+
sessionVariables = lib.mkIf (cfg.npmrc != "") {
49+
NPM_CONFIG_USERCONFIG = "${config.home.homeDirectory}/.npmrc";
50+
};
51+
};
52+
};
53+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
npm-example-settings = ./example-settings.nix;
3+
npm-no-npmrc = ./no-npmrc.nix;
4+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{ pkgs, ... }:
2+
3+
{
4+
programs.npm = {
5+
enable = true;
6+
npmrc = ''
7+
prefix = ''${HOME}/.npm
8+
registry=https://registry.example.com/
9+
color=true
10+
'';
11+
package = null;
12+
};
13+
14+
nmt.script =
15+
let
16+
configPath = "home-files/.npmrc";
17+
expectedConfig = pkgs.writeText "npmrc-expected" ''
18+
prefix = ''${HOME}/.npm
19+
registry=https://registry.example.com/
20+
color=true
21+
'';
22+
in
23+
''
24+
assertFileExists "${configPath}"
25+
assertFileContent "${configPath}" "${expectedConfig}"
26+
assertFileContains home-path/etc/profile.d/hm-session-vars.sh \
27+
'export NPM_CONFIG_USERCONFIG="/home/hm-user/.npmrc"'
28+
'';
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{ pkgs, ... }:
2+
3+
{
4+
programs.npm = {
5+
enable = true;
6+
npmrc = "";
7+
package = null;
8+
};
9+
10+
nmt.script = ''
11+
assertPathNotExists home-files/.npmrc
12+
assertFileExists home-path/etc/profile.d/hm-session-vars.sh
13+
assertFileNotRegex home-path/etc/profile.d/hm-session-vars.sh \
14+
"export NPM_CONFIG_USERCONFIG="
15+
'';
16+
}

0 commit comments

Comments
 (0)