1<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-ad-hoc-packages">
2 <title>Ad-Hoc Package Management</title>
3 <para>
4 With the command <literal>nix-env</literal>, you can install and
5 uninstall packages from the command line. For instance, to install
6 Mozilla Thunderbird:
7 </para>
8 <programlisting>
9$ nix-env -iA nixos.thunderbird
10</programlisting>
11 <para>
12 If you invoke this as root, the package is installed in the Nix
13 profile <literal>/nix/var/nix/profiles/default</literal> and visible
14 to all users of the system; otherwise, the package ends up in
15 <literal>/nix/var/nix/profiles/per-user/username/profile</literal>
16 and is not visible to other users. The <literal>-A</literal> flag
17 specifies the package by its attribute name; without it, the package
18 is installed by matching against its package name (e.g.
19 <literal>thunderbird</literal>). The latter is slower because it
20 requires matching against all available Nix packages, and is
21 ambiguous if there are multiple matching packages.
22 </para>
23 <para>
24 Packages come from the NixOS channel. You typically upgrade a
25 package by updating to the latest version of the NixOS channel:
26 </para>
27 <programlisting>
28$ nix-channel --update nixos
29</programlisting>
30 <para>
31 and then running <literal>nix-env -i</literal> again. Other packages
32 in the profile are <emphasis>not</emphasis> affected; this is the
33 crucial difference with the declarative style of package management,
34 where running <literal>nixos-rebuild switch</literal> causes all
35 packages to be updated to their current versions in the NixOS
36 channel. You can however upgrade all packages for which there is a
37 newer version by doing:
38 </para>
39 <programlisting>
40$ nix-env -u '*'
41</programlisting>
42 <para>
43 A package can be uninstalled using the <literal>-e</literal> flag:
44 </para>
45 <programlisting>
46$ nix-env -e thunderbird
47</programlisting>
48 <para>
49 Finally, you can roll back an undesirable <literal>nix-env</literal>
50 action:
51 </para>
52 <programlisting>
53$ nix-env --rollback
54</programlisting>
55 <para>
56 <literal>nix-env</literal> has many more flags. For details, see the
57 nix-env(1) manpage or the Nix manual.
58 </para>
59</section>