How to ensure that a directory exists via configuration.nix

Is there a way to create a root folder if it does not exist in configuraion.nix?

I found this 2017 post on the maillist, suggesting to use either ExecBefore or system.activationScripts. I cannot find any mentioning of ExecBefore in the github nixos repo.

I use btrbk service

  services.btrbk.instances."btrbk" = {
    onCalendar = "*:0:0:0";
    settings = {
      snapshot_preserve_min = "2d";
      snapshot_preserve = "48h 20d 6m";
      volume = {
        "/.subvols" = {
          subvolume = "@home";
          snapshot_dir = "snapshots";
        };
      };
    };
  };

How would you ensure /.subvols/snapshots exist? Like

system.activationScripts.script.text = ''
  mkdir -p /.subvols/snapshots
''
1 Like

systemd.tmpfiles.rules:

systemd.tmpfiles.rules = [
  "d /.subvols/snapshots <MODE> <USER> <GROUP>"
];

See also the manpage

1 Like