Btrbk: how to snapshot two different subvolume?

I have a btrbk schedule to snapshot my /home subvolume.
I want to create one to snapshot my /var subvolume.
Can I schedule two separate snapshot jobs like that?
If not, how to run a manual snapshot for /var?

#BTRFS automatic snapshots of Home
  services.btrbk.instances."btrbk" = {
    onCalendar = "*:0";
    settings = {
      snapshot_preserve_min = "7d";
      volume."/" = {
        subvolume = "/home";
        snapshot_dir = ".snapshots";
       };
    };
  };

It looks like a manual btrbk snapshot would be controlled by the btrbk.config file, and the btrbk.config file is controlled by the configuration.nix file.
Can’t figure out the nix syntax for two discrete snapshots. It doesn’t seem to be documented.
What’s the simplest way to snapshot two different subvolumes?

So I figured out how to do it, using a more rudimentary configuration:

environment.etc = {
    "btrbk.conf".text = ''
      snapshot_preserve_min   7d
      volume /
        snapshot_dir .snapshots
        subvolume home
        subvolume var   
    '';
  };
  
  systemd.services.btrfs-snapshot = {
    startAt = "*:0";
    enable = true;
    path = with pkgs; [btrbk];
    serviceConfig.Type = "oneshot";
    script = ''
      mkdir -p /.snapshots
      btrbk run
    '';
  };

It is necessary to add pkgs.btrbk under environment.systemPackages if you want to manually run a snapshot from the terminal.

Much thanks to @solene for both configs

What was the error when you did not have btrk in environment.systemPackages? It should be available for the systemd service.

Adding it to systemPackages should only be useful if you want to use it in a shell.

1 Like
  services.btrbk.instances."btrbk" = {
    onCalendar = "*:0";
    settings = {
      snapshot_preserve_min = "7d";
      volume."/" = {
        subvolume = {
          "/home" = {},
          "/var" = {};
          };
        snapshot_dir = ".snapshots";
       };
    };
  };

should do the trick.

I did sudo btrbk run in terminal to test it out, and it said that it could not find btrbk
So that is like you were saying. I will edit my post.

error:
       … while evaluating the attribute 'config'

         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:322:9:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          323|         _module = checked (config._module);

       … while calling the 'seq' builtin

         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: syntax error, unexpected ',', expecting ':' or '@'

       at /etc/nixos/configuration.nix:202:23:

          201|         subvolume = {
          202|           "/home" = {},
             |                       ^
          203|           "/var" = {};
building Nix...
error:
       … while evaluating the attribute 'config'

         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:322:9:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          323|         _module = checked (config._module);

       … while calling the 'seq' builtin

         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: syntax error, unexpected ',', expecting ':' or '@'

       at /etc/nixos/configuration.nix:202:23:

          201|         subvolume = {
          202|           "/home" = {},
             |                       ^
          203|           "/var" = {};
building the system configuration...
error:
       … while evaluating the attribute 'config.system.build.toplevel'

         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:322:9:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          323|         _module = checked (config._module);

       … while calling the 'seq' builtin

         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: syntax error, unexpected ',', expecting ':' or '@'

       at /etc/nixos/configuration.nix:202:23:

          201|         subvolume = {
          202|           "/home" = {},
             |                       ^
          203|           "/var" = {};

I made a typo, there is a comma instead of a semicolon