How to use nix-env

(In response to Stop using nix-env. before PR 163)

TL;DR: Use nix-env -riA nixpkgs.userPackages

nix-env's defaults are from an earlier era when there were many fewer packages. Nowadays, the defaults are bad; they don't work at all. So don't use the defaults:

-A (or --attr) looks up packages by attribute-name instead of derivation-name. This just should have been the default. Oops.

-r (or --remove-all) turns off nix-env's whole imperative user-profile-management feature. It has become unusable because it records the packages you want by derivation-name instead of attribute-name, which later forces nix-env --upgrade to evaluate a hundred thousand packages to do updates, which nowadays runs out of RAM and crashes. This flag directs nix-env to not bother with any history and just install only what you've specified on the command line right now.

But how can you ever have more than one package installed, then? Well, you define a new package that is the list of packages you'd like installed! This neatly side-steps all of nix-env's shortcomings: It lets you specify the list of packages you want, by attribute-name, in a text file that you can keep in version control. Unfortunately, it requires a tiny bit of boilerplate:

final: prev: {
  userPackages = final.buildEnv {
    name = "userPackages";
    paths = with final; [

      firefox
      libreoffice

    ];
  };
}

Put this in ~/.config/nixpkgs/overlays/userPackages.nix. In this example, we install firefox and libreoffice. You can use search.nixos.org/packages to look up packages or nixsearch.thekoppe.com to find packages by binary name (or on NixOS, just try to run the command in a terminal and it'll tell you the package name).

Then run nix-env -riA nixpkgs.userPackages to 'apply' your configuration. You're now using nix-env declaratively; this command is analogous to how nixos-rebuild 'applies' the OS configuration.

The same command is now also how you upgrade: After updating your channels (with nix-channel --update or pinch or whatever), just run nix-env -riA nixpkgs.userPackages again.

To support our command-line-shy friends, I made a super-simple GUI app to facilitate this usage.