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

nixos/switch-to-configuration: Fix backslashes in unit names

systemd needs this so special characters (like the ones in wireguard
units that appear because they are part of base64) can be escaped using
the \x syntax.

Root of the issue is that `glob()` handles the backslash internally
which is obviously not what we want here.

Also add a test case and fix some perlcritic issues in the subroutine.
This commit is contained in:
Janne Heß 2022-02-17 12:46:00 +01:00
parent b516e55d9d
commit 3617ecb67f
No known key found for this signature in database
GPG key ID: 69165158F05265DF
2 changed files with 51 additions and 10 deletions

View file

@ -145,6 +145,23 @@ import ./make-test-python.nix ({ pkgs, ...} : {
systemd.services.test.serviceConfig."X-Test" = "test";
};
unitWithBackslash.configuration = {
systemd.services."escaped\\x2ddash" = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
ExecReload = "${pkgs.coreutils}/bin/true";
};
};
};
unitWithBackslashModified.configuration = {
imports = [ unitWithBackslash.configuration ];
systemd.services."escaped\\x2ddash".serviceConfig.X-Test = "test";
};
restart-and-reload-by-activation-script.configuration = {
systemd.services = rec {
simple-service = {
@ -433,6 +450,25 @@ import ./make-test-python.nix ({ pkgs, ...} : {
assert_lacks(out, "as well:")
assert_contains(out, "would start the following units: test.service\n")
# Ensure \ works in unit names
out = switch_to_specialisation("${machine}", "unitWithBackslash")
assert_contains(out, "stopping the following units: test.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_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: escaped\\x2ddash.service\n")
assert_lacks(out, "as well:")
out = switch_to_specialisation("${machine}", "unitWithBackslashModified")
assert_contains(out, "stopping the following units: escaped\\x2ddash.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: escaped\\x2ddash.service\n")
assert_lacks(out, "the following new units were started:")
assert_lacks(out, "as well:")
with subtest("failing units"):
# Let the simple service fail
switch_to_specialisation("${machine}", "simpleServiceModified")