Skip to content

Commit

Permalink
dock: refactor persistent-apps option
Browse files Browse the repository at this point in the history
Allows passing in a list of attribute sets for different kinds of tiles.
  • Loading branch information
khaneliman committed Dec 18, 2024
1 parent c5e8286 commit 9631aba
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 11 deletions.
69 changes: 61 additions & 8 deletions modules/system/defaults/dock.nix
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,75 @@ in {
};

system.defaults.dock.persistent-apps = mkOption {
type = types.nullOr (types.listOf (types.either types.path types.str));
type = let
taggedType = types.attrTag {
app = mkOption {
description = "An application to be added to the dock.";
default = null;
type = types.submodule {
options.path = mkOption {
description = "Path to the application.";
type = types.str;
};
};
};
spacer = mkOption {
description = "A spacer to be added to the dock. Can be small or regular size.";
default = {
small = false;
};
type = types.submodule {
options.small = mkOption {
description = "Whether the spacer is small.";
type = types.bool;
};
};
};
folder = mkOption {
description = "A folder to be added to the dock.";
default = null;
type = types.submodule {
options.path = mkOption {
description = "Path to the folder.";
type = types.str;
};
};
};
};
in
types.nullOr (types.listOf (types.coercedTo types.str (path: {app = {inherit path;};}) taggedType));
default = null;
example = [ "/Applications/Safari.app" "/System/Applications/Utilities/Terminal.app" ];
example = [
{ app = { path = "/Applications/Safari.app"; }; }
{ spacer = { small = false; }; }
{ spacer = { small = true; }; }
{ folder = { path = "/System/Applications/Utilities"; }; }
];
description = ''
Persistent applications in the dock.
Persistent applications, spacers, and folders in the dock.
'';
apply =
let
tileTypes = ["spacer-tile" "small-spacer-tile"];
toSpecialTile = type: { tile-data = {}; tile-type = type; };
toAppTile = cfurl: { tile-data = { file-data = { _CFURLString = cfurl; _CFURLStringType = 0; }; }; };
toTile = s: if elem s tileTypes then toSpecialTile s else toAppTile s;
toTile = item: if item ? app then {
tile-data = { file-data = { _CFURLString = item.app.path; _CFURLStringType = 0; }; };
} else if item ? spacer then {
tile-data = {};
tile-type = if item.spacer.small then "small-spacer-tile" else "spacer-tile";
} else if item ? folder then {
tile-data = { file-data = { _CFURLString = "file://" + item.folder.path; _CFURLStringType = 15; }; };
tile-type = if strings.hasInfix "." (last (splitString "/" item.folder.path)) then "file-tile" else "directory-tile";
} else item;
in
value: if isList value then map toTile value else value;
};

# apply =
# let
# tileTypes = ["spacer-tile" "small-spacer-tile"];
# toSpecialTile = type: { tile-data = {}; tile-type = type; };
# toAppTile = cfurl: { tile-data = { file-data = { _CFURLString = cfurl; _CFURLStringType = 0; }; }; };
# toTile = s: if elem s tileTypes then toSpecialTile s else toAppTile s;
# in
# value: if isList value then map toTile value else value;
system.defaults.dock.persistent-others = mkOption {
type = types.nullOr (types.listOf (types.either types.path types.str));
default = null;
Expand Down
30 changes: 28 additions & 2 deletions tests/fixtures/system-defaults-write/activate-user.txt
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ defaults write com.apple.dock 'persistent-apps' $'<?xml version="1.0" encoding="
<key>file-data</key>
<dict>
<key>_CFURLString</key>
<string>MyApp.app</string>
<string>/Applications/MyApp.app</string>
<key>_CFURLStringType</key>
<integer>0</integer>
</dict>
Expand All @@ -267,7 +267,19 @@ defaults write com.apple.dock 'persistent-apps' $'<?xml version="1.0" encoding="
<key>file-data</key>
<dict>
<key>_CFURLString</key>
<string>Cool.app</string>
<string>/Applications/MyApp.app</string>
<key>_CFURLStringType</key>
<integer>0</integer>
</dict>
</dict>
</dict>
<dict>
<key>tile-data</key>
<dict>
<key>file-data</key>
<dict>
<key>_CFURLString</key>
<string>/Applications/Cool.app</string>
<key>_CFURLStringType</key>
<integer>0</integer>
</dict>
Expand All @@ -289,6 +301,20 @@ defaults write com.apple.dock 'persistent-apps' $'<?xml version="1.0" encoding="
<key>tile-type</key>
<string>spacer-tile</string>
</dict>
<dict>
<key>tile-data</key>
<dict>
<key>file-data</key>
<dict>
<key>_CFURLString</key>
<string>file:///Applications/Utilities</string>
<key>_CFURLStringType</key>
<integer>15</integer>
</dict>
</dict>
<key>tile-type</key>
<string>directory-tile</string>
</dict>
</array>
</plist>'
defaults write com.apple.dock 'persistent-others' $'<?xml version="1.0" encoding="UTF-8"?>
Expand Down
9 changes: 8 additions & 1 deletion tests/system-defaults-write.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@
system.defaults.dock.appswitcher-all-displays = false;
system.defaults.dock.autohide-delay = 0.24;
system.defaults.dock.orientation = "left";
system.defaults.dock.persistent-apps = ["MyApp.app" "Cool.app" "small-spacer-tile" "spacer-tile"];
system.defaults.dock.persistent-apps = [
"/Applications/MyApp.app"
{ app = { path = "/Applications/MyApp.app"; }; }
{ app = { path = "/Applications/Cool.app"; }; }
{ spacer = { small = true; }; }
{ spacer = { small = false; }; }
{ folder = { path = "/Applications/Utilities"; }; }
];
system.defaults.dock.persistent-others = ["~/Documents" "~/Downloads/file.txt"];
system.defaults.dock.scroll-to-open = false;
system.defaults.finder.AppleShowAllFiles = true;
Expand Down

0 comments on commit 9631aba

Please sign in to comment.