[LINUX] How to use Nix package manager

Nix is good

I use Windows (company-supplied) for work, MacBook Pro (given by a friend) for everyday use, and Manjaro Linux (given by a senior company) for experimentation, all of which are package managers. I'm using nix. (However, Windows uses Nix in Ubuntu on WSL2)

The reason why I use Nix instead of using apt or homebrew quietly is

  1. Easy to reproduce environment because environment can be managed declaratively
  2. Highly customizable
  3. No need for docker
  4. No dust remains in unexpected places when uninstalling
  5. The environment can be restored at any time

I'm happy about that.

I want Nix to become more popular, so I'll show you how to install the Nix package manager and how to use basic commands.

Nix installation

Linux

$ sh <(curl -L https://nixos.org/nix/install)

One caveat is that you can't run it with a fish shell.

macOS

$ sh <(curl -L https://nixos.org/nix/install) --darwin-use-unencrypted-nix-store-volume

Addendum to .bashrc

In ~ / .bashrc to set the necessary environment variables to use nix on both Linux and Mac

if [ -e $HOME/.nix-profile/etc/profile.d/nix.sh ]; then
    . $HOME/.nix-profile/etc/profile.d/nix.sh;
fi

Let's add.

Channel registration

First, you need to subscribe to a Unix package channel to use it. Use the nix-channel command to manipulate channels.

$ nix-channel --add https://nixos.org/channels/nixpkgs-unstable
$ nix-channel --update

nix-channel --update is like sudo apt update.

Package management

The main command for package management is nix-env. You can use nix-env to install, upgrade, remove, search for packages, and more.

Search

You can browse all installable packages with nix-env -qa. The same is true for nix-env --query --available.

$ nix-env -qa
aterm-2.2
bash-3.0
binutils-2.15
bison-1.875d
blackdown-1.4.2
bzip2-1.0.2
...

You can search for a specific package by putting the name of the package after -qa.

$ nix-env -qa firefox
firefox-68.11.0esr
firefox-78.1.0esr
firefox-79.0

You can also use regular expressions to search.

$ nix-env -qa 'firefox-.*'

Package installation

You can install the package with nix-env -i or nix-env --install.

By the way, you don't need sudo. Installed packages are installed by the user, not the system. There is (probably) no impact on the entire system.

$ nix-env -i subversion

You can check all installed packages with nix-env -q.

$ nix-env -q
nix-2.3.7
subversion-1.12.2

upgrade

You can update your environment with nix-env -u or nix-env --upgrade.

When upgrading only a specific package,

$ nix-env -u subversion

When upgrading all installed packages,

$ nix-env -u

Uninstall

You can uninstall the package with nix-env -e or nix-env --erase.

nix-env -e subversion
uninstalling 'subversion-1.12.2'

rollback

Nix creates a generation for your environment each time you add or remove a package. And you can roll back to those generations at any time.

For example, it is useful when you have installed a package and had a bug and want to revert to a working installation.

Check generations nix-env --list-generations

$ nix-env --list-generations
   1   2020-08-03 23:36:12   
   2   2020-08-06 19:15:43   
   3   2020-08-06 19:55:13   
   4   2020-08-09 18:33:59   
   5   2020-08-09 18:34:46   (current)

(current) is the current environment.

Use nix-env --rollback to return to the previous environment.

$ nix-env --rollback
switching from generation 5 to 4

$ nix-env --list-generations
   1   2020-08-03 23:36:12   
   2   2020-08-06 19:15:43   
   3   2020-08-06 19:55:13   
   4   2020-08-09 18:33:59   (current)
   5   2020-08-09 18:34:46   

To move to a specific generation, use nix-env --switch-generation.

$ nix-env --switch-generation 2
switching from generation 4 to 2

Recommended Posts

How to use Nix package manager
How to use Python's Context Manager
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use Pandas 2
How to use Virtualenv
How to use pytest_report_header
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Golang flag package (minimum knowledge)
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
Python: How to use pydub
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes
How to use cron (personal memo)
Python: How to use async with
How to use the zip function