zhaofengli / nix-homebrew

Homebrew installation manager for nix-darwin
MIT License
273 stars 13 forks source link

ln: /usr/local/Homebrew/Library/Homebrew: No such file or directory #43

Closed wenbang24 closed 3 weeks ago

wenbang24 commented 1 month ago

When running darwin-rebuild switch --flake ~/nix#mbp, it tells me that there is no brew - isn't nix-homebrew supposed to install homebrew for me? This is the output:

❯ darwin-rebuild switch --flake ~/nix#mbp
building the system configuration...
setting up Homebrew (/opt/homebrew)...
setting up Homebrew (/usr/local)...
ln: /usr/local/Homebrew/Library/Homebrew: No such file or directory

This is my flake.nix:

{
  description = "wenbang24's Darwin system flake";

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

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

      nixpkgs.config.allowUnfree = true;

      # List packages installed in system profile. To search by name, run:
      # $ nix-env -qaP | grep wget
      environment.systemPackages =
        [
            pkgs.neovim
            pkgs.ffmpeg
            pkgs.zoxide
            pkgs.thefuck
            pkgs.python3
            pkgs.kitty
            pkgs.yt-dlp
            pkgs.openjdk
            pkgs.cmatrix
            pkgs.snow 
            pkgs.yabai
            pkgs.skhd
            pkgs.gimp
            pkgs.clang-tools
            pkgs.eza
            pkgs.git
            pkgs.oh-my-posh
            pkgs.obsidian
            pkgs.vesktop
            pkgs.spicetify-cli
            pkgs.spotify
            pkgs.mkalias
            #pkgs.zsh-autosuggestions
            pkgs.zsh-f-sy-h
        ];

      homebrew = {
        enable = true;
      };

      system.activationScripts.applications.text = let
        env = pkgs.buildEnv {
        name = "system-applications";
        paths = config.environment.systemPackages;
        pathsToLink = "/Applications";
        };
      in
        pkgs.lib.mkForce ''
        # Set up applications.
        echo "setting up /Applications..." >&2
        rm -rf /Applications/Nix\ Apps
        mkdir -p /Applications/Nix\ Apps
        find ${env}/Applications -maxdepth 1 -type l -exec readlink '{}' + |
        while read src; do
        app_name=$(basename "$src")
        echo "copying $src" >&2
        ${pkgs.mkalias}/bin/mkalias "$src" "/Applications/Nix Apps/$app_name"
        done
            '';
      # Auto upgrade nix package and the daemon service.
      services.nix-daemon.enable = true;
      # nix.package = pkgs.nix;

      # Necessary for using flakes on this system.
      nix.settings.experimental-features = "nix-command flakes";

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

      # Set Git commit hash for darwin-version.
      system.configurationRevision = self.rev or self.dirtyRev or null;

      # Used for backwards compatibility, please read the changelog before changing.
      # $ darwin-rebuild changelog
      system.stateVersion = 5;

      # The platform the configuration will be used on.
      nixpkgs.hostPlatform = "aarch64-darwin";
    };
  in
  {
    # Build darwin flake using:
    # $ darwin-rebuild build --flake .#mbp
    darwinConfigurations."mbp" = nix-darwin.lib.darwinSystem {
      modules = [
        configuration
        nix-homebrew.darwinModules.nix-homebrew
        {
          nix-homebrew = {
            enable = true;
            enableRosetta = true;
            user = "ben_wang";
            #autoMigrate = true;
          };
        }
      ];
    };

    # Expose the package set, including overlays, for convenience.
    darwinPackages = self.darwinConfigurations."mbp".pkgs;
  };
}

I did originally have homebrew installed but i uninstalled it, which is why the autoMigrate line is commented out. I am on 2020 Macbook Pro M1

Does anyone know how to fix this?

wenbang24 commented 1 month ago

somehow the error changed but idk why lol

leoroese commented 3 weeks ago

@wenbang24 I was getting a similar issue when porting my nix flake from one macbook to another. I found that deleting the /usr/local/.managed_by_nix_darwin then retrying fixed it for me.

My .managed_by_nix_darwin was empty, so I'd be careful to double check yours

I also deleted my flake.lock and ran the initial command nix run nix-darwin --extra-experimental-features "nix-command flakes" -- switch --flake ~/dotfiles/nix#macbookpro

wenbang24 commented 3 weeks ago

yeah deleting /usr/local/.managed_by_nix_darwin worked for me!

thanks @leoroese