Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/misc/news/2025/12/2025-12-06_11-03-01.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
time = "2025-12-06T10:03:01+00:00";
condition = true;
message = ''
A new module is available: `programs.npm`

It allows you manage your npm user configuration (`.npmrc`)
and install a specific version of the package.
'';
}
74 changes: 74 additions & 0 deletions modules/programs/npm.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/programs/npm.nix
{
config,
lib,
pkgs,
...
}:
let
cfg = config.programs.npm;

xdgConfigHome = lib.removePrefix config.home.homeDirectory config.xdg.configHome;
configFile = if config.home.preferXdgDirectories then "${xdgConfigHome}/npm/npmrc" else ".npmrc";

iniFormat = pkgs.formats.ini {
listsAsDuplicateKeys = true;
};

toNpmrc =
let
mkLine = lib.generators.mkKeyValueDefault { } "=";
mkLines = k: v: if lib.isList v then map (x: mkLine "${k}[]" x) v else [ (mkLine k v) ];
in
attrs: lib.concatLines (lib.concatLists (lib.mapAttrsToList mkLines attrs));
in
{
meta.maintainers = with lib.maintainers; [ mirkolenz ];

options = {
programs.npm = {
enable = lib.mkEnableOption "{command}`npm` user config";

package = lib.mkPackageOption pkgs [ "nodejs" ] {
example = "nodejs_24";
nullable = true;
};

settings = lib.mkOption {
type = lib.types.attrsOf iniFormat.lib.types.atom;
description = ''
The user-specific npm configuration.
See <https://docs.npmjs.com/cli/using-npm/config> and
<https://docs.npmjs.com/cli/configuring-npm/npmrc>
for more information.
'';
default = {
prefix = "\${HOME}/.npm";
};
example = lib.literalExpression ''
{
color = true;
include = [
"dev"
"prod"
];
init-license = "MIT";
prefix = "''${HOME}/.npm";
}
'';
};
};
};

config = lib.mkIf cfg.enable {
home = {
packages = lib.mkIf (cfg.package != null) [ cfg.package ];
file.${configFile} = lib.mkIf (cfg.settings != { }) {
text = toNpmrc cfg.settings;
};
sessionVariables = lib.mkIf (cfg.settings != { }) {
NPM_CONFIG_USERCONFIG = "${config.home.homeDirectory}/${configFile}";
};
Comment on lines +69 to +71
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could see a potential to support customized location, in the future, since this is setting a variable a user might want control over without using mkForce.

};
};
}
4 changes: 4 additions & 0 deletions tests/modules/programs/npm/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
npm-example-settings = ./example-settings.nix;
npm-no-settings = ./no-settings.nix;
}
36 changes: 36 additions & 0 deletions tests/modules/programs/npm/example-settings.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{ pkgs, ... }:

{
programs.npm = {
enable = true;
settings = {
color = true;
include = [
"dev"
"prod"
];
init-license = "MIT";
prefix = "\${HOME}/.npm";
};
};

test.stubs.nodejs = { };

nmt.script =
let
configPath = "home-files/.npmrc";
expectedConfig = pkgs.writeText "npmrc-expected" ''
color=true
include[]=dev
include[]=prod
init-license=MIT
prefix=''${HOME}/.npm
'';
in
''
assertFileExists "${configPath}"
assertFileContent "${configPath}" "${expectedConfig}"
assertFileContains home-path/etc/profile.d/hm-session-vars.sh \
'export NPM_CONFIG_USERCONFIG="/home/hm-user/.npmrc"'
'';
}
17 changes: 17 additions & 0 deletions tests/modules/programs/npm/no-settings.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{ ... }:

{
programs.npm = {
enable = true;
settings = { };
};

test.stubs.nodejs = { };

nmt.script = ''
assertPathNotExists home-files/.npmrc
assertFileExists home-path/etc/profile.d/hm-session-vars.sh
assertFileNotRegex home-path/etc/profile.d/hm-session-vars.sh \
"export NPM_CONFIG_USERCONFIG="
'';
}