Convert Figma logo to code with AI

LnL7 logonix-darwin

nix modules for darwin

2,742
428
2,742
282

Top Related Projects

17,805

Nix Packages collection & NixOS

Manage a user environment using Nix [maintainer=@rycee]

41,118

🍺 The missing package manager for macOS (or Linux)

Nix-enabled environment for your Android device. [maintainers=@t184256,@Gerschtli]

Quick Overview

Nix-darwin is a project that allows users to manage macOS configurations using the Nix package manager. It provides a declarative approach to system configuration, enabling users to define their entire macOS setup in a single file, including system preferences, installed applications, and services.

Pros

  • Declarative configuration: Easily manage and version control your entire macOS setup
  • Reproducibility: Quickly set up new machines or recover from system failures
  • Integration with Nix ecosystem: Access to a vast collection of packages and tools
  • Flexibility: Customize various aspects of macOS, including system preferences and services

Cons

  • Learning curve: Requires understanding of Nix language and concepts
  • Limited to macOS: Not applicable for other operating systems
  • Potential conflicts with manual system changes: May overwrite manual configurations
  • Incomplete coverage: Some macOS features may not be fully configurable through nix-darwin

Getting Started

  1. Install Nix package manager:
sh <(curl -L https://nixos.org/nix/install)
  1. Install nix-darwin:
nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer
./result/bin/darwin-installer
  1. Create a basic configuration file (~/.nixpkgs/darwin-configuration.nix):
{ config, pkgs, ... }:

{
  # List packages installed in system profile
  environment.systemPackages = with pkgs; [
    vim
    git
  ];

  # Auto upgrade nix package and the daemon service
  services.nix-daemon.enable = true;
  nix.package = pkgs.nix;

  # Create /etc/zshrc that loads the nix-darwin environment
  programs.zsh.enable = true;

  # Set Git as the default shell
  programs.zsh.enableCompletion = true;
  programs.zsh.enableBashCompletion = true;

  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;

  # Create /etc/bashrc that loads the nix-darwin environment
  programs.bash.enable = true;

  # Used for backwards compatibility, please read the changelog before changing.
  system.stateVersion = 4;
}
  1. Apply the configuration:
darwin-rebuild switch

Competitor Comparisons

17,805

Nix Packages collection & NixOS

Pros of nixpkgs

  • Larger community and more extensive package collection
  • Supports multiple operating systems, including Linux and macOS
  • More comprehensive documentation and resources

Cons of nixpkgs

  • Can be more complex to set up and configure for macOS-specific use cases
  • May include unnecessary packages and configurations for non-Linux systems

Code Comparison

nixpkgs:

{ config, pkgs, ... }:
{
  environment.systemPackages = with pkgs; [
    vim
    git
    firefox
  ];
}

nix-darwin:

{ config, pkgs, ... }:
{
  environment.systemPackages = with pkgs; [
    vim
    git
  ];
}

Key Differences

  • nix-darwin is specifically designed for macOS, offering tighter integration with macOS-specific features and services
  • nixpkgs provides a broader range of packages and configurations, but may require more customization for optimal macOS use
  • nix-darwin offers simpler setup and configuration for macOS users, while nixpkgs provides greater flexibility across multiple operating systems

Use Cases

  • Choose nix-darwin for a streamlined, macOS-focused Nix experience with easier setup and configuration
  • Opt for nixpkgs when working across multiple operating systems or requiring a wider range of packages and configurations

Community and Support

  • nixpkgs has a larger and more active community, resulting in more frequent updates and broader package support
  • nix-darwin has a smaller but dedicated community focused on macOS-specific issues and improvements

Manage a user environment using Nix [maintainer=@rycee]

Pros of home-manager

  • Cross-platform support (Linux and macOS)
  • More extensive module ecosystem
  • Manages user-specific configurations

Cons of home-manager

  • Requires separate installation and setup
  • Less integrated with macOS-specific features
  • May have a steeper learning curve for beginners

Code Comparison

nix-darwin:

{ config, pkgs, ... }:
{
  system.defaults.dock.autohide = true;
  environment.systemPackages = [ pkgs.vim ];
}

home-manager:

{ config, pkgs, ... }:
{
  programs.vim.enable = true;
  home.packages = [ pkgs.ripgrep ];
}

Key Differences

  • nix-darwin focuses on system-wide macOS configurations
  • home-manager manages user-specific dotfiles and packages
  • nix-darwin integrates better with macOS-specific features
  • home-manager offers more flexibility for cross-platform setups

Use Cases

  • Use nix-darwin for macOS system configurations
  • Use home-manager for user-specific dotfiles and package management
  • Combine both for a comprehensive macOS setup

Community and Support

  • Both projects have active communities
  • home-manager has a larger user base and more contributors
  • nix-darwin is more specialized for macOS users
41,118

🍺 The missing package manager for macOS (or Linux)

Pros of Homebrew

  • Larger package ecosystem with more readily available formulae
  • Simpler installation process and easier learning curve for beginners
  • Better integration with macOS native applications and workflows

Cons of Homebrew

  • Less reproducible and declarative system configuration
  • Potential for conflicts between system and Homebrew-installed packages
  • Limited ability to manage system-level configurations and services

Code Comparison

nix-darwin configuration example:

{ config, pkgs, ... }:
{
  environment.systemPackages = [ pkgs.vim pkgs.git ];
  services.nix-daemon.enable = true;
}

Homebrew usage example:

brew install vim git
brew services start postgresql

While nix-darwin allows for declarative system configuration, Homebrew focuses on package management through command-line operations. nix-darwin provides a more comprehensive approach to system management, including services and configurations, whereas Homebrew primarily deals with software installation and updates.

Nix-enabled environment for your Android device. [maintainers=@t184256,@Gerschtli]

Pros of nix-on-droid

  • Designed specifically for Android devices, offering a Nix environment on mobile platforms
  • Provides a terminal-based interface, making it suitable for command-line enthusiasts
  • Allows for customization of the Android environment using Nix expressions

Cons of nix-on-droid

  • Limited integration with Android system compared to nix-darwin's integration with macOS
  • Smaller community and fewer available packages compared to nix-darwin
  • May require more manual configuration for certain Android-specific features

Code Comparison

nix-on-droid configuration example:

{ pkgs, ... }:

{
  environment.packages = with pkgs; [ vim git ];
  nix.binaryCaches = [ "https://cache.nixos.org" ];
}

nix-darwin configuration example:

{ config, pkgs, ... }:

{
  environment.systemPackages = with pkgs; [ vim git ];
  services.nix-daemon.enable = true;
}

Both configurations demonstrate package installation and basic system setup, but nix-darwin includes macOS-specific features like enabling the Nix daemon service.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

logo

nix-darwin

Test

Nix modules for darwin, /etc/nixos/configuration.nix for macOS.

This project aims to bring the convenience of a declarative system approach to macOS. nix-darwin is built up around Nixpkgs, quite similar to NixOS.

Installing

To install nix-darwin, a working installation of Nix is required.

If you wish to use nix-darwin with flakes, please refer to the flakes section.

nix-build https://github.com/LnL7/nix-darwin/archive/master.tar.gz -A installer
./result/bin/darwin-installer

NOTE: the system activation scripts don't overwrite existing etc files, so files like /etc/bashrc and /etc/zshrc won't be updated by default. If you didn't use the installer or skipped some of the options you'll have to take care of this yourself. Either modify the existing file to source/import the one from /etc/static or remove it. Some examples:

  • mv /etc/bashrc /etc/bashrc.before-nix-darwin
  • echo 'if test -e /etc/static/bashrc; then . /etc/static/bashrc; fi' | sudo tee -a /etc/bashrc

Updating

The installer will configure a channel for this repository.

nix-channel --update darwin
darwin-rebuild changelog

NOTE: If you are using Nix as a daemon service the channel for that will be owned by root. Use sudo -i nix-channel --update darwin instead.

Uninstalling

To run the latest version of the uninstaller, you can run the following command:

nix --extra-experimental-features "nix-command flakes" run nix-darwin#darwin-uninstaller

If that command doesn't work for you, you can try the locally installed uninstaller:

darwin-uninstaller

Example configuration

Configuration lives in ~/.nixpkgs/darwin-configuration.nix. Check out modules/examples for some example configurations.

{ pkgs, ... }:
{
  # List packages installed in system profile. To search by name, run:
  # $ nix-env -qaP | grep wget
  environment.systemPackages =
    [ pkgs.vim
    ];

  # Auto upgrade nix package and the daemon service.
  services.nix-daemon.enable = true;
  nix.package = pkgs.nix;
}

Flakes

nix-darwin aims for both non-flake and flake configurations to be well supported despite flakes being an experimental feature in Nix.

Step 1. Creating flake.nix

Getting started from scratch

If you don't have an existing configuration.nix, you can run the following commands to generate a basic flake.nix inside ~/.config/nix-darwin:

mkdir -p ~/.config/nix-darwin
cd ~/.config/nix-darwin
nix flake init -t nix-darwin
sed -i '' "s/simple/$(scutil --get LocalHostName)/" flake.nix

Make sure to change nixpkgs.hostPlatform to aarch64-darwin if you are using Apple Silicon.

Migrating from an existing configuration.nix

Add the following to flake.nix in the same folder as configuration.nix:

{
  description = "John's darwin system";

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

  outputs = inputs@{ self, nix-darwin, nixpkgs }: {
    darwinConfigurations."Johns-MacBook" = nix-darwin.lib.darwinSystem {
      modules = [ ./configuration.nix ];
    };
  };
}

Make sure to replace Johns-MacBook with your hostname which you can find by running scutil --get LocalHostName.

Make sure to set nixpkgs.hostPlatform in your configuration.nix to either x86_64-darwin (Intel) or aarch64-darwin (Apple Silicon).

Step 2. Installing nix-darwin

Instead of using darwin-installer, you can just run darwin-rebuild switch to install nix-darwin. As darwin-rebuild won't be installed in your PATH yet, you can use the following command:

nix run nix-darwin -- switch --flake ~/.config/nix-darwin

Step 3. Using nix-darwin

After installing, you can run darwin-rebuild to apply changes to your system:

darwin-rebuild switch --flake ~/.config/nix-darwin

Using flake inputs

Inputs from the flake can also be passed into darwinSystem. These inputs are then accessible as an argument inputs, similar to pkgs and lib, inside the configuration.

# in flake.nix
nix-darwin.lib.darwinSystem {
  modules = [ ./configuration.nix ];
  specialArgs = { inherit inputs; };
}
# in configuration.nix
{ pkgs, lib, inputs }:
# inputs.self, inputs.nix-darwin, and inputs.nixpkgs can be accessed here

Documentation

Reference documentation of all the options is available here. This can also be accessed locally using man 5 configuration.nix.

darwin-help will open a HTML version of the manpage in the default browser.

Furthermore there's darwin-option to introspect the settings of a system and its available options.

NOTE: darwin-option is only available to non-flake installations.

$ darwin-option services.activate-system.enable
Value:
true

Default:
false

Example:
no example

Description:
Whether to activate system at boot time.

There's also a small wiki https://github.com/LnL7/nix-darwin/wiki about specific topics, like macOS upgrades.

Tests

There are basic tests that run sanity checks for some of the modules, you can run them like this:

# run all tests
nix-build release.nix -A tests
# or just a subset
nix-build release.nix -A tests.environment-path

Contributing

Let's make Nix on macOS awesome!

Don't hesitate to contribute modules or open an issue.

To build your configuration with local changes you can run this. This flag can also be used to override darwin-config or nixpkgs, for more information on the -I flag look at the nix-build manpage.

darwin-rebuild switch -I darwin=.

If you're adding a module, please add yourself to meta.maintainers, for example

  meta.maintainers = [
    lib.maintainers.alice or "alice"
  ];

  options.services.alicebot = # ...

The or operator takes care of graceful degradation when lib from Nixpkgs goes out of sync.

Also feel free to contact me if you have questions,

  • Matrix - @daiderd:matrix.org, you can find me in #macos:nixos.org
  • @LnL7 on twitter