Add support for requesting restarts of services for startup scripts (#222)

This commit is contained in:
magnouvean 2024-06-21 09:51:52 +02:00 committed by GitHub
parent ef64e5da3b
commit 6f182700ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 71 additions and 54 deletions

View File

@ -25,7 +25,7 @@ let
${stringIfNotNull panel.maxLength "panel.maximumLength = ${toString panel.maxLength};"}
${stringIfNotNull panel.minLength "panel.minimumLength = ${toString panel.minLength};"}
${stringIfNotNull panel.offset "panel.offset = ${toString panel.offset};"}
${optionalString (panel.screen != 0) ''panel.writeConfig("lastScreen[$i]", ${toString panel.screen});''}
${optionalString (panel.screen != null) ''panel.writeConfig("lastScreen[$i]", ${toString panel.screen});''}
${addWidgetStmts "panel" "panelWidgets" panel.widgets}
${stringIfNotNull panel.extraSettings panel.extraSettings}

View File

@ -15,25 +15,25 @@ let
description = "The height of the panel.";
};
offset = lib.mkOption {
type = lib.types.nullOr lib.types.int;
type = with lib.types; nullOr int;
default = null;
example = 100;
description = "The offset of the panel from the anchor-point.";
};
minLength = lib.mkOption {
type = lib.types.nullOr lib.types.int;
type = with lib.types; nullOr int;
default = null;
example = 1000;
description = "The minimum required length/width of the panel.";
};
maxLength = lib.mkOption {
type = lib.types.nullOr lib.types.int;
type = with lib.types; nullOr int;
default = null;
example = 1600;
description = "The maximum allowed length/width of the panel.";
};
lengthMode = lib.mkOption {
type = lib.types.nullOr (lib.types.enum [ "fit" "fill" "custom" ]);
type = with lib.types; nullOr (enum [ "fit" "fill" "custom" ]);
default =
if config.minLength != null || config.maxLength != null then
"custom"
@ -44,18 +44,18 @@ let
};
location = lib.mkOption {
type = lib.types.str;
default = lib.types.nullOr (lib.types.enum [ "top" "bottom" "left" "right" "floating" ]);
default = with lib.types; nullOr (enum [ "top" "bottom" "left" "right" "floating" ]);
example = "left";
description = "The location of the panel.";
};
alignment = lib.mkOption {
type = lib.types.nullOr (lib.types.enum [ "left" "center" "right" ]);
type = with lib.types; nullOr (enum [ "left" "center" "right" ]);
default = "center";
example = "right";
description = "The alignment of the panel.";
};
hiding = lib.mkOption {
type = lib.types.nullOr (lib.types.enum [
type = with lib.types; nullOr (enum [
"none"
"autohide"
# Plasma 5 only
@ -102,12 +102,12 @@ let
apply = map widgets.convert;
};
screen = lib.mkOption {
type = lib.types.int;
default = 0;
type = with lib.types; nullOr int;
default = null;
description = "The screen the panel should appear on";
};
extraSettings = lib.mkOption {
type = lib.types.nullOr lib.types.str;
type = with lib.types; nullOr str;
default = null;
description = ''
Extra lines to add to the layout.js. See
@ -136,7 +136,7 @@ in
(
let
anyPanels = ((builtins.length cfg.panels) > 0);
anyNonDefaultScreens = ((builtins.any (panel: panel.screen != 0)) cfg.panels);
anyNonDefaultScreens = ((builtins.any (panel: panel.screen != null)) cfg.panels);
panelPreCMD = (if anyPanels then ''
# We delete plasma-org.kde.plasma.desktop-appletsrc to hinder it
# growing indefinitely. See:
@ -145,11 +145,7 @@ in
'' else "");
panelLayoutStr = (if anyPanels then (import ../lib/panel.nix { inherit lib; inherit config; }) else "");
panelPostCMD = (if anyNonDefaultScreens then ''
if [ -f ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc ]; then
sed -i 's/^lastScreen\\x5b$i\\x5d=/lastScreen[$i]=/' ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc
# We sleep a second in order to prevent some bugs (like the incorrect height being set)
sleep 1; nohup plasmashell --replace &
fi
sed -i 's/^lastScreen\\x5b$i\\x5d=/lastScreen[$i]=/' ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc
'' else "");
# This meaningless comment inserts the URL into the desktop-script
# which means that when the wallpaper is updated, the sha256 hash
@ -189,6 +185,7 @@ in
preCommands = panelPreCMD;
text = panelLayoutStr + wallpaperDesktopScript + wallpaperSlideShow + wallpaperPOTD;
postCommands = panelPostCMD + wallpaperPostCMD;
restartServices = (if anyNonDefaultScreens then [ "plasma-plasmashell" ] else [ ]);
priority = 2;
}
));

View File

@ -5,30 +5,33 @@ let
cfg = config.programs.plasma;
topScriptName = "run_all.sh";
textOption = lib.mkOption {
type = lib.types.str;
description = "The content of the startup-script.";
};
priorityOption = lib.mkOption {
type = (lib.types.ints.between 0 8);
default = 0;
description = "The priority for the execution of the script. Lower priority means earlier execution.";
};
restartServicesOption = lib.mkOption {
type = with lib.types; listOf str;
default = [ ];
description = "Services to restart after the script has been run.";
};
startupScriptType = lib.types.submodule {
options = {
text = lib.mkOption {
type = lib.types.str;
description = "The content of the startup-script.";
};
priority = lib.mkOption {
type = lib.types.int;
description = "The priority for the execution of the script. Lower priority means earlier execution.";
default = 0;
};
text = textOption;
priority = priorityOption;
restartServices = restartServicesOption;
};
};
desktopScriptType = lib.types.submodule {
options = {
text = lib.mkOption {
type = lib.types.str;
description = "The content of the desktop script.";
};
priority = lib.mkOption {
type = lib.types.int;
description = "The priority for the execution of the desktop-script. Lower priority means earlier execution.";
default = 0;
};
text = textOption;
priority = priorityOption;
restartServices = restartServicesOption;
preCommands = lib.mkOption {
type = lib.types.str;
description = "Commands to run before the desktop script lines.";
@ -42,24 +45,26 @@ let
};
};
createScriptContent = name: sha256sumFile: priority: text: {
"plasma-manager/${cfg.startup.scriptsDir}/${builtins.toString priority}_${name}.sh" = {
text =
''
#!/bin/sh
last_update="$(sha256sum ${sha256sumFile})"
last_update_file=${config.xdg.dataHome}/plasma-manager/last_run_${name}
if [ -f "$last_update_file" ]; then
stored_last_update=$(cat "$last_update_file")
fi
createScriptContent = name: sha256sumFile: script: text: {
"plasma-manager/${cfg.startup.scriptsDir}/${builtins.toString script.priority}_${name}.sh" = {
text = ''
#!/bin/sh
last_update="$(sha256sum ${sha256sumFile})"
last_update_file=${config.xdg.dataHome}/plasma-manager/last_run_${name}
if [ -f "$last_update_file" ]; then
stored_last_update=$(cat "$last_update_file")
fi
if ! [ "$last_update" = "$stored_last_update" ]; then
success=1
trap 'success=0' ERR
${text}
[ $success -eq 1 ] && echo "$last_update" > "$last_update_file"
if ! [ "$last_update" = "$stored_last_update" ]; then
success=1
trap 'success=0' ERR
${text}
if [ $success -eq 1 ]; then
echo "$last_update" > "$last_update_file"
${builtins.concatStringsSep "\n" (map (s: "echo ${s} >> ${config.xdg.dataHome}/plasma-manager/services_to_restart") script.restartServices)}
fi
'';
fi
'';
executable = true;
};
};
@ -81,7 +86,7 @@ in
'';
};
dataFile = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
type = with lib.types; attrsOf str;
default = { };
description = "Datafiles, typically for use in autostart scripts.";
};
@ -106,14 +111,14 @@ in
# Autostart scripts
(lib.mkMerge
(lib.mapAttrsToList
(name: script: createScriptContent name "$0" script.priority script.text)
(name: script: createScriptContent name "$0" script script.text)
cfg.startup.startupScript))
# Desktop scripts
(lib.mkMerge
((lib.mapAttrsToList
(name: script:
let layoutScriptPath = "${config.xdg.dataHome}/plasma-manager/${cfg.startup.dataDir}/desktop_script_${name}.js";
in createScriptContent "desktop_script_${name}" layoutScriptPath script.priority
in createScriptContent "desktop_script_${name}" layoutScriptPath script
''
${script.preCommands}
qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript "$(cat ${layoutScriptPath})"
@ -142,9 +147,24 @@ in
"plasma-manager/${topScriptName}" = {
text = ''
#!/bin/sh
services_restart_file="${config.xdg.dataHome}/plasma-manager/services_to_restart"
# Reset the file keeping track of which scripts to restart.
# Technically can be put at the end as well (maybe better, at
# least assuming the file hasn't been tampered with of some sort).
if [ -f $services_restart_file ]; then rm $services_restart_file; fi
for script in ${config.xdg.dataHome}/plasma-manager/${cfg.startup.scriptsDir}/*.sh; do
[ -x "$script" ] && $script
done
# Restart the services
if [ -f $services_restart_file ]; then
for service in $(sort $services_restart_file | uniq); do
systemctl --user restart $service
done
fi
'';
executable = true;
};