1{
2 lib,
3 config,
4 ...
5}:
6let
7 cfg = config.environment.customIcons;
8 inherit (lib)
9 mkEnableOption
10 mkIf
11 mkMerge
12 mkOption
13 types
14 ;
15in
16{
17 options.environment.customIcons = {
18 enable = mkEnableOption "environment.customIcons";
19 clearCacheOnActivation = mkEnableOption "environment.customIcons.clearCacheOnActivation";
20
21 icons = mkOption {
22 type = types.listOf (
23 types.submodule {
24 options = {
25 path = mkOption { type = types.path; };
26 icon = mkOption { type = types.path; };
27 };
28 }
29 );
30 };
31 };
32
33 config = mkMerge [
34 (mkIf cfg.enable {
35 system.activationScripts.extraActivation.text = ''
36 echo -e "applying custom icons..."
37
38 ${
39 (builtins.concatStringsSep "\n\n" (
40 builtins.map (iconCfg: ''
41 osascript <<EOF >/dev/null
42 use framework "Cocoa"
43
44 set iconPath to "${iconCfg.icon}"
45 set destPath to "${iconCfg.path}"
46
47 set imageData to (current application's NSImage's alloc()'s initWithContentsOfFile:iconPath)
48 (current application's NSWorkspace's sharedWorkspace()'s setIcon:imageData forFile:destPath options:2)
49 EOF
50 '') cfg.icons
51 ))
52 }
53
54 ${lib.optionalString cfg.clearCacheOnActivation ''
55 sudo rm -rf /Library/Caches/com.apple.iconservices.store
56 sudo find /private/var/folders/ -name com.apple.dock.iconcache -or -name com.apple.iconservices -or -name com.apple.iconservicesagent -exec rm -rf {} \; || true
57 killall Dock
58 ''}'';
59 })
60 ];
61}