LnL7 / nix-darwin

nix modules for darwin
MIT License
2.82k stars 431 forks source link

com.apple.controlcenter.plist values #899

Closed rmgpinto closed 6 months ago

rmgpinto commented 7 months ago

Is there a way I could achive the settings below?

# Bluetooth
defaults write ~/Library/Preferences/ByHost/com.apple.controlcenter.plist Bluetooth -int 18
# Sound
defaults write ~/Library/Preferences/ByHost/com.apple.controlcenter.plist Sound -int 16
# Battery
defaults write ~/Library/Preferences/ByHost/com.apple.controlcenter.plist BatteryShowPercentage -int 1
# Spotlight
defaults -currentHost write com.apple.Spotlight MenuItemHidden -int 1

Thanks

Samasaur1 commented 7 months ago

The first three can be set like so:

system.defaults = {
  CustomUserPreferences = {
    "~/Library/Preferences/ByHost/com.apple.controlcenter.plist" = {
      "Bluetooth" = 18;
      "Sound" = 16;
      "BatteryShowPercentage" = 1;
    };
  };
};

I'm not sure how to get nix-darwin to use the currentHost flag, so I don't know how to set the Spotlight setting. The closest I can get is:

system.defaults = {
  CustomSystemPreferences = {
    "com.apple.Spotlight" = {
      "MenuItemHidden" = 1;
    };
  };
};

which produces this command:

defaults write com.apple.Spotlight 'MenuItemHidden' $'<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<integer>1</integer>
</plist>'

which is equivalent to defaults write com.apple.Spotlight MenuItemHidden -int 1

Samasaur1 commented 7 months ago

Ah, okay. nix-darwin doesn't support the -currentHost flag (although home-manager does). However, in practice it seems that you can replicate this option by using a path in ~/Library/Preferences/ByHost (as you did for Control Center). Thus:

system.defaults = {
  CustomUserPreferences = {
    "~/Library/Preferences/ByHost/com.apple.controlcenter.plist" = {
      "Bluetooth" = 18;
      "Sound" = 16;
      "BatteryShowPercentage" = 1;
    };
    "~/Library/Preferences/ByHost/com.apple.Spotlight.plist" = {
      "MenuItemHidden" = 1;
    };
  };
};

should do exactly what you want

rmgpinto commented 6 months ago

thanks @Samasaur1

Is there a way to define those CustomUserPreferences in a separate block, example:

system.defaults = {
  CustomUserPreferences = {
    "~/Library/Preferences/ByHost/com.apple.controlcenter.plist" = {
      "Bluetooth" = 18;
      "BatteryShowPercentage" = 1;
    };
  };

  // more attributes...

  CustomUserPreferences = {
    "~/Library/Preferences/ByHost/com.apple.controlcenter.plist" = {
      "Sound" = 16;
    };
  };
};

the last block is overriding the first one.

Samasaur1 commented 6 months ago

huh, that's weird, I don't think it's supposed to do that. Regardless, you should be able to set the key directly, which will hopefully get around the issue

system.defaults.CustomUserPreferences."~/Library/Preferences/ByHost/com.apple.controlcenter.plist"."Sound" = 16;