Difference between installing via nix-env and configuration.nix?

I’m new to Nix and NixOS and have been installing new packages by adding them to environment.systemPackages in /etc/nixos/configuration.nix then rebuilding, which I thought was the same as using the package manager. Today I used nix-env -i hello and realized that it doesn’t put that package in the configuration.nix file.

What’s the difference between those installation methods? Is it like installing to /Applications vs ~/Applications on macOS?

packages listed in configuration.nix get added to a system profile. This also gives you the generations

no, nix-env is meant to give you a similar experience to other package managers, however, i manages it’s own gcroots (what derivations can be cleaned up), and even generations. Generally you should avoid using nix-env for package installation as it doesn’t leverage the power of nix.

Somewhat. In terms of user vs system installation, yes. However, nixos is different from normal FHS operating systems in that there’s a layer of indirection when you install something, so your PATH and other environment variables just reference the current “profile” or generation

$ echo $PATH
/run/wrappers/bin:/home/jon//.nix-profile/bin:/etc/profiles/per-user/jon/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/home/jon//.cargo/bin:/home/jon//.config/nixpkgs/bin

/home/jon//.cargo/bin:/home/jon//.config was added by me.

1 Like

This is marked as solution but tbh it makes me more confused. :smiley:

So nix-env is supposed to behave like a traditional package manager but then somehow throws out some of Nix’s advantages with the bath water?

I’m not sure what the gcroots and generations are. Could somebody explain, please?

Unfortunately, nix covers a lot of surface area. So I apologize if I didn’t clarify much :frowning:

You can take a look at https://nixos.org/nixos/nix-pills/ which goes from a fundamentals perspective and works its way up the nix stack.

EDIT: I also have some videos which cover different topics in nix https://www.youtube.com/channel/UC-cY3DcYladGdFQWIKL90SQ , unfortunately I haven’t made one on specifically nix-env yet