npmnixosnixpkgs

With nixos can I install a nodejs package globally?


Say I'd like to install @squoosh/cli, this package is mentioned in nixpkgs here. Is there a way to specify the package in configuration.nix, or in the home-manager, to have it be installed by nixos-rebuild?


Solution

  • Yes, the packages in node-packages.json appear in pkgs.nodePackages.

    In NixOS, you can add it as follows:

    { pkgs, ... }: {
    
      # ...
    
      environment.systemPackages = [
        pkgs.nodePackages."@squoosh/cli"
      ];
    
    }
    

    In Home Manager it's similar, but home.packages instead of environment.systemPackages.

    EDIT: Some node packages are packaged differently. It's worth giving search.nixos.org a try. Personally I don't think any of the current node packaging methods work well for Nixpkgs. The implementation of dynamic derivations will likely lead to a significantly improved solution.