Referencing runtime closures in NixOS config

I’m toying with the somewhat novel idea of running least-privileged/hardened services. One of the cooler options systemd exposes is RootDirectory, which runs the service in a chroot where paths can then be bind-mounted into. Most of the examples (or, the one on the wiki, anyway) shows using builtins.storeDir to just mount the entire Nix store, which is fine, but limiting to exactly what a service requires would be better.

TLDR: I basically want nix-store -q --requisites ${pkgs.somepackage} during build time for the above usecase.

Also! While closures give everything needed for a program itself, it (understandably) does not include special stuff like generated systemd pre-start scripts. The Jellyfin module, for example, uses one to create declarative transcode settings. That could technically be overridden if there isn’t a way to reference it… but is there?

From everything I can tell after a little more digging, Nix has no clue about runtime closures until those paths actually exist in the store. For the usecase of referring to one in a systemd unit, though, overrides are basically a perfect fit.

The way I went about it was to bash together a helper script:

#!/usr/bin/env bash

#
# Creates an override for a service that contains BindReadOnlyPaths definitions for its runtime
# dependencies/closure.
#

service_name="$1"
service_path="$2"

override_dir="$XDG_CONFIG_HOME/systemd/user/${service_name}.service.d"
chroot_dir="$XDG_RUNTIME_DIR/${service_name}"

mkdir -p "$override_dir"
echo "[Service]" > "$override_dir/store-paths.conf"

for store_path in $(nix-store -q --requisites $service_path); do
  echo "BindReadOnlyPaths=$store_path" >> "$override_dir/store-paths.conf"
done

systemctl --user daemon-reload

Then, use it in a pre-start service (vesktop and home-manager syntax as an example):

  # ...

  systemd.user.services.vesktop-prestart = {
    Unit = {
      Description = "Setup paths for Vesktop";
      PartOf = [
        "vesktop.service"
      ];
    };
    Service = {
      Type = "oneshot";
      ExecStart = [
        # Or whatever you want to name the script
        "${pkgs.bash}/bin/bash -c '${../runtime-closure-service-override.sh} vesktop ${pkgs.vesktop}'"

        "${pkgs.coreutils}/bin/mkdir -p %t/vesktop%C"
        "${pkgs.coreutils}/bin/mkdir -p %t/vesktop%D"
      ];
    };
  };

And that’s pretty much it!

I haven’t tried it yet, but running it as an activation script is probably cleaner. Also, the script could definitely be expanded to look over the main service file and find any other store paths not apart of the closure.

Check out the systemd-confinement module, it builds a service override file at build time using pkgs.closureInfo for all the paths that need to be mounted on top of an empty RootDirectory=. That appears to be exactly what you are looking for.

module: https://github.com/NixOS/nixpkgs/blob/fe13242281a67641c0102cdb1087f0ebe586c7df/nixos/modules/security/systemd-confinement.nix
manual: Appendix A. Configuration Options

Note that this comes with some complications for things like service reloads, as for example configuration files and mounted ssl root certs can’t be updated because the new ones aren’t visible in the service’s sandbox.

Also note that activationScripts are going away, see Replacing activationScripts - Official NixOS Wiki

Huh, well that’s basically exactly what I would’ve been using in the first place. Neat.

This 101% seems like it should have an excerpt of its own in that wiki page I referenced. Also would be cool to see a home-manager/hjem variant of it, too… Suppose if nothing else, I’ll look into both those at some point.

While I’m here, another bit of context for GUI apps in particular: Including graphics drivers is a bit more involved given their inherent impurity and such. jail.nix’s approach basically just crawls /run/opengl-driver and mounts everything it finds.