The Mac has a number of third-party package deal managers for putting in further software program. Here is how one can use the Nix package deal supervisor with macOS.
What’s Nix?
Nix is a package deal supervisor that gives container-based improvement environments throughout platforms. Much like container programs resembling Docker and Kubernetes, Nix permits you to set up configured improvement environments throughout machines and networks with much less configuration and bother.
Historically on the Mac, customers used both MacPorts or Homebrew to put in third-party packages. Each programs have advantages and downsides.
Homebrew has been common within the Mac neighborhood due to its ease of use and set up: packages may be put in from the Terminal app in macOS with only a few instructions, and updates are computerized and simply as simple.
Nonetheless, each Homebrew and MacPorts lack portability throughout machines. You may write Terminal (shell) scripts to put in Homebrew or MacPorts packages throughout programs, however script upkeep can turn out to be advanced and infrequently it is tough to write down scripts which can be conscious of all doable machine configurations.
Nix solves this by containerizing improvement environments and making them extra transportable throughout programs.
The tradeoff price you pay with Nix is a barely extra advanced configuration – and a steeper studying curve.
Nix ecosystem
NixOS from the NixOS Basis is definitely a number of issues: it is a Linux distribution that makes use of the Nix package deal supervisor. Nixpkgs is a group of over 80,000 packages that may be put in utilizing the Nix package deal supervisor.
There are three separate manuals for Nix: the Nix Guide, the Nixpkgs Guide, and the NixOS Guide.
There may be additionally a Nix Reference Guide.
A community-maintained unofficial NixOS Wiki is accessible and a Nix improvement website is situated at nix.dev. The NixOS Wiki has information on the package deal supervisor, the Nix language, Nixpkgs, and NixOS itself.
There may be additionally a Nix Language you need to use to create and replica components of packages for brand new installations.
You will most likely wish to learn your complete Nix Guide at the very least earlier than you start putting in and utilizing Nix.
Definitions
There are a number of new phrases you will want to grasp to make use of Nix.
Nix relies on declarative programming – creating packages based mostly on describing what a chunk of software program is meant to do fairly than how one can do it. Lengthy story brief, declarative programming tries to keep away from software program based mostly on state and altering state to realize some end result.
Expressions. Nix expressions are principally subcomponents of installable software program which can be predefined for later retrieval and set up. The concept with expressions is the part is already outlined in order that when it’s put in, it really works the identical method throughout all programs.
Every expression additionally has a search path.
Channels. A Nix channel is a particular path to a number of predefined Nix expressions that may be remotely retrieved and put in. There’s a record of official channels at channels.nixos.org.
Most channels have variations. A channel will also be subscribed or unsubscribed.
Every channel has downloadable content material which will get put in when the channel is put in.
Retailer. In contrast to Homebrew and UNIX itself, Nix does not set up packages into the usual UNIX directories resembling /usr, /bin, /sbin, or /choose.
As a substitute, it creates its personal retailer – a particular listing and quantity used to retailer all put in packages. Through the use of a retailer, Nix packages are rather more transportable as a result of your complete retailer may be moved as a unit.
Nix does set up a configuration file into /and so forth/nixos/configuration.nix
, which is often invisible except you activate macOS Finder’s hidden information characteristic.
Flakes are an experimental Nix characteristic that are information used to outline and management the dependencies amongst packages. Though common, Nix flakes are nonetheless experimental and missing robust documentation so use them at your personal threat.
Rubbish assortment. As in some programming environments, if you take away a Nix package deal it does not really get uninstalled instantly. To try this you run the Nix Rubbish Collector.
Consider a rubbish collector as a course of that runs silently within the background, one which cleans up any unused or stale parts which can be now not wanted.
Many fashionable programs use rubbish collectors – together with the Java VM, and the Swift and Goal-C runtimes from Apple.
You may invoke the Nix rubbish collector any time after set up in Terminal with:
The Nix set up contains a number of command-line instruments (CLI) you possibly can run from the Terminal app. These embrace:
nix-env
– used to govern Nix consumer environmentsnix-build
– used to construct Nix expressionsnix-shell
– an interactive command atmosphere based mostly on a Nix expressionnix-store
– manipulate or question the Nix retailer
All these instructions and extra are detailed within the Nix Guide.
A Nix consumer atmosphere is solely a set of software program and instruments put in by Nix.
Putting in Nix
Nix usually installs to /nix
however since macOS Catalina, the basis of the macOS Startup Disk is read-only.
As a substitute, on post-Catalina programs, Nix will create a brand new quantity to carry the Nix retailer and mount it as a quantity within the hidden macOS folder Volumes
on the root of the Startup Disk.
In the event you do have admin entry, the installer will immediate you for sudo (superuser) permission if you run it with a purpose to create the brand new quantity.
To run the interactive Nix installer in Terminal, sort:
curl -L https://nixos.org/nix/set up | sh
When the installer runs, it offers you a brief message about what it’s about to do and the place issues might be put in. It additionally creates a brand new UNIX consumer and UUID for the consumer – that are used throughout Nix container and package deal builds.
The Nix installer shops a reference to the Nix retailer quantity in /and so forth/fstab
.
Word the Nix retailer quantity mounts, however not on the Finder’s desktop – you will not see it in your Mac’s Desktop but it surely’s nonetheless there nonetheless.
As a substitute, Nix mounts it on the root of your Startup Disk in a listing named “Nix Retailer”. Within this listing, there is a retailer and a var folder.
The retailer folder incorporates all of the packages and channels NIX downloaded and put in, the var folder incorporates a log folder, and the nix folder which incorporates the DB, profiles, a connection to the background daemon, the consumer swimming pools and different components.
fstab
is the File System Desk file which tells UNIX the place all of the filesystems and their volumes dwell.
If you end up able to proceed with the installer, enter an admin password in Terminal and press Return.
You will see the Nix installer run and because it does it’ll describe what it’s creating.
If the Nix put in has sudo entry, it’ll additionally attempt to add some configuration choices to the hidden consumer profile information in your Mac’s startup disk within the consumer and and so forth
folders, particularly:
/and so forth/bashrc /and so forth/profile.d/nix.sh /and so forth/zshrc /and so forth/bash.bashrc /and so forth/zsh/zshrc
These profile information get loaded by macOS if you log in and so they include atmosphere variables and paths to put in software program.
Lastly, the Nix installer units up a background course of, or daemon named nix-daemon
which manages the Nix retailer within the background silently.
As soon as the installer finishes, you will both have to give up Terminal and re-open it, or else open a brand new Terminal shell to ensure that Terminal to load all the brand new Nix configurations.
After the installer finishes you possibly can initialize the default Nix packages and take a look at the Nix set up in Terminal with:
nix-shell -p nix-info --run "nix-info -m"
It’s best to see a message about fetching paths after which a listing of all of the packages put in within the native Nix retailer.
On the backside of the message, it’s best to see the present Nix and Darwin model quantity, channel identify, and the trail to the packages profile file.
You can even manually set up any current Nix package deal in Terminal by utilizing the nix-env --install
command adopted by the package deal identify. For instance:
nix-env --install firefox
Package deal uninstallation is similar, besides you employ the --uninstall
flag to the nix-env
command
Extra sources
There are a number of good third-party Nix websites out there – make sure you examine them out.
Zero-to-nix.com is a information to utilizing Nix and features a Fast Begin and a Ideas part which provides a great overview of every a part of Nix and the way they match collectively.
Determinate.programs is a Nix-centric website that incorporates many Nix tutorials in addition to a standalone Nix installer in case you do not wish to use the Terminal-based set up.
The Nix and Flakes E-book from Ryan Yin is a web-based tutorial and GitHub repo for inexperienced persons filled with helpful Nix-related information.
The nix-darwin repo incorporates all of the modules, packages, and documentation for the Darwin (macOS) port of the Nix package deal supervisor.
Nixpacks is a utility that may take any listing of information and containerize it for reuse.
Nix professionals and cons
When you get previous the training curve, Nix may be fairly efficient and helpful. The power to configure and reinstall transportable containers throughout programs could be very highly effective.
You may configure any set of environments and instruments, after which package deal it for redeployment on most programs.
The power to create your personal customized Nix packages and containers which may then be reused saves numerous time each time you must configure a brand new machine.
The primary draw back of Nix as a package deal supervisor is elevated complexity and its studying curve. There additionally at present is not any Home windows model.
Homebrew continues to be less complicated than Nix, however apart from writing your personal customized shell scripts for set up, Homebrew lacks portability. Attempting to write down customized set up shell scripts for each doable system is impractical.
When you be taught and grasp Nix, it’ll turn out to be a strong instrument in your improvement arsenal.