Skip to content

lukejcollins/nix-multi-system-flake

Repository files navigation

nix-darwin macOS nixos

Multi-System Configuration with Nix

This repository provides a unified configuration setup for both NixOS and macOS (via nix-darwin), catering to both personal and work environments. It leverages the power of Nix and flakes for a declarative and reproducible system configuration.

image

An indication of the appearance of the nix-darwin build once properly deployed

What are NixOS and nix-darwin?

  • NixOS: A Linux distribution built on top of the Nix package manager, designed for declarative configuration and reliable system upgrades.
  • nix-darwin: A tool for managing macOS configuration using the Nix package manager, similar to how NixOS configurations are managed.

How the Configuration Works

The configuration is hierarchical, with root-level configuration files (home.nix) that set up general settings and package installations that are OS agnostic. Each system type (baremetal and virtual) then has its own directory with specific configurations for personal and work environments where relevant.

Root Configuration

The root home.nix includes settings and packages common to all systems. This is where you define the core configuration, such as allowed packages, basic system settings, and shared services. The intention is to shift more and more packages up to here where possible over time.

NixOS Configuration

Under the nixos directory, there are subdirectories for personal and work configurations. Each of these contains configuration.nix and home.nix files that include device-specific settings and services, such as the bootloader configuration, hostname, and additional packages. The root of the nixos directory contains the device agnostic configuration for NixOS.

macOS Configuration

Similarly, under the darwin directory, there are subdirectories for personal and work configurations. Each of these contains configuration.nix and home.nix files tailored for macOS, managing services like yabai (tiling window manager) and other macOS-specific settings. The root of the darwin directory contains the device agnostic configuration for macOS.

Virtual Configuration

Under the virtual directory, there are currently no subdirectories for personal and work configurations. This structure only contains home.nix, as there are no plans to use NixOS for any virtual deployments.

Prerequisites

Before you begin, ensure you have the following:

  1. For macOS:

    • A macOS device.
    • nix-darwin installed on macOS.
  2. For NixOS:

    • A running NixOS installation.
  3. For Virtual:

    • A virtual machine running a Linux distro.

Step 1: Clone the Repository

Clone this repository to your device:

git clone /~https://github.com/lukejcollins/nix-darwin-build

Step 2: Update Username

Update the username references in your configuration files to match your device.

flake.nix

Update the device-specific details:

  modules = [
    # ...
    {
      users.users."your.username".home = "/Users/your.username";
    }
    # ...
  ];

Replace "your.username" with your username. Ensure all paths and configurations specific to your device are updated accordingly. There may be subfolders and subfiles that also require username adjustments.

Step 3: Deploy Your Configuration

Deploy the configuration to your device. Follow the instructions from the nix-darwin repository or NixOS manual for detailed steps. I have also included some aliases in .zshrc to make this as easy as possible, referenced below.

Step 4: Additional Configuration and Advice (Optional)

The NixOS configuration is mostly ready to go out of the box. The nix-darwin configuration, due to the nature of the platform, will require a little more massaging to get going. Here's a starter for ten:

  • Raycast: Use Raycast to replace macOS Spotlight for better integration with nix-darwin applications. Raycast is included in the default configuration. Follow these instructions to set it up.

  • Menu Bar: Set the macOS menu bar to hide automatically to avoid interference with Simple Bar. Follow this guide.

  • Yabai: Configure Yabai (tiling window manager) in /nix-darwin-build/dotfiles/yabai/yabairc. Ensure paths and settings are updated to match your system.

    # Enable Yabai
    yabai = {
      enable = true;
      package = pkgs.yabai;
      extraConfig = "/Users/your.username/.config/yabai/yabairc";
    };
  • General Configuration: Review and update other configuration files in the repository to suit your preferences. I intend to make this README.md more user friendly in time, for now it isn't particularly granular and will require digging in to understand.

Step 5: Enjoy Your Setup

Your configuration should now be applied. Enjoy the benefits of a declarative and reproducible system configuration!

Maintenance Commands

Aliases for maintaining the flake across Darwin and NixOS are included in .zshrc:

Build Aliases

# Darwin Personal Build
alias darwin-personal-build='darwin-rebuild switch --flake "$(pwd)#personal"'

# Darwin Work Build
alias darwin-work-build='darwin-rebuild switch --flake "$(pwd)#work"'

# NixOS Personal Build
alias nixos-personal-build='sudo nixos-rebuild switch --flake "$(pwd)#personal" && home-manager switch --flake "$(pwd)#personal" --extra-experimental-features nix-command --extra-experimental-features flakes'

# NixOS Work Build
alias nixos-work-build='sudo nixos-rebuild switch --flake "$(pwd)#work" && home-manager switch --flake "$(pwd)#work" --extra-experimental-features nix-command --extra-experimental-features flakes'

Cleaning Aliases

# Darwin Clean
alias darwin-clean='nix-collect-garbage -d'

# NixOS Clean
alias nixos-clean='sudo nix-env --delete-generations old -p /nix/var/nix/profiles/system && sudo nix-collect-garbage -d && flake-build'

Flake Update

# Flake Update
alias flake-update='sudo nix flake update --extra-experimental-features nix-command --extra-experimental-features flakes'

For more information and advanced usage, refer to the official NixOS documentation and nix-darwin documentation.