0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-13 21:50:33 +03:00

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.
This commit is contained in:
r-vdp 2025-01-28 11:40:38 +01:00
parent 0c598375d8
commit e6bc540ce0
No known key found for this signature in database
3 changed files with 8 additions and 5 deletions

View file

@ -103,7 +103,7 @@ if (($ENV{"NIXOS_NO_CHECK"} // "") ne "1") {
chomp(my $pre_switch_checks = <<'EOFCHECKS'); chomp(my $pre_switch_checks = <<'EOFCHECKS');
@preSwitchCheck@ @preSwitchCheck@
EOFCHECKS EOFCHECKS
system("$pre_switch_checks $out") == 0 or exit 1; system("$pre_switch_checks $out $action") == 0 or exit 1;
if ($action eq "check") { if ($action eq "check") {
exit 0; exit 0;
} }

View file

@ -612,8 +612,10 @@ in {
system.switch.enable = true; system.switch.enable = true;
users.mutableUsers = true; users.mutableUsers = true;
system.preSwitchChecks.succeeds = '' system.preSwitchChecks.succeeds = ''
echo this will succeed config="$1"
true action="$2"
echo "this should succeed (config: $config, action: $action)"
[ "$action" == "check" ] || [ "$action" == "test" ]
''; '';
specialisation.failingCheck.configuration.system.preSwitchChecks.failEveryTime = '' specialisation.failingCheck.configuration.system.preSwitchChecks.failEveryTime = ''
echo this will fail echo this will fail

View file

@ -139,7 +139,7 @@ fn parse_os_release() -> Result<HashMap<String, String>> {
})) }))
} }
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 mut cmd_split = command.split_whitespace();
let Some(argv0) = cmd_split.next() else { let Some(argv0) = cmd_split.next() else {
bail!("missing first argument in install bootloader commands"); 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) match std::process::Command::new(argv0)
.args(cmd_split.collect::<Vec<&str>>()) .args(cmd_split.collect::<Vec<&str>>())
.arg(toplevel) .arg(toplevel)
.arg::<&str>(action.into())
.spawn() .spawn()
.map(|mut child| child.wait()) .map(|mut child| child.wait())
{ {
@ -1053,7 +1054,7 @@ fn do_system_switch(action: Action) -> anyhow::Result<()> {
.unwrap_or_default() .unwrap_or_default()
!= "1" != "1"
{ {
do_pre_switch_check(&pre_switch_check, &toplevel)?; do_pre_switch_check(&pre_switch_check, &toplevel, action)?;
} }
if *action == Action::Check { if *action == Action::Check {