Nix Commands

nix-channel

Interacts with channels (Nix's equivalent to a repository: URL to a set of Nix expressions).

  • --list lists added channels,
  • --add <url> [name] adds a channel,
  • --update [name...] downloads an updated copy of added channels,
  • --remove <name> removes a channel,
  • --rollback reverts the most recent --update.

nix-collect-garbage

Deletes all Nix store paths not currently being used.

Additionally, can delete old generations of user profiles:

  • {--delete-old | -d} deletes all non-current generations,
  • --delete-older-than <period> deletes all generations older than the period.

nix-env

Interacts with profiles, normally user environments (sets of packages available to a given user).

To specify a user other than yourself, you can use {--profile | -p} /nix/var/nix/profiles/per-user/<username>. To interact with the system profile (in order to use the generations-related actions), you can even specify /nix/var/nix/profiles/system.

For commands accepting packages as arguments, package names are provided as regular expressions, optionally followed by a dash and a version, e.g. hello-2.10. If the option -A is provided, attribute names as in pkgs.<name> are used instead.

  • {--query | -q} <name...> queries installed packages (pass -a to query available but not installed packages),
  • {--install | -i} <name...> installs packages,
  • {--uninstall | -e} <name...> uninstalls packages,
  • --set <name> uninstalls everything and installs just the specified package,
  • {--upgrade | -u} [name...] upgrades packages (all installed packages if none specified),
  • --list-generations lists generations,
  • {--switch-generation | -G} <generation> switches to the specified generation,
  • --delete-generations <generation...> deletes generations (specify a generation, an age like "30d" to select all older generations, a count like "+5" to keep all but the most recent 5 generations, or "old" to delete all but the currently active generation),
  • --rollback rolls back to the previous generation.

nix-prefetch-url

Prints the hash of the file downloaded from the specified URL. Useful when writing a package with a fetchurl input.

nix-shell

Starts an interactive shell after building the dependencies of a derivation. If the path to a derivation is not specified, shell.nix or default.nix (in order of preference) in the current directory is used instead.

# shell.nix

{ pkgs ? import <nixpkgs> {} }: pkgs.mkShell {
  inputsFrom = with pkgs; [ hello ];
  buildInputs = with pkgs; [ python3 ];
  shellHook = ''
    python
  '';
}

This is an example shell.nix that uses the special derivation function mkShell. If you run nix-shell in the same directory, you will enter a shell with python3 and all the inputs needed to build hello, and the shell will immediately start run the python interpreter.

Instead of specifying a derivation, you can also use nix-shell -p to specify a list of packages you want in the environment. If you just wanted the python interpreter, you could use nix-shell -p python3, or even nix-shell -p python3 --run python to enter the interpreter automatically.

see also Nix Manual: Part III. Package Management