From cccd3c092427272de438105e0e4251a7c99e3a79 Mon Sep 17 00:00:00 2001 From: Jack Wilsdon Date: Sun, 4 Feb 2024 00:15:33 +0000 Subject: [PATCH 1/2] nixos/switch-to-configuration: correctly handle template instances with '.' in name --- .../modules/system/activation/switch-to-configuration.pl | 6 +++--- nixos/tests/switch-test.nix | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/nixos/modules/system/activation/switch-to-configuration.pl b/nixos/modules/system/activation/switch-to-configuration.pl index 4beca4f0a42a9..05ec443a33e41 100755 --- a/nixos/modules/system/activation/switch-to-configuration.pl +++ b/nixos/modules/system/activation/switch-to-configuration.pl @@ -579,7 +579,7 @@ sub handle_modified_unit { ## no critic(Subroutines::ProhibitManyArgs, Subroutin my $new_base_unit_file = $new_unit_file; # Detect template instances. - if (!-e $cur_unit_file && !-e $new_unit_file && $unit =~ /^(.*)@[^\.]*\.(.*)$/msx) { + if (!-e $cur_unit_file && !-e $new_unit_file && $unit =~ /^(.*)@.*\.(.*)$/msx) { $base_unit = "$1\@.$2"; $cur_base_unit_file = "/etc/systemd/system/$base_unit"; $new_base_unit_file = "$toplevel/etc/systemd/system/$base_unit"; @@ -762,7 +762,7 @@ sub filter_units { my $new_base_unit_file = $new_unit_file; # Detect template instances. - if (!-e $new_unit_file && $unit =~ /^(.*)@[^\.]*\.(.*)$/msx) { + if (!-e $new_unit_file && $unit =~ /^(.*)@.*\.(.*)$/msx) { $base_unit = "$1\@.$2"; $new_base_unit_file = "$toplevel/etc/systemd/system/$base_unit"; } @@ -835,7 +835,7 @@ sub filter_units { my $new_base_unit_file = $new_unit_file; # Detect template instances. - if (!-e $new_unit_file && $unit =~ /^(.*)@[^\.]*\.(.*)$/msx) { + if (!-e $new_unit_file && $unit =~ /^(.*)@.*\.(.*)$/msx) { $base_unit = "$1\@.$2"; $new_base_unit_file = "$toplevel/etc/systemd/system/$base_unit"; } diff --git a/nixos/tests/switch-test.nix b/nixos/tests/switch-test.nix index e00c22b816820..a59b0ce1e7b62 100644 --- a/nixos/tests/switch-test.nix +++ b/nixos/tests/switch-test.nix @@ -336,6 +336,10 @@ in { wantedBy = [ "multi-user.target" ]; overrideStrategy = "asDropin"; }; + systemd.services."instantiated@three.with.dot" = { + wantedBy = [ "multi-user.target" ]; + overrideStrategy = "asDropin"; + }; }; unitWithTemplateModified.configuration = { @@ -954,11 +958,11 @@ in { # Ensure templated units are restarted when the base unit changes switch_to_specialisation("${machine}", "unitWithTemplate") out = switch_to_specialisation("${machine}", "unitWithTemplateModified") - assert_contains(out, "stopping the following units: instantiated@one.service, instantiated@two.service\n") + assert_contains(out, "stopping the following units: instantiated@one.service, instantiated@three.with.dot.service, instantiated@two.service\n") assert_lacks(out, "NOT restarting the following changed units:") assert_lacks(out, "reloading the following units:") assert_lacks(out, "\nrestarting the following units:") - assert_contains(out, "\nstarting the following units: instantiated@one.service, instantiated@two.service\n") + assert_contains(out, "\nstarting the following units: instantiated@one.service, instantiated@three.with.dot.service, instantiated@two.service\n") assert_lacks(out, "the following new units were started:") with subtest("failing units"): From b46fde32cda7e5d80760435cdd5ff790b6af01b4 Mon Sep 17 00:00:00 2001 From: Jack Wilsdon Date: Sat, 26 Oct 2024 12:20:13 +0100 Subject: [PATCH 2/2] switch-to-configuration-ng: correctly handle template instances with '.' in name --- pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 61932cb55591c..399ad7653b86a 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 @@ -1108,7 +1108,7 @@ won't take effect until you reboot the system. let current_active_units = get_active_units(&systemd)?; - let template_unit_re = Regex::new(r"^(.*)@[^\.]*\.(.*)$") + let template_unit_re = Regex::new(r"^(.*)@.*\.(.*)$") .context("Invalid regex for matching systemd template units")?; let unit_name_re = Regex::new(r"^(.*)\.[[:lower:]]*$") .context("Invalid regex for matching systemd unit names")?;