nixos/programs/gnome-session: add top-level systemd namespace for systemd-specific options

Also included a little refactor.
This commit is contained in:
Gabriel Arazas 2024-08-21 18:51:57 +08:00
parent 23b2be907f
commit 03f51384cf
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
3 changed files with 150 additions and 137 deletions

View File

@ -107,20 +107,20 @@ let
pathToUnit serviceToUnit targetToUnit timerToUnit socketToUnit; pathToUnit serviceToUnit targetToUnit timerToUnit socketToUnit;
mkSystemdUnits = name: component: { mkSystemdUnits = name: component: {
"${component.id}.service" = serviceToUnit component.serviceUnit; "${component.id}.service" = serviceToUnit component.systemd.serviceUnit;
"${component.id}.target" = targetToUnit component.targetUnit; "${component.id}.target" = targetToUnit component.systemd.targetUnit;
} // lib.optionalAttrs (component.socketUnit != null) { } // lib.optionalAttrs (component.systemd.socketUnit != null) {
"${component.id}.socket" = socketToUnit component.socketUnit; "${component.id}.socket" = socketToUnit component.systemd.socketUnit;
} // lib.optionalAttrs (component.timerUnit != null) { } // lib.optionalAttrs (component.systemd.timerUnit != null) {
"${component.id}.timer" = timerToUnit component.timerUnit; "${component.id}.timer" = timerToUnit component.systemd.timerUnit;
} // lib.optionalAttrs (component.pathUnit != null) { } // lib.optionalAttrs (component.systemd.pathUnit != null) {
"${component.id}.path" = pathToUnit component.pathUnit; "${component.id}.path" = pathToUnit component.systemd.pathUnit;
}; };
componentsUnits = lib.concatMapAttrs mkSystemdUnits session.components; componentsUnits = lib.concatMapAttrs mkSystemdUnits session.components;
in in
componentsUnits // { componentsUnits // {
"gnome-session@${session.name}.target" = targetToUnit session.targetUnit; "gnome-session@${session.name}.target" = targetToUnit session.systemd.targetUnit;
} }
) )
cfg.sessions; cfg.sessions;

View File

@ -1,10 +1,13 @@
{ name, config, pkgs, lib, utils, session, ... }: { name, config, pkgs, lib, utils, session, ... }:
let let
optionalSystemdUnitOption = type: systemdModuleAttribute: optionalSystemdUnitOption = {
unitType, systemdModuleAttribute, otherType,
}:
lib.mkOption { lib.mkOption {
type = lib.types.nullOr otherType;
description = '' description = ''
An optional systemd ${type} configuration to be generated. This should An optional systemd ${unitType} configuration to be generated. This should
be configured if the session is managed by systemd. be configured if the session is managed by systemd.
:::{.note} :::{.note}
@ -73,6 +76,7 @@ in
}; };
}; };
systemd = {
# Most of the systemd config types are trying to eliminate as much of the # Most of the systemd config types are trying to eliminate as much of the
# NixOS systemd extensions as much as possible. For more details, see # NixOS systemd extensions as much as possible. For more details, see
# `config` attribute of the `sessionType`. # `config` attribute of the `sessionType`.
@ -136,43 +140,50 @@ in
visible = "shallow"; visible = "shallow";
}; };
timerUnit = optionalSystemdUnitOption "timer" "timers" // { timerUnit = optionalSystemdUnitOption {
type = unitType = "timer";
systemdModuleAttribute = "timers";
otherType =
let let
inherit (utils.systemdUtils.unitOptions) timerOptions commonUnitOptions; inherit (utils.systemdUtils.unitOptions) timerOptions commonUnitOptions;
inherit (utils.systemdUtils.lib) unitConfig; inherit (utils.systemdUtils.lib) unitConfig;
in in
with lib.types; nullOr (submodule [ lib.types.submodule [
commonUnitOptions commonUnitOptions
timerOptions timerOptions
unitConfig unitConfig
]); ];
}; };
socketUnit = optionalSystemdUnitOption "socket" "sockets" // { socketUnit = optionalSystemdUnitOption {
type = unitType = "socket";
systemdModuleAttribute = "sockets";
otherType =
let let
inherit (utils.systemdUtils.unitOptions) socketOptions commonUnitOptions; inherit (utils.systemdUtils.unitOptions) socketOptions commonUnitOptions;
inherit (utils.systemdUtils.lib) unitConfig; inherit (utils.systemdUtils.lib) unitConfig;
in in
with lib.types; nullOr (submodule [ lib.types.submodule [
commonUnitOptions commonUnitOptions
socketOptions socketOptions
unitConfig unitConfig
]); ];
}; };
pathUnit = optionalSystemdUnitOption "path" "paths" // { pathUnit = optionalSystemdUnitOption {
type = unitType = "path";
systemdModuleAttribute = "paths";
otherType =
let let
inherit (utils.systemdUtils.unitOptions) pathOptions commonUnitOptions; inherit (utils.systemdUtils.unitOptions) pathOptions commonUnitOptions;
inherit (utils.systemdUtils.lib) unitConfig; inherit (utils.systemdUtils.lib) unitConfig;
in in
with lib.types; nullOr (submodule [ lib.types.submodule [
commonUnitOptions commonUnitOptions
pathOptions pathOptions
unitConfig unitConfig
]); ];
};
}; };
id = lib.mkOption { id = lib.mkOption {
@ -235,7 +246,7 @@ in
systemd user unit, much of them are unnecessary and rarely needed (if systemd user unit, much of them are unnecessary and rarely needed (if
ever like `Service.PrivateTmp=`?) so we didn't set such defaults here. ever like `Service.PrivateTmp=`?) so we didn't set such defaults here.
*/ */
serviceUnit = { systemd.serviceUnit = {
script = lib.mkAfter config.script; script = lib.mkAfter config.script;
description = lib.mkDefault config.description; description = lib.mkDefault config.description;
@ -282,7 +293,7 @@ in
likely for a user to design their own desktop session with full control likely for a user to design their own desktop session with full control
so it would be better for these options to be empty for less confusion. so it would be better for these options to be empty for less confusion.
*/ */
targetUnit = { systemd.targetUnit = {
# This should be the dependency-related directive to be configured. The # This should be the dependency-related directive to be configured. The
# rest is for the user to judge. # rest is for the user to judge.
wants = [ "${config.id}.service" ]; wants = [ "${config.id}.service" ];

View File

@ -184,6 +184,7 @@ in
]; ];
}; };
systemd = {
targetUnit = lib.mkOption { targetUnit = lib.mkOption {
type = type =
let let
@ -220,12 +221,13 @@ in
''; '';
}; };
}; };
};
config = { config = {
# Append the session argument. # Append the session argument.
extraArgs = [ "--session=${name}" ]; extraArgs = [ "--session=${name}" ];
targetUnit = { systemd.targetUnit = {
overrideStrategy = lib.mkForce "asDropin"; overrideStrategy = lib.mkForce "asDropin";
wants = lib.mkDefault (builtins.map (c: "${c}.target") config.requiredComponents); wants = lib.mkDefault (builtins.map (c: "${c}.target") config.requiredComponents);
}; };