From e6bc540ce0714a02a5e7d1677622e3308b6252c5 Mon Sep 17 00:00:00 2001 From: r-vdp Date: Tue, 28 Jan 2025 11:40:38 +0100 Subject: [PATCH] nixos/activation: pass the action as an argument to the pre-switch-checks This allows for instance to reject switching into a configuration, while still allowing to reboot into that same configuration. This can be useful for instance to reject switching to a configuration with a new systemd major version, but setting that same configuration as the new boot default with `switch-to-configuration boot` is fine. --- nixos/modules/system/activation/switch-to-configuration.pl | 2 +- nixos/tests/switch-test.nix | 6 ++++-- pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs | 5 +++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/nixos/modules/system/activation/switch-to-configuration.pl b/nixos/modules/system/activation/switch-to-configuration.pl index 3553c5fce4ce..672291541d03 100755 --- a/nixos/modules/system/activation/switch-to-configuration.pl +++ b/nixos/modules/system/activation/switch-to-configuration.pl @@ -103,7 +103,7 @@ if (($ENV{"NIXOS_NO_CHECK"} // "") ne "1") { chomp(my $pre_switch_checks = <<'EOFCHECKS'); @preSwitchCheck@ EOFCHECKS - system("$pre_switch_checks $out") == 0 or exit 1; + system("$pre_switch_checks $out $action") == 0 or exit 1; if ($action eq "check") { exit 0; } diff --git a/nixos/tests/switch-test.nix b/nixos/tests/switch-test.nix index 37a808c3c471..4ad08b726bf5 100644 --- a/nixos/tests/switch-test.nix +++ b/nixos/tests/switch-test.nix @@ -612,8 +612,10 @@ in { system.switch.enable = true; users.mutableUsers = true; system.preSwitchChecks.succeeds = '' - echo this will succeed - true + config="$1" + action="$2" + echo "this should succeed (config: $config, action: $action)" + [ "$action" == "check" ] || [ "$action" == "test" ] ''; specialisation.failingCheck.configuration.system.preSwitchChecks.failEveryTime = '' echo this will fail diff --git a/pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs b/pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs index 93da7aff7406..ecd6a6a71390 100644 --- a/pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs +++ b/pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs @@ -139,7 +139,7 @@ fn parse_os_release() -> Result> { })) } -fn do_pre_switch_check(command: &str, toplevel: &Path) -> Result<()> { +fn do_pre_switch_check(command: &str, toplevel: &Path, action: &Action) -> Result<()> { let mut cmd_split = command.split_whitespace(); let Some(argv0) = cmd_split.next() else { bail!("missing first argument in install bootloader commands"); @@ -148,6 +148,7 @@ fn do_pre_switch_check(command: &str, toplevel: &Path) -> Result<()> { match std::process::Command::new(argv0) .args(cmd_split.collect::>()) .arg(toplevel) + .arg::<&str>(action.into()) .spawn() .map(|mut child| child.wait()) { @@ -1053,7 +1054,7 @@ fn do_system_switch(action: Action) -> anyhow::Result<()> { .unwrap_or_default() != "1" { - do_pre_switch_check(&pre_switch_check, &toplevel)?; + do_pre_switch_check(&pre_switch_check, &toplevel, action)?; } if *action == Action::Check {