Nix usage

From wikinotes

Administration

Nix Upgrades

# upgrade nix itself
nix upgrade-nix

# fetch latest packages and update
nix-channel --update  # upgrade package index
nix-env -u            # upgrade everything
nix-env -i direnv     # install package
nix-env -e direnv     # uninstall package

Package Management

packages are installed globally, and activated within a profile/env.
projects can have their own env, and so can users.

# set current nix profile (~/.nix-profile)
nix-env --switch-profile /nix/var/nix/profiles/x
nix-channel --update    # fetch packagelist update

nix search vim          # search for package 'vim'

nix-env -i vim          # install package   (in current profile)
nix-env -e vim          # uninstall package (in current profile)
nix-env -u vim          # upgrade package   (in current profile)

nix-env -q --installed  # list installed packages
nix package search https://search.nixos.org/packages#openshift
nix packages https://github.com/NixOS/nixpkgs/find/master

Package Environment

nix-shell -p vim            # resolve nix shell with vim
nix-shell -p vim --run vim  # resolve nix-shell and run vim

nix eval --raw nixpkgs.vim          # list path to vim package
find $(nix eval --raw nixpkgs.vim)  # show contents of vim package

Profiles/Generations

a profile defines a set of packages. changes to profiles are recorded atomically, each change is a generation.

nix-env -p /nix/var/nix/profiles/x -i vim         # install vim within (non-active) profile 'x'

nix-env --switch-profile /nix/var/nix/profiles/x  # change current profile
nix-env --rollback                                # revert back one nix profile generation
nix-env --switch-generation 43                    # choose profile generation
nix-env --list-generations                        # list generations

Garbage Collection

NOTE:

Don't manually delete things from your nix store.
If you do, just reinstall nix, and follow it's prompts to remove all installed nix files.

nix-env --delete-generations old  # delete all non-current profiles
nix-env --delete-generations 14d  # delete genrations older than 14 days
nix-env --delete-generation 10 11 # delete specific generations

nix-env --gc                      # after deleting generations, gc will remove unused packages

Development

shell environments

nix-shell                     # evaluates instructions in `pwd`/shell.nix
nix-shell -p vim ripgrep ...  # dynamic nix shell with vim/ripgrep/... packages

You can also produce your own shell that wraps/encapsulates both your project's requirements
and some additional packages

nix-shell -E 'with import <nixpkgs> {}; runCommand "foo" { buildInputs=(import ./default.nix {}).buildInputs ++ [vim ripgrep]; } "" '

REPL

The nix repl is an essential tool, it will be your best friend while writing packages, overlays, etc.

nix repl  # start nix repl

Useful repl functions

builtins.attrNames     var      # list object attrs
builtins.functionArgs  fn       # list function arguments

:?                              # show repl commands
:t var                          # show type of 'var'
:s var                          # build deps of 'var', start nix-shell
:q                              # exit repl

Here's an example of a nix-repl session that imports the package from cwd
adds vim as requirement, then resolves a nix-shell for it.

nix-repl> pkgs = import <nixpkgs> {}     # bind nixpkgs to 'pkgs'
nix-repl> pkg = import ./default.nix {}  # invokes default.nix function, returns derivation
nix-repl> pkg-dev = pkgs.stdenv.mkDerivation { name="${pkg.name}-vim"; buildInputs=pkg.buildInputs ++ [ pkgs.vim ]; }
nix-repl> :s pkg-dev
:s foo