Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to minimize steam deck display manager restart risk #237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 48 additions & 25 deletions src/action/linux/start_systemd_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ Start a given systemd unit
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct StartSystemdUnit {
unit: String,
enable: bool,
}

impl StartSystemdUnit {
#[tracing::instrument(level = "debug", skip_all)]
pub async fn plan(unit: impl AsRef<str>) -> Result<StatefulAction<Self>, ActionError> {
pub async fn plan(
unit: impl AsRef<str>,
enable: bool,
) -> Result<StatefulAction<Self>, ActionError> {
Ok(StatefulAction {
action: Self {
unit: unit.as_ref().to_string(),
enable,
},
state: ActionState::Uncompleted,
})
Expand Down Expand Up @@ -47,19 +52,35 @@ impl Action for StartSystemdUnit {

#[tracing::instrument(level = "debug", skip_all)]
async fn execute(&mut self) -> Result<(), ActionError> {
let Self { unit, .. } = self;

// TODO(@Hoverbear): Handle proxy vars
execute_command(
Command::new("systemctl")
.process_group(0)
.arg("enable")
.arg("--now")
.arg(format!("{unit}"))
.stdin(std::process::Stdio::null()),
)
.await
.map_err(|e| ActionError::Custom(Box::new(StartSystemdUnitError::Command(e))))?;
let Self { unit, enable } = self;

match enable {
true => {
// TODO(@Hoverbear): Handle proxy vars
execute_command(
Command::new("systemctl")
.process_group(0)
.arg("enable")
.arg("--now")
.arg(format!("{unit}"))
.stdin(std::process::Stdio::null()),
)
.await
.map_err(|e| ActionError::Custom(Box::new(StartSystemdUnitError::Command(e))))?;
},
false => {
// TODO(@Hoverbear): Handle proxy vars
execute_command(
Command::new("systemctl")
.process_group(0)
.arg("start")
.arg(format!("{unit}"))
.stdin(std::process::Stdio::null()),
)
.await
.map_err(|e| ActionError::Custom(Box::new(StartSystemdUnitError::Command(e))))?;
},
}

Ok(())
}
Expand All @@ -73,17 +94,19 @@ impl Action for StartSystemdUnit {

#[tracing::instrument(level = "debug", skip_all)]
async fn revert(&mut self) -> Result<(), ActionError> {
let Self { unit, .. } = self;

execute_command(
Command::new("systemctl")
.process_group(0)
.arg("disable")
.arg(format!("{unit}"))
.stdin(std::process::Stdio::null()),
)
.await
.map_err(|e| ActionError::Custom(Box::new(StartSystemdUnitError::Command(e))))?;
let Self { unit, enable } = self;

if *enable {
execute_command(
Command::new("systemctl")
.process_group(0)
.arg("disable")
.arg(format!("{unit}"))
.stdin(std::process::Stdio::null()),
)
.await
.map_err(|e| ActionError::Custom(Box::new(StartSystemdUnitError::Command(e))))?;
};

// We do both to avoid an error doing `disable --now` if the user did stop it already somehow.
execute_command(
Expand Down
12 changes: 8 additions & 4 deletions src/planner/linux/steam_deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ impl Planner for SteamDeck {
Requires=nix-directory.service\n\
ConditionPathIsDirectory=/nix\n\
DefaultDependencies=no\n\
RequiredBy=nix-daemon.service\n\
RequiredBy=nix-daemon.socket\n\
\n\
[Mount]\n\
What={persistence}\n\
Expand Down Expand Up @@ -180,15 +182,13 @@ impl Planner for SteamDeck {
After=nix.mount\n\
Requires=nix-directory.service\n\
Requires=nix.mount\n\
PropagatesStopTo=nix-directory.service\n\
PropagatesStopTo=nix.mount\n\
DefaultDependencies=no\n\
\n\
[Service]\n\
Type=oneshot\n\
RemainAfterExit=yes\n\
ExecStart=/usr/bin/systemctl daemon-reload\n\
ExecStart=/usr/bin/systemctl restart --no-block sockets.target timers.target multi-user.target\n\
ExecStart=/usr/bin/systemctl restart --no-block nix-daemon.socket\n\
\n\
[Install]\n\
WantedBy=sysinit.target\n\
Expand All @@ -213,7 +213,7 @@ impl Planner for SteamDeck {
nix_directory_unit.boxed(),
create_bind_mount_unit.boxed(),
ensure_symlinked_units_resolve_unit.boxed(),
StartSystemdUnit::plan("ensure-symlinked-units-resolve.service".to_string())
StartSystemdUnit::plan("nix.mount".to_string(), false)
.await
.map_err(PlannerError::Action)?
.boxed(),
Expand All @@ -230,6 +230,10 @@ impl Planner for SteamDeck {
.await
.map_err(PlannerError::Action)?
.boxed(),
StartSystemdUnit::plan("ensure-symlinked-units-resolve.service".to_string(), true)
.await
.map_err(PlannerError::Action)?
.boxed(),
])
}

Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/linux/steam-deck.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
{
"action": {
"action": "start_systemd_unit",
"unit": "ensure-symlinked-units-resolve.service"
"unit": "ensure-symlinked-units-resolve.service",
"enable": true
},
"state": "Uncompleted"
},
Expand Down