fix activation script

This commit is contained in:
Charlotte 🦝 Delenk 2022-09-27 07:49:06 +01:00
parent ffeff5fa1a
commit 5c7c16a3b6
Signed by: darkkirb
GPG key ID: AB2BD8DAF2E37122

View file

@ -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;
}