Debian alternatives system: programs with the same name and priorities

alternativesdebian

I can't understand from the update-alternatives documentation how the priorities system works.

Suppose that I want /usr/bin/node to point to nodejs for nodejs applications and to an amateur radio program, called node, in other cases. I have to say:

update-alternatives --install /usr/bin/node nodejs /usr/bin/nodejs 100 --slave /usr/share/man/...

Suppose I have 2 programs that expect /usr/bin/node to be different things, first – nodejs, second – amateur radio. How would they determine which one to use?

What's the role of priority in this process?

ADDITION: Note, that npm, nodejs package manager, expects nodejs command to be called node, thus I have to install nodejs as node.

Best Answer

At any time, /usr/bin/node can only be one of the programs. The alternatives mechanism is a way to choose which one it is.

Priorities control which one is the default when both are installed. They don't offer a way to somehow have both and select between the two based on what other program called this one.

When programs call each other by name, there can only be a single program by a given name that comes first on the search path. You should disambiguate the node command in your programs. Following Debian, use nodejs for Node.js and ax25-node for the HAM program. If there's one that it would be really annoying to change, install either the node package (which makes /usr/bin/node an alias for ax25-node) or the nodejs-legacy package (which makes /usr/bin/node an alias for nodejs).

If you really have a lot of programs that call node and that can't be changed easily, run them with different PATH variables, one containing a directory containing a symbolic link to ax25-node and one containing a directory containing a symbolic link to nodejs:

mkdir -p /usr/local/etc/nodejs/bin /usr/local/etc/ax25/bin
ln -s ../../../../bin/nodejs /usr/local/etc/nodejs/bin/node
ln -s ../../../../bin/ax25-node /usr/local/etc/ax25/bin/node

PATH=/usr/local/etc/ax25/bin:$PATH program-using-ax25
PATH=/usr/local/etc/node/bin:$PATH program-using-nodejs