From 5c7c16a3b6034b8ee7e40bd9d70d9597f85c6c96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charlotte=20=F0=9F=A6=9D=20Delenk?= Date: Tue, 27 Sep 2022 07:49:06 +0100 Subject: [PATCH] fix activation script --- extra/switch-to-configuration.pl | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/extra/switch-to-configuration.pl b/extra/switch-to-configuration.pl index 52f84b09..9c65fc2a 100644 --- a/extra/switch-to-configuration.pl +++ b/extra/switch-to-configuration.pl @@ -18,7 +18,8 @@ use Config::IniFiles; use File::Path qw(make_path); use File::Basename; use File::Slurp qw(read_file write_file edit_file); -use Net::DBus; +use JSON::PP; +use IPC::Cmd; use Sys::Syslog qw(:standard :macros); use Cwd qw(abs_path); @@ -91,7 +92,7 @@ make_path("/run/nixos", { mode => oct(755) }); openlog("nixos", "", LOG_USER); # Install or update the bootloader. -if ($action eq "boot") { +if ($action eq "switch") { chomp(my $install_boot_loader = <<'EOFBOOTLOADER'); @installBootLoader@ EOFBOOTLOADER @@ -124,12 +125,29 @@ EOF # virtual console 1 and we restart the "tty1" unit. $SIG{PIPE} = "IGNORE"; +# Replacement for Net::DBus that calls busctl of the current systemd, parses +# it's json output and returns the response using only core modules to reduce +# dependencies on perlPackages in baseSystem +sub busctl_call_systemd1_mgr { + my (@args) = @_; + my $cmd = [ + "$cur_systemd/busctl", "--json=short", "call", "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", + @args + ]; + + my ($ok, $err, undef, $stdout) = IPC::Cmd::run(command => $cmd); + die $err unless $ok; + + my $res = decode_json(join "", @$stdout); + return $res; +} + # Asks the currently running systemd instance via dbus which units are active. # Returns a hash where the key is the name of each unit and the value a hash # of load, state, substate. sub get_active_units { - my $mgr = Net::DBus->system->get_service("org.freedesktop.systemd1")->get_object("/org/freedesktop/systemd1"); - my $units = $mgr->ListUnitsByPatterns([], []); + my $units = busctl_call_systemd1_mgr("ListUnitsByPatterns", "asas", 0, 0)->{data}->[0]; my $res = {}; for my $item (@{$units}) { my ($id, $description, $load_state, $active_state, $sub_state, @@ -149,9 +167,7 @@ sub get_active_units { # Takes the name of the unit as an argument and returns a bool whether the unit is active or not. sub unit_is_active { my ($unit_name) = @_; - - my $mgr = Net::DBus->system->get_service("org.freedesktop.systemd1")->get_object("/org/freedesktop/systemd1"); - my $units = $mgr->ListUnitsByNames([$unit_name]); + my $units = busctl_call_systemd1_mgr("ListUnitsByNames", "as", 1, $unit_name)->{data}->[0]; if (scalar(@{$units}) == 0) { return 0; }