{ config, lib, ... }: with lib; let cfg = config.system.defaults; isFloat = x: isString x && builtins.match "^[+-]?([0-9]*[.])?[0-9]+$" x != null; boolValue = x: if x then "YES" else "NO"; writeValue = value: if isBool value then "-bool ${boolValue value}" else if isInt value then "-int ${toString value}" else if isFloat value then "-float ${toString value}" else if isString value then "-string '${value}'" else throw "invalid value type"; writeDefault = domain: key: value: "defaults write ${domain} '${key}' ${writeValue value}"; defaultsToList = domain: attrs: mapAttrsToList (writeDefault domain) (filterAttrs (n: v: v != null) attrs); # defaults alf = defaultsToList "/Library/Preferences/com.apple.alf" cfg.alf; loginwindow = defaultsToList "/Library/Preferences/com.apple.loginwindow" cfg.loginwindow; smb = defaultsToList "/Library/Preferences/SystemConfiguration/com.apple.smb.server" cfg.smb; SoftwareUpdate = defaultsToList "/Library/Preferences/SystemConfiguration/com.apple.SoftwareUpdate" cfg.SoftwareUpdate; # userDefaults GlobalPreferences = defaultsToList ".GlobalPreferences" cfg.".GlobalPreferences"; LaunchServices = defaultsToList "com.apple.LaunchServices" cfg.LaunchServices; NSGlobalDomain = defaultsToList "-g" cfg.NSGlobalDomain; dock = defaultsToList "com.apple.dock" cfg.dock; finder = defaultsToList "com.apple.finder" cfg.finder; magicmouse = defaultsToList "com.apple.AppleMultitouchMouse" cfg.magicmouse; magicmouseBluetooth = defaultsToList "com.apple.driver.AppleMultitouchMouse.mouse" cfg.magicmouse; screencapture = defaultsToList "com.apple.screencapture" cfg.screencapture; 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; mkIfAttrs = list: mkIf (any (attrs: attrs != { }) list); in { config = { system.activationScripts.defaults.text = mkIfAttrs [ alf loginwindow smb SoftwareUpdate ] '' # Set defaults echo >&2 "system defaults..." ${concatStringsSep "\n" alf} ${concatStringsSep "\n" loginwindow} ${concatStringsSep "\n" smb} ${concatStringsSep "\n" SoftwareUpdate} ''; system.activationScripts.userDefaults.text = mkIfAttrs [ GlobalPreferences LaunchServices NSGlobalDomain dock finder magicmouse magicmouseBluetooth screencapture spaces trackpad trackpadBluetooth universalaccess ActivityMonitor ] '' # Set defaults echo >&2 "user defaults..." ${concatStringsSep "\n" NSGlobalDomain} ${concatStringsSep "\n" GlobalPreferences} ${concatStringsSep "\n" LaunchServices} ${concatStringsSep "\n" dock} ${concatStringsSep "\n" finder} ${concatStringsSep "\n" magicmouse} ${concatStringsSep "\n" magicmouseBluetooth} ${concatStringsSep "\n" screencapture} ${concatStringsSep "\n" spaces} ${concatStringsSep "\n" trackpad} ${concatStringsSep "\n" trackpadBluetooth} ${concatStringsSep "\n" universalaccess} ${concatStringsSep "\n" ActivityMonitor} ''; }; }