nixos/programs/gnome-session: add name option for session and components

Also updated the usage for `xToUnit` function from systemd-lib NixOS
library.
This commit is contained in:
Gabriel Arazas 2024-05-17 10:43:17 +08:00
parent 580e0ebe93
commit ce9506a2bc
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
3 changed files with 41 additions and 34 deletions

View File

@ -10,7 +10,7 @@ let
# The bulk of the work. Pretty much the main purpose of this module. # The bulk of the work. Pretty much the main purpose of this module.
sessionPackages = lib.mapAttrsToList sessionPackages = lib.mapAttrsToList
(name: session: (_: session:
let let
gnomeSession = '' gnomeSession = ''
[GNOME Session] [GNOME Session]
@ -22,7 +22,7 @@ let
[Desktop Entry] [Desktop Entry]
Name=@fullName@ Name=@fullName@
Comment=${session.description} Comment=${session.description}
Exec="@out@/libexec/${name}-session" Exec="@out@/libexec/${session.name}-session"
Type=Application Type=Application
DesktopNames=${lib.concatStringsSep ";" session.desktopNames}; DesktopNames=${lib.concatStringsSep ";" session.desktopNames};
''; '';
@ -46,14 +46,14 @@ let
{ {
wayland = '' wayland = ''
( (
DISPLAY_SESSION_FILE="$out/share/wayland-sessions/${name}.desktop" DISPLAY_SESSION_FILE="$out/share/wayland-sessions/${session.name}.desktop"
install -Dm0644 "$displaySessionPath" "$DISPLAY_SESSION_FILE" install -Dm0644 "$displaySessionPath" "$DISPLAY_SESSION_FILE"
${hasMoreDisplays "Wayland"} substituteAllInPlace "$DISPLAY_SESSION_FILE" ${hasMoreDisplays "Wayland"} substituteAllInPlace "$DISPLAY_SESSION_FILE"
) )
''; '';
x11 = '' x11 = ''
( (
DISPLAY_SESSION_FILE="$out/share/xsessions/${name}.desktop" DISPLAY_SESSION_FILE="$out/share/xsessions/${session.name}.desktop"
install -Dm0644 "$displaySessionPath" "$DISPLAY_SESSION_FILE" install -Dm0644 "$displaySessionPath" "$DISPLAY_SESSION_FILE"
${hasMoreDisplays "X11"} substituteAllInPlace "$DISPLAY_SESSION_FILE" ${hasMoreDisplays "X11"} substituteAllInPlace "$DISPLAY_SESSION_FILE"
) )
@ -66,13 +66,10 @@ let
session.display; session.display;
installDesktops = installDesktops =
let
sessionName = name;
in
lib.mapAttrsToList lib.mapAttrsToList
(name: component: (name: component:
let let
scriptName = "${sessionName}-${name}-script"; scriptName = "${session.name}-${component.name}-script";
# There's already a lot of bad bad things in this world, we # There's already a lot of bad bad things in this world, we
# don't to add more of it here (only a fraction of it, though). # don't to add more of it here (only a fraction of it, though).
@ -90,21 +87,21 @@ let
'') '')
session.components; session.components;
in in
pkgs.runCommandLocal "${name}-desktop-session-files" pkgs.runCommandLocal "${session.name}-desktop-session-files"
{ {
env = { env = {
inherit (session) fullName; inherit (session) fullName;
}; };
inherit displaySession gnomeSession sessionScript; inherit displaySession gnomeSession sessionScript;
passAsFile = [ "displaySession" "gnomeSession" "sessionScript" ]; passAsFile = [ "displaySession" "gnomeSession" "sessionScript" ];
passthru.providedSessions = [ name ]; passthru.providedSessions = [ session.name ];
} }
'' ''
SESSION_SCRIPT="$out/libexec/${name}-session" SESSION_SCRIPT="$out/libexec/${session.name}-session"
install -Dm0755 "$sessionScriptPath" "$SESSION_SCRIPT" install -Dm0755 "$sessionScriptPath" "$SESSION_SCRIPT"
substituteAllInPlace "$SESSION_SCRIPT" substituteAllInPlace "$SESSION_SCRIPT"
GNOME_SESSION_FILE="$out/share/gnome-session/sessions/${name}.session" GNOME_SESSION_FILE="$out/share/gnome-session/sessions/${session.name}.session"
install -Dm0644 "$gnomeSessionPath" "$GNOME_SESSION_FILE" install -Dm0644 "$gnomeSessionPath" "$GNOME_SESSION_FILE"
substituteAllInPlace "$GNOME_SESSION_FILE" substituteAllInPlace "$GNOME_SESSION_FILE"
@ -116,26 +113,26 @@ let
cfg.sessions; cfg.sessions;
sessionSystemdUnits = lib.concatMapAttrs sessionSystemdUnits = lib.concatMapAttrs
(name: session: (_: session:
let let
inherit (utils.systemdUtils.lib) inherit (utils.systemdUtils.lib)
pathToUnit serviceToUnit targetToUnit timerToUnit socketToUnit; pathToUnit serviceToUnit targetToUnit timerToUnit socketToUnit;
mkSystemdUnits = name: component: { mkSystemdUnits = name: component: {
"${component.id}.service" = serviceToUnit component.id component.serviceUnit; "${component.id}.service" = serviceToUnit component.serviceUnit;
"${component.id}.target" = targetToUnit component.id component.targetUnit; "${component.id}.target" = targetToUnit component.targetUnit;
} // lib.optionalAttrs (component.socketUnit != null) { } // lib.optionalAttrs (component.socketUnit != null) {
"${component.id}.socket" = socketToUnit component.id component.socketUnit; "${component.id}.socket" = socketToUnit component.socketUnit;
} // lib.optionalAttrs (component.timerUnit != null) { } // lib.optionalAttrs (component.timerUnit != null) {
"${component.id}.timer" = timerToUnit component.id component.timerUnit; "${component.id}.timer" = timerToUnit component.timerUnit;
} // lib.optionalAttrs (component.pathUnit != null) { } // lib.optionalAttrs (component.pathUnit != null) {
"${component.id}.path" = pathToUnit component.id component.pathUnit; "${component.id}.path" = pathToUnit component.pathUnit;
}; };
componentsUnits = lib.concatMapAttrs mkSystemdUnits session.components; componentsUnits = lib.concatMapAttrs mkSystemdUnits session.components;
in in
componentsUnits // { componentsUnits // {
"gnome-session@${name}.target" = targetToUnit "gnome-session@${name}" session.targetUnit; "gnome-session@${session.name}.target" = targetToUnit session.targetUnit;
} }
) )
cfg.sessions; cfg.sessions;

View File

@ -19,6 +19,13 @@ let
in in
{ {
options = { options = {
name = lib.mkOption {
type = lib.types.nonEmptyStr;
description = "Component name.";
default = name;
example = "desktop-manager";
};
description = lib.mkOption { description = lib.mkOption {
type = lib.types.nonEmptyStr; type = lib.types.nonEmptyStr;
description = "One-sentence description of the component."; description = "One-sentence description of the component.";
@ -163,7 +170,7 @@ in
The identifier of the component used in generating filenames for its The identifier of the component used in generating filenames for its
`.desktop` files and as part of systemd unit names. `.desktop` files and as part of systemd unit names.
''; '';
default = "${session.name}.${name}"; default = "${session.name}.${config.name}";
defaultText = "\${session-name}.\${name}"; defaultText = "\${session-name}.\${name}";
readOnly = true; readOnly = true;
}; };
@ -203,7 +210,7 @@ in
* Even if we have a way to limit starting desktop components with * Even if we have a way to limit starting desktop components with
`systemd-xdg-autostart-condition`, using `Service.ExecCondition=` would `systemd-xdg-autostart-condition`, using `Service.ExecCondition=` would
severely limit possible reuse of desktop components with other severely limit possible reuse of desktop components with other
NixOS-module-generated sessiond sessions so we're not bothering with NixOS-module-generated gnome-session sessions so we're not bothering with
those. those.
TODO: Is `Type=notify` a good default? TODO: Is `Type=notify` a good default?

View File

@ -28,6 +28,22 @@ let
in in
{ {
options = { options = {
name = lib.mkOption {
type = lib.types.nonEmptyStr;
description = ''
The identifier of the desktop environment to be used for the filenames
of related outputs.
::: {.note}
While there is no formal specification around naming them, a common
convention is to use kebab-casing of the name (e.g., "mosey-branch" for
"Mosey Branch").
:::
'';
default = name;
example = "mosey-branch";
};
fullName = lib.mkOption { fullName = lib.mkOption {
type = lib.types.nonEmptyStr; type = lib.types.nonEmptyStr;
description = "The display name of the desktop environment."; description = "The display name of the desktop environment.";
@ -188,19 +204,6 @@ in
} }
''; '';
}; };
sessionPackage = lib.mkOption {
type = lib.types.package;
description = ''
The collective package containing everything desktop-related
such as:
* The display session (`<name>.desktop`) files.
* gnome-session `.session` file.
* The components `.desktop` file.
'';
readOnly = true;
};
}; };
config = { config = {