1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-03-06 16:57:08 +00:00
nix-darwin/modules/system/defaults-write.nix

63 lines
2.1 KiB
Nix
Raw Normal View History

2016-12-14 13:05:23 +01:00
{ config, lib, ... }:
with lib;
let
cfg = config.system.defaults;
2018-01-01 09:20:27 +09:00
isFloat = x: isString x && builtins.match "^[+-]?([0-9]*[.])?[0-9]+$" x != null;
2017-12-31 11:44:02 +09:00
2016-12-14 13:05:23 +01:00
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
2017-12-31 11:44:02 +09:00
if isFloat value then "-float ${toString value}" else
2016-12-14 13:05:23 +01:00
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);
NSGlobalDomain = defaultsToList "-g" cfg.NSGlobalDomain;
LaunchServices = defaultsToList "com.apple.LaunchServices" cfg.LaunchServices;
dock = defaultsToList "com.apple.dock" cfg.dock;
finder = defaultsToList "com.apple.finder" cfg.finder;
2018-01-05 00:15:11 +01:00
smb = defaultsToList "/Library/Preferences/SystemConfiguration/com.apple.smb.server" cfg.smb;
2018-02-27 17:24:45 +00:00
screencapture = defaultsToList "com.apple.screencapture" cfg.screencapture;
trackpad = defaultsToList "com.apple.AppleMultitouchTrackpad" cfg.trackpad;
trackpadBluetooth = defaultsToList "com.apple.driver.AppleBluetoothMultitouch.trackpad" cfg.trackpad;
2016-12-14 13:05:23 +01:00
mkIfAttrs = list: mkIf (any (attrs: attrs != {}) list);
2016-12-14 13:05:23 +01:00
in
{
config = {
system.activationScripts.defaults.text = mkIfAttrs [ smb ]
''
# Set defaults
echo >&2 "system defaults..."
${concatStringsSep "\n" smb}
'';
system.activationScripts.userDefaults.text = mkIfAttrs
2018-02-27 17:24:45 +00:00
[ NSGlobalDomain LaunchServices dock finder screencapture trackpad trackpadBluetooth ]
''
# Set defaults
echo >&2 "user defaults..."
${concatStringsSep "\n" NSGlobalDomain}
${concatStringsSep "\n" LaunchServices}
${concatStringsSep "\n" dock}
${concatStringsSep "\n" finder}
2018-02-27 17:24:45 +00:00
${concatStringsSep "\n" screencapture}
${concatStringsSep "\n" trackpad}
${concatStringsSep "\n" trackpadBluetooth}
'';
2016-12-14 13:05:23 +01:00
};
}