Creating directories and files declararively

I need to make sure that there’s directory exists in /var filesystem before some sort of service will start. Is it possible to define, which directory(or file) I want to create and which permissions and owner I want to set for it, inside .nix config files?


If you want /var/lib/foo, /var/cache/foo, or /var/log/foo, your want to use StateDirectory, CacheDirectory, or LogsDirectory, as well as StateDirectoryMode, etc…

systemd.myservice.serviceConfig = {
  User = "user";
  Group = "group";
  StateDirectory = "foo";
  StateDirectoryMode = "0750";

If you want something directory under /var, like /var/foo then you’re looking for systemd.tmpfiles.rules:

systemd.tmpfiles.rules = [
  "d /var/foo 0755 user group"

See systemd.exec and tmpfiles.d for details.