nix-homebrew

repository·main·Indexed 19 days ago

https://github.com/zhaofengli/nix-homebrew

A tool to manage Homebrew installations on macOS via nix-darwin. It provides declarative tap management, prefix management with Rosetta 2 support, and the ability to migrate existing Homebrew installations into Nix configuration.

Tokens
1.2K
Snippets
6
Records
6
Agent score
23%

What's inside nix-homebrew

  1. Configure declarative tap management

    main

    You can manage Homebrew taps declaratively using the taps attribute.

    Important Naming Rule: In Homebrew, tap repository names must have homebrew- prepended. When declaring taps in nix-homebrew.taps, ensure the key is the unique folder name starting with homebrew- (e.g., user/homebrew-repo).

    Strict Mode: If you set mutableTaps = false, taps can no longer be added imperatively via brew tap. They can only be managed by updating your Nix configuration. To remove a tap in this mode, delete its attribute from nix-homebrew.taps and re-activate the configuration.

    To avoid configuration mismatches between nix-homebrew and nix-darwin, it is recommended to align homebrew.taps with the names used in nix-homebrew.taps.

    nix-homebrew = {
      enable = true;
      taps = {
        "homebrew/homebrew-core" = homebrew-core;
        "homebrew/homebrew-cask" = homebrew-cask;
      };
      mutableTaps = false;
    };
    
    # Align homebrew taps config with nix-homebrew
    ({config, ...}: {
      homebrew.taps = builtins.attrNames config.nix-homebrew.taps;
    })
  2. Migrate an existing Homebrew installation

    main

    If you have already installed Homebrew using the official installation script, you can migrate it to be managed by nix-homebrew by setting autoMigrate = true in your nix-homebrew configuration block.

    modules = [
      nix-homebrew.darwinModules.nix-homebrew
      {
        nix-homebrew = {
          enable = true;
          enableRosetta = true;
          user = "yourname";
          autoMigrate = true;
        };
      }
    ];
  3. Install nix-homebrew via Flake inputs

    main

    To use nix-homebrew, add it to your Nix Flake inputs. If you want to use declarative tap management, you should also include the specific tap inputs (e.g., homebrew-core and homebrew-cask) with flake = false;.

    {
      inputs = {
        nix-homebrew.url = "github:zhaofengli/nix-homebrew";
    
        # Optional: Declarative tap management
        homebrew-core = {
          url = "github:homebrew/homebrew-core";
          flake = false;
        };
        homebrew-cask = {
          url = "github:homebrew/homebrew-cask";
          flake = false;
        };
      };
    }
  4. Perform a new Homebrew installation

    main

    For a fresh installation where Homebrew has not been installed previously, include nix-homebrew.darwinModules.nix-homebrew in your darwinConfigurations modules.

    Key configuration options:

    • enable = true: Installs Homebrew.
    • enableRosetta = true: (Apple Silicon only) Also installs Homebrew under the Intel prefix for Rosetta 2.
    • user = "yourname": Sets the user who owns the Homebrew prefix.
    • autoMigrate = true: (Use this instead if you already have an existing Homebrew installation).
    modules = [
      nix-homebrew.darwinModules.nix-homebrew
      {
        nix-homebrew = {
          enable = true;
          enableRosetta = true;
          user = "yourname";
        };
      }
    ];
  5. Configure Homebrew tap trust entries

    main

    For non-official taps, Homebrew requires explicit trust. You can configure these entries declaratively via nix-homebrew.trust.

    Note: Removing an entry from these lists in your Nix configuration will not automatically remove the trust entry from Homebrew. To remove a trust entry, you must manually run the brew untrust command.

    nix-homebrew.trust = {
      formulae = [ "user/repo/formula" ];
      casks = [ "user/repo/cask" ];
      commands = [ "user/repo/command" ];
      taps = [ "user/repo" ];
    };
  6. Configure non-standard Homebrew prefixes

    main

    You can define extra Homebrew prefixes using the nix-homebrew.prefixes attribute.

    Warning: When using a non-standard prefix, you will no longer be able to use most bottles (prebuilt packages).

    nix-homebrew.prefixes = {
      "/some/prefix" = {
        library = "/some/prefix/Library";
        taps = {
          # ...
        };
      };
    }