config/modules/macos/icons.nix

62 lines
1.6 KiB
Nix
Raw Normal View History

2025-04-18 15:55:34 -07:00
{
lib,
config,
...
2025-05-12 15:19:01 -07:00
}:
let
2025-04-18 15:55:34 -07:00
cfg = config.environment.customIcons;
2025-05-12 15:19:01 -07:00
inherit (lib)
2025-04-18 15:55:34 -07:00
mkEnableOption
mkIf
mkMerge
mkOption
types
;
2025-05-12 15:19:01 -07:00
in
{
2025-04-18 15:55:34 -07:00
options.environment.customIcons = {
enable = mkEnableOption "environment.customIcons";
clearCacheOnActivation = mkEnableOption "environment.customIcons.clearCacheOnActivation";
icons = mkOption {
type = types.listOf (
types.submodule {
options = {
2025-05-12 15:19:01 -07:00
path = mkOption { type = types.path; };
icon = mkOption { type = types.path; };
2025-04-18 15:55:34 -07:00
};
}
);
};
};
config = mkMerge [
(mkIf cfg.enable {
system.activationScripts.extraActivation.text = ''
echo -e "applying custom icons..."
${
(builtins.concatStringsSep "\n\n" (
builtins.map (iconCfg: ''
osascript <<EOF >/dev/null
use framework "Cocoa"
set iconPath to "${iconCfg.icon}"
set destPath to "${iconCfg.path}"
set imageData to (current application's NSImage's alloc()'s initWithContentsOfFile:iconPath)
(current application's NSWorkspace's sharedWorkspace()'s setIcon:imageData forFile:destPath options:2)
EOF
2025-05-12 15:19:01 -07:00
'') cfg.icons
2025-04-18 15:55:34 -07:00
))
}
${lib.optionalString cfg.clearCacheOnActivation ''
sudo rm -rf /Library/Caches/com.apple.iconservices.store
sudo find /private/var/folders/ -name com.apple.dock.iconcache -or -name com.apple.iconservices -or -name com.apple.iconservicesagent -exec rm -rf {} \; || true
killall Dock
''}'';
})
];
}