Understanding risk vector for building of nixpkgs

Hi, nix noob here,

I’ve spent the last while constructing a NixOS desktop environment that I like. Recently it has been brought to my attention that certain package managers support pre and post install scripts which appear to cause a myriad of attack vectors for malware, but also support developers immensely. I was wondering if:

a) nix has build script features, and

b) do build scripts run as root or a privileged user?

c) if both of the above are “yes”, does nix have anything preventing me from getting a rootkit from just doing a environment.systempackages = ...; or thing.enable = true;???

Thanks for your support!

Additional context:
Using the NixOS linux distribution (x86_64 I believe) with its built in package manager. Everything custom configured.

The pre- and post-install scripts are no more of an attack vector than the software you’re building. They’re just a convenient place to put stuff people in the npm world don’t pay attention to; if you have sufficient access to change those scripts, you can also change the software itself to be malware. I wouldn’t treat them as an attack vector separate from anything that runs malicious code at build time.

Nix technically doesn’t have such scripts, but nix also doesn’t have a default build behavior since it isn’t tied to a specific language ecosystem. All of nix’ building is one big equivalent of the postinstall script (though nixpkgs has mechanisms to split this script into parts for coding convenience). It is entirely sandboxed, but the point of this sandbox is explicitly not securing the host from what the script runs, so running malware in it is still inadvisable.

Ultimately these scripts aren’t why npm keeps having malware infestations, though. They only pose a risk to build servers which wouldn’t otherwise execute the software being built; if they didn’t exist people would just abuse the test infrastructure.


All of that said, with the focus on specific config options out of the way, nix is nearly as vulnerable to running malicious code as part of a build, but nixpkgs isn’t quite the same as npm; nixpkgs has much tighter controls. It’s probably still possible to slip something through, but every line is reviewed both by peers and one of a few hundred committers. With npm this doesn’t happen, as the published projects are treated as artifacts, not as a big project that should be subject to strict code review.

That said, nix builds many npm projects, and many malicious pre- and post-install scripts would still be executed on build machines. Reviewers would never see the ones hidden in innocuous npm dependency bumps, and there is no culture of inspection and responsible consumption of upstreams like what debian has.

The sandbox would thwart simple attacks, but in practice a dedicated supply chain attack against nixpkgs is probably only a little harder than against npm, although it most likely wouldn’t happen through nix code, but ironically through a trojan npm project.


As for your specific questions, I’ll answer them, but given the above you probably appreciate that they’re somewhat nonsensical. You’re focusing on the wrong problem.

Nix doesn’t really have non-build-script features.

No, but escaping sandboxes is feasible and local user privilege escalation CVEs are a dime a dozen, so this doesn’t thwart dedicated attacks.

Code review and distribution maintenance. This question is a bit silly; if a distribution, regardless of whether it’s debian or NixOS, accidentally ships malware, you will get that malware, regardless of how it builds its software.

Your system never executes the build scripts, so the rest of your questions are completely unrelated to this question, but it does execute the binaries in the packages themselves, so the question itself remains valid.

If you distrust the distro packages you cannot ever run any Linux distro (or even Windows), regardless of the build tooling they use. You’re literally downloading software from the internet and executing it, and unless you somehow inspect millions of lines of code by hand and rebuild locally you can only trust that nobody made that software do something malicious.

Everything is “vulnerable” to someone substituting malware somewhere in the stack, the question is how likely that is to happen, not the specific line of code they change.

And yeah, software security is exactly the house of cards it sounds like.