The Mac has a number of third-party package deal managers for putting in further software program. This is methods to use the Nix package deal supervisor with macOS.
What’s Nix?
Nix is a package deal supervisor that gives container-based growth environments throughout platforms. Much like container techniques resembling Docker and Kubernetes, Nix lets you set up configured growth 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 techniques have advantages and downsides.
Homebrew has been widespread within the Mac neighborhood due to its ease of use and set up: packages could be put in from the Terminal app in macOS with just some instructions, and updates are automated and simply as straightforward.
Nevertheless, each Homebrew and MacPorts lack portability throughout machines. You’ll be able to write Terminal (shell) scripts to put in Homebrew or MacPorts packages throughout techniques, however script upkeep can turn out to be advanced and sometimes it is troublesome to jot down scripts which can be conscious of all attainable machine configurations.
Nix solves this by containerizing growth environments and making them extra transportable throughout techniques.
The tradeoff value 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 set 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’s additionally a Nix Reference Guide.
A community-maintained unofficial NixOS Wiki is offered and a Nix growth website is positioned at nix.dev. The NixOS Wiki has data on the package deal supervisor, the Nix language, Nixpkgs, and NixOS itself.
There’s additionally a Nix Language you need to use to create and replica elements of packages for brand new installations.
You may most likely need to learn the whole Nix Guide not less than earlier than you start putting in and utilizing Nix.
Definitions
There are a couple of new phrases you may want to know to make use of Nix.
Nix is predicated on declarative programming – creating applications primarily based on describing what a chunk of software program is meant to do moderately than methods to do it. Lengthy story quick, declarative programming tries to keep away from software program primarily based on state and altering state to attain some consequence.
Expressions. Nix expressions are mainly subcomponents of installable software program which can be predefined for later retrieval and set up. The concept with expressions is the element is already outlined in order that when it’s put in, it really works the identical method throughout all techniques.
Every expression additionally has a search path.
Channels. A Nix channel is a selected 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 can be subscribed or unsubscribed.
Every channel has downloadable content material which will get put in when the channel is put in.
Retailer. Not like Homebrew and UNIX itself, Nix would not set up packages into the usual UNIX directories resembling /usr, /bin, /sbin, or /decide.
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 way more transportable as a result of the whole retailer could be moved as a unit.
Nix does set up a configuration file into /and many others/nixos/configuration.nix
, which is generally invisible except you activate macOS Finder’s hidden information function.
Flakes are an experimental Nix function that are information used to outline and management the dependencies amongst packages. Though widespread, Nix flakes are nonetheless experimental and missing robust documentation so use them at your individual threat.
Rubbish assortment. As in some programming environments, once you take away a Nix package deal it would not really get uninstalled immediately. To do 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 trendy techniques use rubbish collectors – together with the Java VM, and the Swift and Goal-C runtimes from Apple.
You’ll be able to 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 control Nix consumer environmentsnix-build
– used to construct Nix expressionsnix-shell
– an interactive command setting primarily based 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 setting 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 foundation of the macOS Startup Disk is read-only.
As a substitute, on post-Catalina techniques, 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 case you do have admin entry, the installer will immediate you for sudo (superuser) permission once you run it in an effort to create the brand new quantity.
To run the interactive Nix installer in Terminal, kind:
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 many others/fstab
.
Word the Nix retailer quantity mounts, however not on the Finder’s desktop – you will not see it in your Mac’s Desktop nevertheless it’s nonetheless there nonetheless.
As a substitute, Nix mounts it on the root of your Startup Disk in a listing named “Nix Retailer”. Inside 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 elements.
fstab
is the File System Desk file which tells UNIX the place all of the filesystems and their volumes stay.
When you’re able to proceed with the installer, enter an admin password in Terminal and press Return.
You may see the Nix installer run and because it does it would describe what it’s creating.
If the Nix put in has sudo entry, it would additionally attempt to add some configuration choices to the hidden consumer profile information in your Mac’s startup disk within the consumer and and many others
folders, particularly:
/and many others/bashrc /and many others/profile.d/nix.sh /and many others/zshrc /and many others/bash.bashrc /and many others/zsh/zshrc
These profile information get loaded by macOS once you log in and so they include setting 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 may both must 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"
You must 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 title, and the trail to the packages profile file.
You may as well manually set up any present Nix package deal in Terminal through the use of the nix-env --install
command adopted by the package deal title. For instance:
nix-env --install firefox
Bundle uninstallation is identical, besides you utilize the --uninstall
flag to the nix-env
command
Further assets
There are a number of good third-party Nix websites obtainable – 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 supplies a great overview of every a part of Nix and the way they match collectively.
Determinate.techniques is a Nix-centric website that incorporates many Nix tutorials in addition to a standalone Nix installer in case you do not need to use the Terminal-based set up.
The Nix and Flakes Ebook from Ryan Yin is a web based tutorial and GitHub repo for newcomers filled with helpful Nix-related data.
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 could be fairly efficient and helpful. The flexibility to configure and reinstall transportable containers throughout techniques could be very highly effective.
You’ll be able to configure any set of environments and instruments, after which package deal it for redeployment on most techniques.
The flexibility to create your individual customized Nix packages and containers which might then be reused saves a number of time at any time when you should configure a brand new machine.
The principle 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 easier than Nix, however apart from writing your individual customized shell scripts for set up, Homebrew lacks portability. Making an attempt to jot down customized set up shell scripts for each attainable system is impractical.
When you be taught and grasp Nix, it would turn out to be a strong software in your growth arsenal.