fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
3.3 KiB107 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.targets.darwin;
7
8  mkActivationCmds = isLocal: settings:
9    let
10      toDefaultsFile = domain: attrs:
11        pkgs.writeText "${domain}.plist" (lib.generators.toPlist { } attrs);
12
13      cliFlags = lib.optionalString isLocal "-currentHost";
14
15      toActivationCmd = domain: attrs:
16        "$DRY_RUN_CMD /usr/bin/defaults ${cliFlags} import ${
17          escapeShellArg domain
18        } ${toDefaultsFile domain attrs}";
19
20      nonNullDefaults =
21        mapAttrs (domain: attrs: (filterAttrs (n: v: v != null) attrs))
22        settings;
23
24      writableDefaults =
25        filterAttrs (domain: attrs: attrs != { }) nonNullDefaults;
26    in mapAttrsToList toActivationCmd writableDefaults;
27
28  defaultsCmds = mkActivationCmds false cfg.defaults;
29  currentHostDefaultsCmds = mkActivationCmds true cfg.currentHostDefaults;
30
31  activationCmds = defaultsCmds ++ currentHostDefaultsCmds;
32in {
33  meta.maintainers = [ maintainers.midchildan ];
34
35  options.targets.darwin.defaults = mkOption {
36    type = types.submodule ./opts-allhosts.nix;
37    default = { };
38    example = {
39      "com.apple.desktopservices" = {
40        DSDontWriteNetworkStores = true;
41        DSDontWriteUSBStores = true;
42      };
43    };
44    description = ''
45      Set macOS user defaults. Values set to `null` are
46      ignored.
47
48      ::: {.warning}
49      Some settings might require a re-login to take effect.
50      :::
51
52      ::: {.warning}
53      Some settings are only read from
54      {option}`targets.darwin.currentHostDefaults`.
55      :::
56    '';
57  };
58
59  options.targets.darwin.currentHostDefaults = mkOption {
60    type = types.submodule ./opts-currenthost.nix;
61    default = { };
62    example = {
63      "com.apple.controlcenter" = { BatteryShowPercentage = true; };
64    };
65    description = ''
66      Set macOS user defaults. Unlike {option}`targets.darwin.defaults`,
67      the preferences will only be applied to the currently logged-in host. This
68      distinction is important for networked accounts.
69
70      Values set to `null` are ignored.
71
72      ::: {.warning}
73      Some settings might require a re-login to take effect.
74      :::
75    '';
76  };
77
78  config = mkIf (activationCmds != [ ]) {
79    assertions = [
80      (hm.assertions.assertPlatform "targets.darwin.defaults" pkgs
81        platforms.darwin)
82    ];
83
84    warnings = let
85      batteryOptionName = ''
86        targets.darwin.currentHostDefaults."com.apple.controlcenter".BatteryShowPercentage'';
87      batteryPercentage =
88        attrByPath [ "com.apple.menuextra.battery" "ShowPercent" ] null
89        cfg.defaults;
90      webkitDevExtras = attrByPath [
91        "com.apple.Safari"
92        "com.apple.Safari.ContentPageGroupIdentifier.WebKit2DeveloperExtrasEnabled"
93      ] null cfg.defaults;
94    in optional (batteryPercentage != null) ''
95      The option 'com.apple.menuextra.battery.ShowPercent' no longer works on
96      macOS 11 and later. Instead, use '${batteryOptionName}'.
97    '' ++ optional (webkitDevExtras != null) ''
98      The option 'com.apple.Safari.com.apple.Safari.ContentPageGroupIdentifier.WebKit2DeveloperExtrasEnabled'
99      is no longer present in recent versions of Safari.
100    '';
101
102    home.activation.setDarwinDefaults = hm.dag.entryAfter [ "writeBoundary" ] ''
103      $VERBOSE_ECHO "Configuring macOS user defaults"
104      ${concatStringsSep "\n" activationCmds}
105    '';
106  };
107}