From 4c2764c69c094d6917ff857aa4667f8886ef8d5e Mon Sep 17 00:00:00 2001 From: Stig Palmquist Date: Thu, 4 Aug 2022 00:08:26 +0200 Subject: nixos/switch-to-configuration: replace Net::DBus with busctl Call dbus by using `$cur_systemd/busctl --json=...` and core modules JSON::PP and IPC::Cmd to slim down dependencies for baseSystem. perlPackages.NetDBus pulls in quite a few other dependencies, like XML::Twig, LWP, and HTTP::Daemon. These are not really neccecary for s-t-c, and some of them have caused issues particularly with cross builds after updates to perlPackages. --- .../system/activation/switch-to-configuration.pl | 28 +++++++++++++++++----- nixos/modules/system/activation/top-level.nix | 2 +- 2 files changed, 23 insertions(+), 7 deletions(-) (limited to 'nixos/modules/system') diff --git a/nixos/modules/system/activation/switch-to-configuration.pl b/nixos/modules/system/activation/switch-to-configuration.pl index 3f0d976e70b8..f39549db883d 100755 --- a/nixos/modules/system/activation/switch-to-configuration.pl +++ b/nixos/modules/system/activation/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); @@ -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; } diff --git a/nixos/modules/system/activation/top-level.nix b/nixos/modules/system/activation/top-level.nix index 84f560691fc4..0da3c855fb4e 100644 --- a/nixos/modules/system/activation/top-level.nix +++ b/nixos/modules/system/activation/top-level.nix @@ -124,7 +124,7 @@ let configurationName = config.boot.loader.grub.configurationName; # Needed by switch-to-configuration. - perl = pkgs.perl.withPackages (p: with p; [ ConfigIniFiles FileSlurp NetDBus ]); + perl = pkgs.perl.withPackages (p: with p; [ ConfigIniFiles FileSlurp ]); }; # Handle assertions and warnings -- cgit 1.4.1