More actions
This article is a stub. Please help expand it.
Nix is a package manager which is used on nest to allow users to install packages without the need of apt
, which requires sudo (Superuser Do) permissions. It's normally used to make reproducible developer and production environments with just one file! What is great about nix, compared to pacman
(Arch), or apt
(Debian/Ubuntu), is that you can have multiple versions of a package installed on the nest server because it's on the user-level. This means one user could have Node.js 18, another 20, etc.
Nix Environment (persistent)
You can permanently install packages using nix! Just like using apt
, packages will stay installed on the system even if you reboot or log off. Unlike apt, the packages are only installed for your user account, meaning sudo
is not required. You can run nix profile install nixpkgs#<package>
to install packages.
If you want to remove a package, you can run nix profile remove <package>
The name of package will usually be the same as when you installed it (excluding the nixpkgs#
part) but you can always check installed packages and their names with nix profile list
Nix shells (temporary)
Let's talk about nix shells! You can spawn a new shell with the packages so you can try out packages, and when you close out the shell, all of those packages will be gone! For example, you can run nix shell nixpkgs#<package>
(replacing "package" with the packages you want) to spin up a new shell with those packages.
Once you're done, exit out using the exit
command.
Run a single command
Sometimes, you just need a package in order to run a single command. Instead of using nix shell
, resets your working directory, you can use nix run
to run a command in your current environment. Use nix run nixpkgs#<package>
to run the command associated with that package. If you need to pass command line options to the command, place two dashes after the package name, such as nix run nixpkgs#<package> -- <options>
Searching for a package
You can also search for the package you want online or by running nix search nixpkgs <package>
. It appears to use a lot of memory running it from the command line so it's recommended that you use the online version instead.
Advanced: Nix flakes
While nix profile
and nix shell
are powerful tools, they do have some drawbacks. as an example, lets say you have two projects, one using Node.js 20, and the other using Node.js 17. You can't install both Node.js versions with nix profile
because they have conflicting npm
commands, and typing nix shell nixpkgs#nodejs_20
can get tedious if you have to do it every time you want to work on the project. It can also be annoying to install the dependencies for your projects on both your personal computer and nest, especially when using git
to sync your project to nest.
What's the solution? Nix flakes! Flakes allow you to use code to specify the packages you need, meaning they can be managed with git
to keep your dependencies in sync, and avoiding the hassle of typing out a long nix shell
command. Additionally, your dependencies are locked to a specific version until you chose to update them ensuring that updates won't unknowingly break your code.