Computer

Homebrew and nix-darwin: the package manager that was missing on macOS

Homebrew and nix-darwin: the package manager that was missing on macOS

The macOS operating system itself does not integrate a package manager, that is, a useful tool for installing and uninstalling applications, even in an automated manner and from the command line. To manage macOS programs, Apple only makes its famous one available App Store. With the specific intention of offering Apple users a package manager worthy of the name, Max Howell thought about it in 2009 by presenting the idea of Homebrew.

The software bridges the gap between the programs available by default on macOS and those that each user may want to install on his system. The authors of Homebrew summarize the utility’s “mission” well: “If you have ever wanted to install a tool like wget or a new application like Firefox, but find it tedious to search and download the installation files yourself, Homebrew is the perfect solution for you“.

Homebrew: what it is and how it works

Homebrew is a package manager that makes it easy to install and manage additional software. It works through the concept of “formulas”, script Ruby which describe how to install a certain software and its dependencies.

When you want to install a new program via Homebrew, you just need to use its terminal command brew followed by the name of the package you want to install. Homebrew takes care of downloading the desired software, along with all its dependencies, and installing it correctly on your system.

Distributed as a program open sourceHomebrew benefits from a large community of developers who maintain the project, update it and develop it.

How to install Homebrew

L’installation of Homebrew is a very simple process. Simply open a terminal window on macOS then paste the following command into it:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

The installation script describes each step. Upon completion, each user has access to a vast software ecosystem ready to be managed with a single command. For example, if you wanted to install the legendary wgetutility for downloading files from the Internet, you can proceed with the following command:

brew install wget

Homebrew installs the package in its directory and creates a symbolic link along the way /opt/homebrewkeeping your system clean and organized.

Homebrew Cask: to install macOS applications with ease

In addition to managing the software via formulas, the application also integrates Homebrew Caskwhich allows you to install macOS applications, fonts, and plugins similar to how you install packages via the command brew.

Homebrew Cask is specifically designed to simplify the installation of GUI applications, such as web browsers, text editors, productivity apps, and so on. The idea is to download and install binaries pre-compiled applications of your interest. It is useful when you want to quickly install applications without having to manually navigate to developer websites. For example, to install Firefox all you need is a simple command:

brew install --cask firefox

More information about Homebrew is available on the project’s official website.

nix-darwin: what it is and what are the differences compared to Homebrew

Although enormous steps forward have also been made on Windows (remember, for example, the Scoop package manager and Microsoft Winget), it is GNU/Linux that, historically, has set the example with regards to best package managers.

nix it is one of the best ever for several reasons. First, it offers a sort of shared memory long-term. There is no longer any need to remember the syntax used months ago to configure a machine or which commands were used at two in the morning to perfectly configure a work environment. There synchronization of data is an indisputable strong point.

It is a valuable tool that ensures a complete vision on your software environment. All configuration files, editor themes and extensions, and programs launched from the command line are defined in a single repository.

Thanks to nix, it is also possible to reduce manual work: users can automate the update process and configuration management, thus reducing the workload and time needed to keep all managed systems “healthy”.

Using a tool like nix-darwin you can manage virtually any system setting on the macOS platform. The environment in use thus becomes more easily manageable and transparent.

What are Darwin and nix-darwin

Darwin it is the open source operating system POSIX-compliant released by Apple in 2000. It is composed of code developed by Apple, derived from NeXTSTEP, BSD and other free software projects. The “core” of macOS and iOS sees Darwin as one of the fundamental pillars: we talked about it in the article on the differences between macOS and Linux.

nix-darwin is a set of modules for managing the configuration of macOS. Users have access to a centralized nix platform that allows access to every “fold” of the system configuration in use.

nix takes a declarative approach to package management, meaning the user simply indicates which packages they want and Nix takes care of resolving dependencies and proceeding with the installation in a consistent manner. Additionally, nix-darwin creates a isolated environment for each installed package, allowing you to avoid interference or conflicts between packages.

Users can also appreciate the creation of reproducible environments, the possible rollback (restore) of packages and the centralized repository.

nix-darwin: The most powerful approach to managing software packages with macOS

Although Homebrew remains a real point of reference, nix-darwin today offers the best way to manage packages and system configurations on macOS.

As Davis Haupt points out, interfacing with the various parts of the nix ecosystem is not exactly simple for beginners. To get started use nix on macOSHaupt suggests using the installer of Determinate Systems. In fact, it integrates a command line utility and in November 2023 an effective installation procedure in graphical format was added.

To configure nix-darwin on your macOS machine, Haupt offers a flake that is, an element that makes the definition, sharing and use of packages, configurations and environments with nix easier and more transparent.

In the flake proposed in correspondence with the paragraph Setting up nix-darwinthe occurrences of $USER e $HOSTNAME they must be replaced, respectively, with the username in use and with the name of the macOS system. Below we propose the contents of the flake file created by Haupt (the file must be saved as ~/.config/nix/flake.nix):

# ~/.config/nix/flake.nix

{
  description = "Mia configurazione di sistema";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    nix-darwin = {
        url = "github:LnL7/nix-darwin";
        inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs@{ self, nix-darwin, nixpkgs }:
  let
    configuration = {pkgs, ... }: {

        services.nix-daemon.enable = true;
        # Riga necessaria per usare i flake sul sistema
        nix.settings.experimental-features = "nix-command flakes";

        system.configurationRevision = self.rev or self.dirtyRev or null;

        # Utilizzato per garantire retrocompatibilita'
        # Vedere `darwin-rebuild changelog`
        system.stateVersion = 4;

        # La piattaforma sulla quale e' usata la configurazione.
        # Su un sistema Intel system, inserire la stringa "x86_64-darwin"
        nixpkgs.hostPlatform = "aarch64-darwin";

        # Specificare l'utente che esegue `nix-darwin`.
        users.users.$USER = {
            name = "$USER";
            home = "/Users/$USER";
        };

        # Crea il percorso /etc/zshrc per l'ambiente nix-darwin
        programs.zsh.enable = true;

        environment.systemPackages = [ ];
    };
  in
  {
    darwinConfigurations."$HOSTNAME" = nix-darwin.lib.darwinSystem {
      modules = [
         configuration
      ];
    };
  };
}

Enabling nix-darwin configuration on macOS

One of the peculiarities when using nix flakes is that all the files recalled must necessarily be controlled through a repository containing source code. That is…

Leave a Reply

Your email address will not be published. Required fields are marked *