summaryrefslogtreecommitdiff
path: root/modules/system/defaults-write.nix
blob: 7ad97da3b3505190e0f27fbc06afd937aff6b4d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{ config, lib, ... }:

with lib;

let

  cfg = config.system.defaults;

  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 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;
  trackpad = defaultsToList "com.apple.driver.AppleBluetoothMultitouch.trackpad" cfg.trackpad;

in

{
  options = {
  };

  config = {

    system.activationScripts.defaults.text = ''
      # Set defaults
      echo "writing defaults..." >&2

      ${concatStringsSep "\n" NSGlobalDomain}
      ${concatStringsSep "\n" LaunchServices}
      ${concatStringsSep "\n" dock}
      ${concatStringsSep "\n" finder}
      ${concatStringsSep "\n" trackpad}
    '';

  };
}