Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fish: Unknown command: darwin-rebuild, even after installing. #1286

Open
esn89 opened this issue Jan 20, 2025 · 30 comments
Open

fish: Unknown command: darwin-rebuild, even after installing. #1286

esn89 opened this issue Jan 20, 2025 · 30 comments

Comments

@esn89
Copy link

esn89 commented Jan 20, 2025

I have installed nix via determinate systems like so:

curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
mkdir -p ~/.config/nix-darwin
cd ~/.config/nix-darwin
nix flake init -t nix-darwin/master
sed -i '' "s/simple/$(scutil --get LocalHostName)/" flake.nix

My ~/.config/nix-darwin/flake.nix looks like this:

{
  description = "Example nix-darwin system flake";

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

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

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

      # Enable alternative shell support in nix-darwin.
      # programs.fish.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 = "x86_64-darwin";
    };
  in
  {
    # Build darwin flake using:
    # $ darwin-rebuild build --flake .#MacBook-Pro
    darwinConfigurations."MacBook-Pro" = nix-darwin.lib.darwinSystem {
      modules = [ configuration ];
    };
  };
}

When I run:
nix run nix-darwin -- switch --flake ~/.config/nix-darwin it succeeds, but:

Error message:

𝙄 ~/.config/nix-darwin ❯ nix run nix-darwin -- switch --flake ~/.config/nix-darwin
building the system configuration...
setting up user launchd services...
setting up /Applications/Nix Apps...
setting up pam...
applying patches...
setting up /etc...
setting up launchd services...
reloading service org.nixos.activate-system
reloading service org.nixos.nix-daemon
reloading nix-daemon...
waiting for nix-daemon
waiting for nix-daemon
configuring networking...
configuring power...
setting up /Library/Fonts/Nix Fonts...
setting nvram variables...
𝙄 ~/.config/nix-darwin ❯ darwin-rebuild                                                                                            22s
fish: Unknown command: darwin-rebuild

I am using fish shell and my $PATH is:

/nix/var/nix/profiles/default/bin /opt/local/libexec/gnubin /opt/local/bin  /usr/local/bin /System/Cryptexes/App/usr/bin /usr/bin /bin /usr/sbin /sbin /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin /Applications/kitty.app/Contents/MacOS
@zhangchi0104
Copy link

Hi, I ran into the same issue as well

MacOS Version: Sequoia 15.2 arm64
Shell: zsh

My ~/.config/nix-darwin/flake.nix

{
  description = "Example nix-darwin system flake";

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

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

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

      # Enable alternative shell support in nix-darwin.
      # programs.fish.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 .#zhangchis-MacBook-Pro
    darwinConfigurations."zhangchis-MacBook-Pro" = nix-darwin.lib.darwinSystem {
      modules = [ configuration ];
    };
  };
}

@emilazy
Copy link
Collaborator

emilazy commented Jan 20, 2025

Does it work after opening a new terminal window?

Note that you need to uncomment the programs.fish.enable line if you’re using Fish.

@zhangchi0104
Copy link

Does it work after opening a new terminal window?

Note that you need to uncomment the programs.fish.enable line if you’re using Fish.

I am using zsh. It should be enabled by default according to #1134. I have checked my $PATH. It looks like /run/current-system/sw/bin is not there.

@emilazy
Copy link
Collaborator

emilazy commented Jan 20, 2025

Yeah, if you’re using Zsh then a new terminal window should have the correct $PATH. What are you getting instead?

@zhangchi0104
Copy link

Yeah, if you’re using Zsh then a new terminal window should have the correct $PATH. What are you getting instead?

This is the $PATH output

❯ echo $PATH

/Users/alexzhang/miniconda3/condabin:/Users/alexzhang/.cabal/bin:/Users/alexzhang/.ghcup/bin:/Users/alexzhang/.ghcup/bin/:/Users/alexzhang/Scripts:/Users/alexzhang/.yarn/bin:/Users/alexzhang/.asdf/shims:/opt/homebrew/opt/asdf/libexec/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/alexzhang/.cargo/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Library/TeX/texbin:/Applications/Docker.app/Contents/Resources/bin/:/Users/alexzhang/.orbstack/bin

❯ env | grep NIX

__NIX_DARWIN_SET_ENVIRONMENT_DONE=1
NIX_PATH=nixpkgs=flake:nixpkgs:/nix/var/nix/profiles/per-user/root/channels
NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
NIX_USER_PROFILE_DIR=/nix/var/nix/profiles/per-user/alexzhang
NIX_PROFILES=/nix/var/nix/profiles/default /run/current-system/sw /Users/alexzhang/.nix-profile
NIX_REMOTE=daemon

and also nix is gone as well

❯ nix
zsh: command not found: nix

uninstalling nix-darwin will recover nix

@emilazy
Copy link
Collaborator

emilazy commented Jan 20, 2025

Do you perhaps have a ~/.zshrc or similar that is overwriting the $PATH?

@esn89
Copy link
Author

esn89 commented Jan 20, 2025

@emilazy

I have uncommented that line now:

{
  description = "Example nix-darwin system flake";

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

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

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

      # Enable alternative shell support in nix-darwin.
      programs.fish.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 = "x86_64-darwin";
    };
  in
  {
    # Build darwin flake using:
    # $ darwin-rebuild build --flake .#MacBook-Pro
    darwinConfigurations."MacBook-Pro" = nix-darwin.lib.darwinSystem {
      modules = [ configuration ];
    };
  };
}

Then ran:

building the system configuration...
warning: Git tree '/Users/evan/.config/nix-darwin' is dirty
Password:
user defaults...
setting up user launchd services...
setting up /Applications/Nix Apps...
setting up pam...
applying patches...
setting up /etc...
system defaults...
setting up launchd services...
reloading service org.nixos.activate-system
reloading nix-daemon...
waiting for nix-daemon
configuring networking...
configuring power...
setting up /Library/Fonts/Nix Fonts...
setting nvram variables...

However, the issue still persists

@zhangchi0104
Copy link

Do you perhaps have a ~/.zshrc or similar that is overwriting the $PATH?

Thanks for pointing this out! In my case I had the following in my ~/.zshenv

unset PATH
PATH=""
if [ -x /usr/libexec/path_helper ]; then
     eval `/usr/libexec/path_helper -s`
fi

removing these lines solves the issue

@esn89
Copy link
Author

esn89 commented Jan 20, 2025

If it helps, for me, I have this in my ~/.config/fish/config.fish:

if status is-interactive
    # Commands to run in interactive sessions can go here
    set -x PATH /opt/local/libexec/gnubin /opt/local/bin ~/.go/bin $PATH
    source /opt/local/share/fzf/shell/key-bindings.fish
    set -U fish_greeting ""
    set -gx KUBE_EDITOR nvim
    zoxide init --cmd cd fish | source
    source "$HOME/.cargo/env.fish"
    # source /etc/static/fish/config.fish
end

@emilazy
Copy link
Collaborator

emilazy commented Jan 21, 2025

@zhangchi0104 Great! Feel free to open another issue if you have any other problems :)

@esn89 Thanks. It looks like that snippet shouldn’t be causing problems by itself (though it will mean that MacPorts executables are preferred over ones from nix-darwin, which may have confusing consequences). Can you run echo $PATH in a new terminal window now and show the output? Also, where did you install Fish from?

@esn89
Copy link
Author

esn89 commented Jan 21, 2025

Here is the $PATH:

/opt/local/libexec/gnubin /opt/local/bin /Users/esn89/.go/bin /nix/var/nix/profiles/default/bin /Users/esn89/.cargo/bin /usr/local/bin /System/Cryptexes/App/usr/bin /usr/bin /bin /usr/sbin /sbin /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin /Applications/kitty.app/Contents/MacOS

I installed Fish from macports

@esn89
Copy link
Author

esn89 commented Jan 22, 2025

Any other ideas? :(

@Samasaur1
Copy link
Contributor

Can you try fish from nixpkgs, just to see if it works?

@esn89
Copy link
Author

esn89 commented Jan 25, 2025

@Samasaur1 no luck here either.

@bestlem
Copy link

bestlem commented Jan 25, 2025

I would try with nothing in ~/.config/fish

The issue seems to me to be /run/current-system/sw/bin is not on the path.
What does ls -l /run/current-system show?

@ngaxavi
Copy link

ngaxavi commented Jan 26, 2025

@bestlem Thanks. adding /run/current-system/sw/bin to the path fixed the issue.

@patmuk
Copy link

patmuk commented Jan 29, 2025

@ngaxavi does it still work after a reboot?

I just upgraded to sequoia (without a proper nix uninstall) and reinstalled nix.
However, /run/current-system was not created. I symlinked it by hand - but when I reboot my Mac the current-system folder is gone again!

Is that the case for you as well? @bestlem what is supposed to create this symlink?

@Samasaur1
Copy link
Contributor

@ngaxavi does it still work after a reboot?

I just upgraded to sequoia (without a proper nix uninstall) and reinstalled nix. However, /run/current-system was not created. I symlinked it by hand - but when I reboot my Mac the current-system folder is gone again!

Is that the case for you as well? @bestlem what is supposed to create this symlink?

This sounds like a different issue. Creating /run/current-system on boot is done by the org.nixos.activate-system LaunchDaemon, which I suspect you turned off in System Settings. There should be a list of three or four sh entries in the Login Items settings page in System Settings, and they all need to be on for nix/nix-darwin to function properly

@Samasaur1
Copy link
Contributor

There's an issue (#1219) tracking improving the current launchd situation, which includes making those services show up with descriptive names.

@patmuk
Copy link

patmuk commented Jan 29, 2025

Ah, many thanks! Yes, the daemon is not running, and I indeed turned off some of the 'sh' entries. Thought they where duplicate skhd & yabai entries, that kept duplicating on nix switch (which looks like it is fixed now).

@Samasaur1 many thanks for pointing me in this direction!!

@ngaxavi
Copy link

ngaxavi commented Jan 31, 2025

@patmuk Can you tell me what you configured so that the nix-daemon starts on reboot?

@patmuk
Copy link

patmuk commented Jan 31, 2025

@ngaxavi
tl;dr: The daemon is starting automatically after the nix installation. Its script is in /Library/LaunchDaemons/org.nixos.nix-daemon.plist.

Here is my journey:
I ran the installer from determinate systems: https://determinate.systems/nix-installer/
The first time I set up nix under MacOS 14. That configured the daemon to start automatically - or in more details, it installed four launch daemons (in /Library/LaunchDaemons). The one, which starts the nix daemon is, I think, org.nixos.nix-daemon.plist.
In system settings.app -> login items there are four 'sh' processes that are allowed to start at startup.

I had troubles with one local project ... and did not upgraded to Sequoia. As a second project with the same setup worked, and I couldn't figure the difference, I decided to upgrade to sequoia and install nix again.

While trying to get the project to work I deactivated the 'sh' entries in login items and forgot to enable them again. Additionally I ran the installer again without deinstalling nix first. After upgrading MacOS I couldn't unlock the nix store virtual drive anymore. As it is on my main drive, which is encrypted (standard Mac setup), it did not needed to be encrypted in the first place. So I deleted it and ran the installer without encrypting the nix store: curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --macos.

Nothing worked, so I began to eliminate one mistake after another until I got the last hint here - that the four 'sh' processes are from nix and are needed.

Hope that helped!

@ngaxavi
Copy link

ngaxavi commented Jan 31, 2025

I found the problem: every time I run darwin-rebuild switch --flake ~/.config/nix, the Nix daemon and Darwin store are unloaded, and the files org.nixos.nix-daemon.plist and org.nixos.darwin-store are deleted from /Library/LaunchDaemons. I have added launchd.daemons-configuration to my flake.nix, but it is not working.

Does anyone know why the Nix daemon gets unloaded when running darwin-rebuild? Here is my configuration.

{
  description = "Mac Darwin system flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    mac-app-util.url = "github:hraban/mac-app-util";
    nix-darwin.url = "github:LnL7/nix-darwin/master";
    nix-homebrew.url = "github:zhaofengli-wip/nix-homebrew";
    nix-darwin.inputs.nixpkgs.follows = "nixpkgs";
    determinate.url = "https://flakehub.com/f/DeterminateSystems/determinate/0.1";
  };

  outputs = inputs@{ self, nix-darwin, nixpkgs, mac-app-util, nix-homebrew, determinate }:
  let
    configuration = { pkgs, config, ... }: {
      
      # List packages installed in system profile. To search by name, run:
      # $ nix-env -qaP | grep wget
      environment.systemPackages =
        [ pkgs.vim
	  pkgs.neovim
	   pkgs.alacritty
          pkgs.fish
          pkgs.magic-wormhole
          pkgs.jdk21_headless
          pkgs.jdk17_headless
          pkgs.jdk23_headless
        ];

      services.nix-daemon.enable = true;

      launchd.daemons.nix-daemon.serviceConfig = {
        KeepAlive = true;
        Program = "/run/current-system/sw/bin/nix-daemon";
      };

      homebrew = {
        enable = true;
        brews = [
          "stow"
          "ykman"
          "libfido2"
          "gnupg"
          "openssl@3"
          "fish"
          "nvm"
          "pnpm"
          "npm"
        ];
        casks = [
          "ghostty"
          "jetbrains-toolbox"
          "docker"
          "microsoft-teams"
          "logi-options+"
        ];
        masApps = {
          "Windows App" = 1295203466;
          "CopyClip" = 595191960;
        };
        onActivation.cleanup = "zap";
        onActivation.autoUpdate = true;
        onActivation.upgrade = true;
      };

      # Set fish as the default shell
      users.knownUsers = [ "test" ];
      users.users.test.uid = 501;
      users.users.test.shell = pkgs.fish;
	
    
      nixpkgs.config.allowUnfree = true;

      system.defaults = {
        dock.autohide = true;
        finder.AppleShowAllExtensions = true;
        finder.FXPreferredViewStyle = "clmv";
      };

      # https://samasaur1.github.io/blog/jdks-on-nix-darwin
      system.activationScripts.extraActivation.text = ''
        ln -sf "${pkgs.jdk17_headless}/zulu-17.jdk" "/Library/Java/JavaVirtualMachines/"
        ln -sf "${pkgs.jdk21_headless}/zulu-21.jdk" "/Library/Java/JavaVirtualMachines/"
        ln -sf "${pkgs.jdk23_headless}/zulu-23.jdk" "/Library/Java/JavaVirtualMachines/"
      '';

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

      # Enable alternative shell support in nix-darwin.
      programs.zsh.enable = true;
      programs.fish.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 .#simple
    darwinConfigurations."mini" = nix-darwin.lib.darwinSystem {
      modules = [	 
      	configuration
	      mac-app-util.darwinModules.default
	      determinate.darwinModules.default
        nix-homebrew.darwinModules.nix-homebrew
        {
          nix-homebrew = {
            enable = true;
             # Apple Silicon Only: Also install Homebrew under the default Intel prefix for Rosetta 2
            enableRosetta = true;

               # User owning the Homebrew prefix
            user = "ngaxavi";

            autoMigrate = true;
          };
        }

      ];
    };
  };
}

@bestlem
Copy link

bestlem commented Feb 1, 2025

Note this is nothing to do with the original issue.

re 'Does anyone know why the Nix daemon gets unloaded when running darwin-rebuild?"
Because the nix executable might be changed and so you want to replace the existing lauchd files as they have the path to nix in them.

As for your issue - I don't know.
But I don't think that launchd.daemons.nix-daemon.serviceConfig is needed

To debug I would start from an empty configuration - just the inputs and a darwinConfiguration without any modules and minimal everything else - just get zsh working

@esn89
Copy link
Author

esn89 commented Feb 1, 2025

I would try with nothing in ~/.config/fish

The issue seems to me to be /run/current-system/sw/bin is not on the path. What does ls -l /run/current-system show?

It shows:

𝙄 ~ ❯ ls -l /run/current-system
lrwxr-xr-x@    - root 31 Dec  1969  Applications -> /nix/store/vpm7riqcg6mqfqn6zz381vin7yka4yyy-system-applications/Applications
dr-xr-xr-x@    - root 31 Dec  1969  darwin
lrwxr-xr-x@    - root 31 Dec  1969  etc -> /nix/store/mfkp60hlvkkrwdmf441qp9hs6c82l68g-etc/etc
dr-xr-xr-x@    - root 31 Dec  1969  Library
lrwxr-xr-x@    - root 31 Dec  1969  patches -> /nix/store/325w8njnis9baq2wyz08kfk60ya6b6yw-patches/patches
lrwxr-xr-x@    - root 31 Dec  1969  sw -> /nix/store/wgrivmga0mvslkhbmqvmx6f6dknzjszz-system-path
dr-xr-xr-x@    - root 31 Dec  1969  user
.r-xr-xr-x@  12k root 31 Dec  1969  activate
.r-xr-xr-x@  13k root 31 Dec  1969  activate-user
.r--r--r--@ 7.2k root 31 Dec  1969  darwin-changes
.r--r--r--@   13 root 31 Dec  1969  darwin-version
lrwxr-xr-x@    - root 31 Dec  1969  darwin-version.json -> /nix/store/lc18dir3id2ql2kg8xwjbjlbjy4vnsqb-darwin-version.json
.r--r--r--@   13 root 31 Dec  1969  system
.r--r--r--@   71 root 31 Dec  1969  systemConfig

@esn89
Copy link
Author

esn89 commented Feb 1, 2025

Image

@Samasaur1 this is the 4 sh files we're talking about, right?

@esn89
Copy link
Author

esn89 commented Feb 1, 2025

/run/current-system/sw/bin

Is this the fix? I mean I do see the darwin-rebuild binaries in there. However, I am just curious as how this got skipped? Was it supposed to be added manually and was just missing in the docs?

Was it supposed to be automated but automation failed somewhere?

@esn89
Copy link
Author

esn89 commented Feb 1, 2025

I've added /run/current-system/sw/bin into my PATH and can now see darwin-rebuild. I've ran it:

But getting:

𝙄 ~/.config/nix-darwin main !2 ?1 ❯ darwin-rebuild switch                                                                                                                                           23s
building the system configuration...
error: file 'darwin' was not found in the Nix search path (add it using $NIX_PATH or -I)

I did some googling and applied this fix:
#740 (comment)

But to no avail

@emilazy
Copy link
Collaborator

emilazy commented Feb 1, 2025

Does anyone know why the Nix daemon gets unloaded when running darwin-rebuild? Here is my configuration.

@ngaxavi Did you pass the --determinate flag to the Determinate Systems installer (or use their Determinate.pkg graphical macOS installer)? If you didn’t, then you installed vanilla Nix and don’t need the determinate.darwinModules.default module you currently have in your configuration; removing it should hopefully make things work.

If you did, then you installed the Determinate distribution, which has its own daemon to manage Nix that currently doesn’t interoperate well with nix-darwin even when using their module. #1313 should provide a better user experience for users of Determinate, but hasn’t yet been reviewed and merged.

@ngaxavi
Copy link

ngaxavi commented Feb 4, 2025

@emilazy Thanks! it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants