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

nixos/switch-to-configuration: correctly handle template instances with '.' in name #286202

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions nixos/modules/system/activation/switch-to-configuration.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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";
}
Expand Down
8 changes: 6 additions & 2 deletions nixos/tests/switch-test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ in {
wantedBy = [ "multi-user.target" ];
overrideStrategy = "asDropin";
};
systemd.services."[email protected]" = {
wantedBy = [ "multi-user.target" ];
overrideStrategy = "asDropin";
};
};

unitWithTemplateModified.configuration = {
Expand Down Expand Up @@ -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: [email protected], [email protected]\n")
assert_contains(out, "stopping the following units: [email protected], 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: [email protected], [email protected]\n")
assert_contains(out, "\nstarting the following units: [email protected], instantiated@three.with.dot.service, instantiated@two.service\n")
assert_lacks(out, "the following new units were started:")

with subtest("failing units"):
Expand Down
2 changes: 1 addition & 1 deletion pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down