SENix: Towards SELinux on NixOS
Over the past few months, I’ve been working on integrating SELinux support into NixOS, and I’ve finally gotten to a point where I would consider it “somewhat presentable” (though notably still highly experimental).
Much work & research has already been done in the past by @RossComputerGuy, @ElvishJerricco, and @numinit, to name a few, and my work largely builds on that.
Some parts of the project (mainly the dev tooling side of things) are pretty much exclusively LLM code, while other parts, and especially the design decisions, are not. For a full writeup on some of the technical design decisions & motivation behind the project, I encourage you to refer to the project’s readme.
The system policy is fully declarative, and is built as its own derivation. This has the advantage that it’s impossible to switch to a system configuration with an invalid policy. Another benefit is that certain parts of the policy can be compiled in on-demand, for example to only include rules for thunderbird if config.programs.thunderbird.enable is set to true. Likewise, this also allows us to include full Nix store paths for the relevant apps, instead of having to rely on wildcards such as /nix/store/[^/]+/bin/thunderbird.
In order to allow us to label files in the Nix store without actually modifying file metadata, and to work around the fact that files that should receive different labels may be hardlinks of one another, a custom FUSE filesystem is employed that keeps xattrs in memory and presents each path as a unique inode. Though it still forwards hardlink ops to the underlying filesystem, it’s recommended to not run with auto-optimise-store, as this is likely to result in a lot of superfluous hardlink requests.
Eventually the goal is to upstream this, but there’s still some work to be done before that becomes feasible. In the meantime, the flake is ready for use in VMs & test machines, so at a minimum you can use this to hack around and get a feel for what it SELinux in NixOS could be like at some point in the future.