This commit is contained in:
Solene Rapenne 2022-05-05 10:27:09 +02:00
parent bc55ee79a9
commit a949ba65a6
5 changed files with 159 additions and 3 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright (c) 2022 Solène Rapenne
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
PREFIX=/usr/local
install:
install -o root -g wheel -m 555 pkgset.pl ${PREFIX}/sbin/pkgset
install -o root -g wheel -m 444 pkgset.1 ${PREFIX}/man/man1/pkgset.1

View File

@ -1,3 +1,20 @@
# pkgset
# Introduction
Manage your OpenBSD packages in a declarative way
`pkgset` is a simple utility to manage the packages installed on an OpenBSD system in a declarative way.
# What is it doing?
Create a file `/etc/pkgset.conf` or files in `/etc/pkgset.conf.d/` listing a package per line, `pkgset` will make sure no extra packages (not counting dependencies) are installed or missing compared to your list.
It does so by marking extra packages as "auto installed" and by installing missing packages, then run `pkg_delete -a` to get ride of unused packages (the one marked as auto installed) if they are not a dependency of another required package.
# How to install
- get the sources
- run `make install` as root
The documentation is available as a man page (see `man pkgset`).
# Why?
After using NixOS too long, it's just a must have for me to manage my packages this way.

52
pkgset.1 Normal file
View File

@ -0,0 +1,52 @@
.Dd $Mdocdate: May 5 2022 $
.Dt PKGSET 1
.Os
.Sh NAME
.Nm pkgset
.Nd manage system packages in a declarative way
.Sh SYNOPSIS
.Nm
.Sh DESCRIPTION
.Pp
.Nm
is a program to manage the installed packages on the system in a
declarative way.
.Pp
.Nm
will check the packages declared but not installed, and will install them.
Packages installed but not declared will be set as "auto
installed", meaning they will be deleted at the end of the process
if they are not a dependency of a listed packages.
.Sh FILES
Create a list of packages in
.Pa /etc/pkgset.conf
or add files to
.Pa /etc/pkgset.conf.d/
directory for include.
.Pp
Packages should be listed one per line and can be written in a format recognized by
.Xr pkg_add 1
, see BUGS for more explanations about flavors or version syntax.
.Sh EXAMPLES
The file
.Pa /etc/pkgset.conf
is used to define a few packages on this system, note the various syntax available
to define a package version or a flavor.
.Bd -literal -offset indent
borgbackup--%1.2
vim--no_x11
xfce
rlwrap
.Ed
.Sh SEE ALSO
.Xr pkg_add 1
.Sh HISTORY
This software tries to make OpenBSD more declarative and ease system administrator work.
.Sh AUTHORS
.An See the LICENSE file for the authors .
.Sh LICENSE
See the LICENSE file for the terms of redistribution.
.Sh BUGS
Some packages may not be installed if you don't specify the version or flavor, like vim or borgbackup because
.Xr pkg_add 1
will tell the package is ambiguous when looking for a match.

82
pkgset.pl Normal file
View File

@ -0,0 +1,82 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
# get the packages you want to have installed on the system
sub get_declared_packages()
{
my @pkg;
if ( -d "/etc/pkgset.conf.d/" ) {
my @result = `cat /etc/pkgset.conf.d/*`;
push @pkg, @result;
}
if ( -f "/etc/pkgset.conf" ) {
my @result = `cat /etc/pkgset.conf`;
push @pkg, @result;
}
for (@pkg) {
chomp;
if ($_ !~ m/--/) {
$_ .= "--";
}
}
push @pkg, "quirks--"; #mandatory package
# remove duplicates
my %hash = map { $_, 1 } @pkg;
return keys %hash;
}
# check what is currently installed
sub get_installed_packages()
{
my @pkg = `pkg_info -mz`;
for (@pkg) {
chomp;
if ($_ !~ m/--/) {
$_ .= "--";
}
}
return @pkg;
}
my @installed = get_installed_packages;
my @wanted = get_declared_packages;
my @autoinstall;
my @toinstall;
# check for installed
FIRST: for my $current (@installed) {
SECOND: for my $w (@wanted) {
if ($w eq $current) {
next FIRST;
}
}
push @autoinstall, $current;
}
# check for missing packages to install
FIRST: for my $w (@wanted) {
SECOND: for my $current (@installed) {
if ($w eq $current) {
next FIRST;
}
}
push @toinstall, $w;
}
for my $pkg (@autoinstall) {
print "Marking $pkg as auto installed\n";
`pkg_add -aa $pkg`;
}
for my $pkg (@toinstall) {
print "Installing $pkg\n";
`pkg_add -I $pkg`;
}
print "Running pkg_delete -a to delete unused dependencies (if any)\n";
`pkg_delete -a`;
print "Done\n";