defaults: move userDefaults to system activation

This commit is contained in:
Emily 2025-01-11 15:44:41 +00:00
parent 7877cba5f5
commit f47b8062cb
5 changed files with 172 additions and 143 deletions

View file

@ -88,6 +88,7 @@ in
${cfg.activationScripts.patches.text}
${cfg.activationScripts.etc.text}
${cfg.activationScripts.defaults.text}
${cfg.activationScripts.userDefaults.text}
${cfg.activationScripts.launchd.text}
${cfg.activationScripts.userLaunchd.text}
${cfg.activationScripts.nix-daemon.text}
@ -138,7 +139,6 @@ in
${cfg.activationScripts.checks.text}
${cfg.activationScripts.etcChecks.text}
${cfg.activationScripts.extraUserActivation.text}
${cfg.activationScripts.userDefaults.text}
${cfg.activationScripts.postUserActivation.text}

View file

@ -9,6 +9,12 @@ let
"defaults write ${domain} '${key}' $'${strings.escape [ "'" ] (generators.toPlist { } value)}'";
defaultsToList = domain: attrs: mapAttrsToList (writeDefault domain) (filterAttrs (n: v: v != null) attrs);
userDefaultsToList = domain: attrs: let
user = escapeShellArg config.system.primaryUser;
in map
(cmd: ''launchctl asuser "$(id -u -- ${user})" sudo --user=${user} -- ${cmd}'')
(defaultsToList domain attrs);
# Filter out options to not pass through
# dock has alias options that we need to ignore
dockFiltered = (builtins.removeAttrs cfg.dock ["expose-group-by-app"]);
@ -18,29 +24,29 @@ let
loginwindow = defaultsToList "/Library/Preferences/com.apple.loginwindow" cfg.loginwindow;
smb = defaultsToList "/Library/Preferences/SystemConfiguration/com.apple.smb.server" cfg.smb;
SoftwareUpdate = defaultsToList "/Library/Preferences/com.apple.SoftwareUpdate" cfg.SoftwareUpdate;
CustomSystemPreferences = flatten (mapAttrsToList (name: value: defaultsToList name value) cfg.CustomSystemPreferences);
# userDefaults
GlobalPreferences = defaultsToList ".GlobalPreferences" cfg.".GlobalPreferences";
LaunchServices = defaultsToList "com.apple.LaunchServices" cfg.LaunchServices;
NSGlobalDomain = defaultsToList "-g" cfg.NSGlobalDomain;
menuExtraClock = defaultsToList "com.apple.menuextra.clock" cfg.menuExtraClock;
dock = defaultsToList "com.apple.dock" dockFiltered;
finder = defaultsToList "com.apple.finder" cfg.finder;
hitoolbox = defaultsToList "com.apple.HIToolbox" cfg.hitoolbox;
iCal = defaultsToList "com.apple.iCal" cfg.iCal;
magicmouse = defaultsToList "com.apple.AppleMultitouchMouse" cfg.magicmouse;
magicmouseBluetooth = defaultsToList "com.apple.driver.AppleMultitouchMouse.mouse" cfg.magicmouse;
screencapture = defaultsToList "com.apple.screencapture" cfg.screencapture;
screensaver = defaultsToList "com.apple.screensaver" cfg.screensaver;
spaces = defaultsToList "com.apple.spaces" cfg.spaces;
trackpad = defaultsToList "com.apple.AppleMultitouchTrackpad" cfg.trackpad;
trackpadBluetooth = defaultsToList "com.apple.driver.AppleBluetoothMultitouch.trackpad" cfg.trackpad;
universalaccess = defaultsToList "com.apple.universalaccess" cfg.universalaccess;
ActivityMonitor = defaultsToList "com.apple.ActivityMonitor" cfg.ActivityMonitor;
WindowManager = defaultsToList "com.apple.WindowManager" cfg.WindowManager;
controlcenter = defaultsToList "~/Library/Preferences/ByHost/com.apple.controlcenter" cfg.controlcenter;
CustomUserPreferences = flatten (mapAttrsToList (name: value: defaultsToList name value) cfg.CustomUserPreferences);
CustomSystemPreferences = flatten (mapAttrsToList (name: value: defaultsToList name value) cfg.CustomSystemPreferences);
GlobalPreferences = userDefaultsToList ".GlobalPreferences" cfg.".GlobalPreferences";
LaunchServices = userDefaultsToList "com.apple.LaunchServices" cfg.LaunchServices;
NSGlobalDomain = userDefaultsToList "-g" cfg.NSGlobalDomain;
menuExtraClock = userDefaultsToList "com.apple.menuextra.clock" cfg.menuExtraClock;
dock = userDefaultsToList "com.apple.dock" dockFiltered;
finder = userDefaultsToList "com.apple.finder" cfg.finder;
hitoolbox = userDefaultsToList "com.apple.HIToolbox" cfg.hitoolbox;
iCal = userDefaultsToList "com.apple.iCal" cfg.iCal;
magicmouse = userDefaultsToList "com.apple.AppleMultitouchMouse" cfg.magicmouse;
magicmouseBluetooth = userDefaultsToList "com.apple.driver.AppleMultitouchMouse.mouse" cfg.magicmouse;
screencapture = userDefaultsToList "com.apple.screencapture" cfg.screencapture;
screensaver = userDefaultsToList "com.apple.screensaver" cfg.screensaver;
spaces = userDefaultsToList "com.apple.spaces" cfg.spaces;
trackpad = userDefaultsToList "com.apple.AppleMultitouchTrackpad" cfg.trackpad;
trackpadBluetooth = userDefaultsToList "com.apple.driver.AppleBluetoothMultitouch.trackpad" cfg.trackpad;
universalaccess = userDefaultsToList "com.apple.universalaccess" cfg.universalaccess;
ActivityMonitor = userDefaultsToList "com.apple.ActivityMonitor" cfg.ActivityMonitor;
WindowManager = userDefaultsToList "com.apple.WindowManager" cfg.WindowManager;
controlcenter = userDefaultsToList "~${config.system.primaryUser}/Library/Preferences/ByHost/com.apple.controlcenter" cfg.controlcenter;
CustomUserPreferences = flatten (mapAttrsToList (name: value: userDefaultsToList name value) cfg.CustomUserPreferences);
mkIfLists = list: mkIf (any (attrs: attrs != [ ]) list);
@ -58,6 +64,30 @@ in
else types.float.check x;
};
system.requiresPrimaryUser = concatMap
(scope: mapAttrsToList
(name: value: mkIf (value != null) (showOption [ "system" "defaults" scope name ]))
(if scope == "dock" then dockFiltered else cfg.${scope}))
[
"CustomUserPreferences"
".GlobalPreferences"
"LaunchServices"
"NSGlobalDomain"
"menuExtraClock"
"dock"
"finder"
"hitoolbox"
"magicmouse"
"screencapture"
"screensaver"
"spaces"
"trackpad"
"universalaccess"
"ActivityMonitor"
"WindowManager"
"controlcenter"
];
system.activationScripts.defaults.text = mkIfLists [
alf
loginwindow
@ -125,11 +155,8 @@ in
${concatStringsSep "\n" controlcenter}
${optionalString (length dock > 0) ''
# Only restart Dock if current user is logged in
if pgrep -xu $UID Dock >/dev/null; then
echo >&2 "restarting Dock..."
killall Dock || true
fi
echo >&2 "restarting Dock..."
killall -qu ${escapeShellArg config.system.primaryUser} Dock || true
''}
'';