Only create dock entries when directory path exists

This commit is contained in:
Olaf 2025-05-08 11:09:53 +02:00
parent 9849195005
commit 3be5337c2e

View File

@ -1,4 +1,9 @@
{ config, pkgs, lib, ... }: {
config,
pkgs,
lib,
...
}:
# Original source: https://gist.github.com/antifuchs/10138c4d838a63c0a05e725ccd7bccdd # Original source: https://gist.github.com/antifuchs/10138c4d838a63c0a05e725ccd7bccdd
@ -15,10 +20,11 @@ in
example = false; example = false;
}; };
local.dock.entries = mkOption local.dock.entries = mkOption {
{ description = "Entries on the Dock";
description = "Entries on the Dock"; type =
type = with types; listOf (submodule { with types;
listOf (submodule {
options = { options = {
path = lib.mkOption { type = str; }; path = lib.mkOption { type = str; };
section = lib.mkOption { section = lib.mkOption {
@ -31,40 +37,64 @@ in
}; };
}; };
}); });
readOnly = true; readOnly = true;
}; };
}; };
config = config = mkIf cfg.enable (
mkIf cfg.enable let
( normalize = path: if hasSuffix ".app" path then path + "/" else path;
let entryURI =
normalize = path: if hasSuffix ".app" path then path + "/" else path; path:
entryURI = path: "file://" + (builtins.replaceStrings "file://"
[" " "!" "\"" "#" "$" "%" "&" "'" "(" ")"] + (builtins.replaceStrings
["%20" "%21" "%22" "%23" "%24" "%25" "%26" "%27" "%28" "%29"] [
(normalize path) " "
); "!"
wantURIs = concatMapStrings "\""
(entry: "${entryURI entry.path}\n") "#"
cfg.entries; "$"
createEntries = concatMapStrings "%"
(entry: "${dockutil}/bin/dockutil --no-restart --add '${entry.path}' --section ${entry.section} ${entry.options}\n") "&"
cfg.entries; "'"
in "("
")"
]
[
"%20"
"%21"
"%22"
"%23"
"%24"
"%25"
"%26"
"%27"
"%28"
"%29"
]
(normalize path)
);
wantURIs = concatMapStrings (entry: "${entryURI entry.path}\n") cfg.entries;
createEntries = concatMapStrings (
entry:
"if [ -d '${entry.path}' ]; then ${dockutil}/bin/dockutil --no-restart --add '${entry.path}' --section ${entry.section} ${entry.options} ; fi \n "
) cfg.entries;
in
{
system.activationScripts.postUserActivation.text = ''
echo >&2 "Setting up the Dock..."
haveURIs="$(${dockutil}/bin/dockutil --list | ${pkgs.coreutils}/bin/cut -f2)"
if ! diff -wu <(echo -n "$haveURIs") <(echo -n '${wantURIs}') >&2 ; then
{ {
system.activationScripts.postUserActivation.text = '' echo >&2 "Resetting Dock."
echo >&2 "Setting up the Dock..." ${dockutil}/bin/dockutil --no-restart --remove all
haveURIs="$(${dockutil}/bin/dockutil --list | ${pkgs.coreutils}/bin/cut -f2)" ${createEntries}
if ! diff -wu <(echo -n "$haveURIs") <(echo -n '${wantURIs}') >&2 ; then killall Dock
echo >&2 "Resetting Dock." }
${dockutil}/bin/dockutil --no-restart --remove all else
${createEntries} echo >&2 "Dock setup complete."
killall Dock fi
else '';
echo >&2 "Dock setup complete." }
fi );
'';
}
);
} }