website/content/posts/installing-every-arch-package.md
Anthony Wang a2372d6b8a
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Start working on Julia code
2022-01-29 22:38:01 -06:00

3 KiB

title date draft description type tags
Installing Every Arch Package 2022-01-26T21:52:58-06:00 true Using algorithms and Julia to install as many packages as possible from the Arch Linux official repositories post
linux
fun
algorithms
computer-science

A stupid idea on Matrix

Challenge accepted. Let's do it!

First things first, let's generate a list of all official Arch Linux packages. Fortunately, pacman, the best pragmatic package manager in existence, makes this a breeze.

pacman -Sql

Great, now let's install it all!

pacman -Sql | xargs sudo pacman -S

10 seconds later, you'll find yourself with... unresolvable package conflicts detected?

OK, fine, let's disable dependency checking then:

pacman -Sql | xargs sudo pacman -Sdd

Nope, didn't work. We have to do something about the conflicting packages!

We could resolve all the conflicts manually with an hour of work... or we could write a program!

Automation

Time for some algorithms!

It's time to put our algorithms knowledge to good use. This is just a graph We can think of each package as a node in a graph and each conflict is an edge. Since we don't care about dependency checks (which would make for a likely broken system), we don't need to add any other edges to the graph.

For each edge, we need to pick at most one package, but not both. That sounds a lot like a maximum independent set!

Wait... it's NP hard though? And we have up to 12000 nodes, so we'll never be able to find the answer before the heat death of the universe, right?

Well, do we have 12000 connected nodes? No, since the largest connected component is probably only a few nodes. We aren't going to have hundreds or thousands of packages all conflicting with each other.

Implementing this in Julia

We're going to use Julia for implementing this algorithm, since Julia is Python but better. We first need to get a list of all packages:

packages = split(read(`pacman -Sql`, String))
n = length(packages)
idx = Dict(packages[i] => i for i = 1:n)

Now, we'll get info about each package:

struct Package
    provides::Vector{String}
    conflicts::Vector{String}
    size::Float64
end

info = Vector{Package}()

Threads.@threads for i = 1:n
    package = packages[i]
    r = map(x -> split(split(x, "\n")[1]), split(read(`pacman -Si $package`, String), " : "))
    push!(info, Package(r[10], r[13], parse(Float64, r[16][1])))
end

We need special handling for virtual packages:

virtual = Dict{String, Vector{String}}()
for i = 1:n
	for p in info[i].provides
		if p not in virtual
			virtual[packages[i]] = Vector{String}()
		end
		push!(virtual[packages[i]], packages[i])
	end
end

We can use this to construct the graph:

graph = [Vector{Int}() for i = 1:n]