Wiki

Guix Guide

Setting Up NPM for Global Install

Due to the filesystem structure that guix follows, it is not possible to install npm packages globally, the following guide helps you configure a local folder for installing global npm packages

  1. Install node from guix repo
  2. Either install it into user's guix profile using the following command:

    $ guix pull
    $ guix install node
          

    Or install it system-wide by adding the line (specification->package "node") to your system's config file /etc/config.scm as shown below:

    (operating-system
      ...
      (packages
        (append
          (list
            ...
            (specification->package "node")
            ...)
          %base-packages))
    ...)
          

    Reconfigure the system if installing node system-wide

    $ guix pull
    $ sudo guix system reconfigure /etc/config.scm
          
  3. Create a folder for npm global installation of packages in .vnode directory in home directory and enable it
  4. $ mkdir $HOME/.vnode
    $ npm config set prefix "$HOME/.vnode"
          
  5. Add the bin folder inside .vnode directory to ~/.bashrc or ~/.zshrc and then source the file. It's recommended that you logout and log back in. Otherwise, you'll need to source the directory everytime you need to use the npm.
  6. $ echo "export PATH=$HOME/.vnode/bin:$PATH" >> ~/.bashrc
    $ source ~/.bashrc
          
  7. Install npm globally and it will install into that directory
  8. $ npm install -g npm
          
  9. Now add the following preinstall script into $HOME/.vnode/lib/node_modules/.hooks/preinstall
  10. #/usr/bin/env bash
    
    pkg_path=$PWD
    
    function patch_shebang() {
        file=$1
        python_bin=`type -p python`
        ruby_bin=`type -p ruby`
        env_bin=`type -p env`
        bash_bin=`type -p bash`
    
        if [ -n "$env_bin" ]; then
            sed -i -uE "s|^#!.+/env|#!${env_bin}|" $file
        elif [ -n "$bash_bin" ]; then
            sed -i -uE "s|^#!.+/bash|#!${bash_bin}|" $file
        elif [ -n "$python_bin" ]; then
            sed -i -uE "s|^#!.+/bash|#!${python_bin}|" $file
        elif [ -n "$ruby_bin" ]; then
            sed -i -uE "s|^#!.+/bash|#!${ruby_bin}|" $file
        fi
    }
    
    files=`find $pkg_path -type f -exec grep -lE '^#!(.+ )' {} \;`
    
    for file in $files; do
        patch_shebang $file
    done
          
  11. Allow execution of the script
  12. $ chmod a+rx $HOME/.vnode/node_modules/.hooks/preinstall
          
  13. If everything went well, you will now be able to run npm install -g package-name and install global packages into the newly created directory. For local packages just copy the hook myprojectdir/node_modules/.hooks/preinstall.

Yours Truly,
Akuma X Machina