Pkgsrc 很酷 (2022)
Pkgsrc Is Cool (2022)

原始链接: https://www.wisellama.rocks/posts/2021-12-23-pkgsrc-is-cool/

作者赞扬了 NetBSD 的高移植性软件包管理器 **pkgsrc**,称其通过将已安装软件与基础操作系统分离,从而保持系统的整洁。与通常会弄乱 `/bin` 或 `/lib` 等系统目录的标准软件包管理器不同,pkgsrc 将软件安装在独立的子目录中(例如 `/usr/pkg` 或用户的主目录)。 除了在各种类 Unix 系统(Linux、macOS、Solaris)上的高移植性外,作者还强调了从源代码构建软件包的优势。Pkgsrc 本质上是一系列 Makefile 的集合,它从去中心化的位置获取源代码(“distfiles”),有效地将软件包管理与单一中央服务器解耦。 对作者而言,其最大的吸引力在于“非特权模式”,该模式允许构建一个无需 root 权限的自定义本地用户环境。此外,作者还强调了该项目的韧性,并指出由于 pkgsrc 使用去中心化的源并支持镜像重定向,因此完全可以在离线状态下构建软件。这使得 pkgsrc 成为那些重视自主性、系统整洁度以及构建自托管、独立软件环境能力的用户的理想工具。

本次讨论围绕 **pkgsrc** 的优点展开,这是一个由 NetBSD 社区开发的跨平台软件包管理系统。 用户称赞 pkgsrc 的可移植性,指出它允许在各种架构上安装最新软件,而无需迁移整个操作系统。其一个主要亮点是“非特权模式”(unprivileged mode),该模式允许用户在其主目录下维护一个自定义的、隔离的用户空间——这一功能被比作一种轻量级、非隔离的容器化形式。 技术讨论还探讨了 pkgsrc 背后的哲学,即作为一个 Makefile 存储库,自动执行从源代码下载、打补丁和构建软件的过程。虽然一些参与者质疑与手动编译相比,该系统的开销较大,但其他人认为精心维护的补丁管理才是其最有价值的资产。此外,讨论还涉及了“离线韧性”的重要性,用户表示希望在软件基础设施上保持本地控制权,以作为抵御潜在互联网碎片化和未来审查的保障。
相关文章

原文
pkgsrc Is Cool

Published: 2021-12-23 22:17:43 -0800 PST

Last updated: 2022-04-24 21:06:24 -0700 PDT

I think I saw something about pkgsrc pop up randomly in my Mastodon feed a few months ago. Before that, I had never heard of it. And now…well now I’m currently writing these words in a version of Emacs that I built from source with pkgsrc.

Pkgsrc is NetBSD’s package manager, but thanks to NetBSD’s portability philosophy, pkgsrc is fully supported as a package manager on other Unix-like operating systems like Linux, Mac, and Solaris. It supports installing binary packages, but the reason I was interested in it is that pkgsrc supports building packages from source as well.

For some context on other similar projects, pkgsrc was originally forked from FreeBSD’s Ports collection decages ago, so for people familiar with the other BSD’s, this is a ports collection that works on other platforms. Even better, pkgsrc and NetBSD get the terminology right and correctly call these things packages instead of ports (and leave “port” as the term for cpu architecture support). Other similar-ish projects include MacPorts, Gentoo’s emerge, GNU Guix, and Ravenports. I probably missed some, but you get the point. There are multiple options out there if you’re interested. I’m not going to compare/contrast them because honestly the only other one I’ve ever used was MacPorts like 10 years ago when I didn’t know what I was doing.

Why did I use it?

I’ve always disliked needing to install a new package to get some program to run. The main reason why is that the new package could pull in other packages and they all end up polluting my system folders like /bin and /lib with extra stuff. Half the time I delete the program I was testing out anyway, but then I’m stuck with this extra stuff in my base system. I never really even stopped to think that there might be way to separate out the “base system” from the other crap I’ve installed along the way.

But this is exactly what NetBSD does. NetBSD’s base system for /bin and such is really only for the core NetBSD system code like the stuff you can build from their source code repo. Anything else is supposed to be installed somewhere else. By default, pkgsrc will install stuff into /usr/pkg where it created its own subdirectory tree of /usr/pkg/bin and /usr/pkg/lib and so on. This way that specfic pkg directory is like a self-contained userland separate from the base operating system. I’m actually using pkgsrc on Debian rather than NetBSD itself, but it works the same way.

Even better, pkgsrc lets you use it in unprivileged mode and install stuff locally in your user’s home directory like ~/pkg. This can give your user their own separate custom userland from other users. Depending on your use case this may not be what you want, but I found it particularly cool that I could run almost a completely separate system just for my user. Because I didn’t want to need sudo/doas to install packages, I am actually using pkgsrc in unprivileged mode with ~/pkg as my base directory.

So that was a long explanation to this answer. Why did I start using pkgsrc? Because I can install packages locally in my home directory, I can build those packages from source, and I know this system will work on any operating system I will ever run into (and I’ve used some weird ones considering how relatively young I am).

How it works

Pkgsrc isn’t really a package repository itself. It is simply a collection of Makefiles that define how to build each package as well as the dependencies needed for a package. Each package’s Makefile defines where to download the source code for the package, how to apply any necessary patches, and then how to build it. This actually answered a long-standing question for me: why do GitHub “releases” build a tarball/zip snapshot of the source code? Well, systems like pkgsrc will use that tarball to download the source code for a specific version without needing to clone a full repository (or even needing the source control software installed at all).

Pkgsrc reaches out to various websites like GitHub and Sourceforge (or wherever else a project’s source code is hosted) to download the specified version of a package to build it. This is highly decentralized because pkgsrc does not rely on a single repository server, instead relying on each project’s own servers. (As a random side note, this is similar to how the Go programming language handles dependencies). This could lead to the potential problem where a single project’s website is down so you can’t build their software. However, the Makefiles also specify alternative distfile mirrors for where to download the source code. New specific terminology for me here was a “distfile” which is that source code tarball snapshot for a specific version of the package.

Tinfoil Hat Time

One potentially “irrational” requirement of mine was that I wanted to be able to install software from my own network in case the internet ever goes down. Not just my internet, but THE internet. And I know that the internet as whole will probably never really go down, but I do think it may get to the point where the internet is fragmented into completely different silos because of government censorship.

In a more practical manner, I also wanted to be able to install software from my local network to save bandwidth when updating multiple computers. This is why I host some mirrors on my server.

One of those mirrors is the pkgsrc distfile mirror. I was able to use pkgsrc with the MASTER_SITE_OVERRIDE option to tell it to use my own distfile mirror. This allows me to build all of the supported pkgsrc packages offline because the distfile mirror has already reached out and downloaded all of the source code for every project (or rather, I’m mirroring from a mirror that has already done that).

I realize this is a weird almost “prepper’ doomsday mentality, but I really wanted to be able to install software locally on my own network. Pkgsrc gave me the solution to be able to build the software from source for any operating system on any cpu architecture that I could find and do it all from my own server.

After searching for more information on pkgsrc, I found these other cool links:

Followup

To keep this from ballooning into one gigantic article, I’ll keep this one as simply why I think pkgsrc is cool and why I wanted to use it. I’ll follow up with another article about how exactly I’m using it.

I should probably also write another article about using Clang as the default pkgsrc compiler since I messed around with that until I finally got it to work. Their documentation about it is a bit scattered and misleading.

I did end up writing more posts about pkgsrc!

Git - Mastodon

www.wisellama.rocks by Wisellama is marked with CC0 1.0
联系我们 contact @ memedata.com