How to automatically update flakes

I want to check daily for new versions of flake, but upgrade them the next time I do nixos-rebuild. I did some looking up and didn’t find anything in particular. I could use systemd to run nix flake update daily.

system.autoUpgrade includes flake options.

Here’s my config

Edit: sorry, realized you actually asked about updating the flake and not the system. I run [this service] (nixos/hosts/office/services/nixos.nix at 38e6b7b410fe6fb3ecb8bada64a2efc88b3e6669 · firecat53/nixos · GitHub) on my desktop to update flake.lock. That gets propagated to the other machines with Syncthing. I know a lot of people keep flake.lock in version control, but I’ve found it works better for my use case to not track that since I don’t have hard requirements for pinning exact versions.

How could I use system.autoUpgrade to auto update the lock file though?

That’s why I edited my comment to show a flake-update service. It’s two separate operations.

Could I do both, autoupgrade and update it?

You have to update the lock file first and then upgrade. That’s just the way flakes work. Notice in the service for nixos-upgrade.service that it is required to run after the flake-update service.

This doesn’t seem to work for me, does it look right? I don’t get entries in sudo journalctl -u nixos-ugprade or sudo journalctl -u flake-update.

  ## Update flake inputs daily
  systemd.services = {
    flake-update = {
      preStart = "/run/current-system/sw/bin/nm-online";
      unitConfig = {
        Description = "Update flake inputs";
        StartLimitIntervalSec = 300;
        StartLimitBurst = 5;
      };
      serviceConfig = {
        ExecStart = "${pkgs.nix}/bin/nix flake update --flake /home/yousuf/.config/nix";
        Restart = "on-failure";
        RestartSec = "30";
        Type = "oneshot"; # Ensure that it finishes before starting nixos-upgrade
        User = "myusername";
      };
      before = [ "nixos-upgrade.service" ];
      path = [
        pkgs.nix
        pkgs.git
        pkgs.host
      ];
    };
  };

  system.autoUpgrade = {
    enable = true;
    flake = "path:///home/yousuf/.config/nix";
    flags = [ "-L" ];
    dates = "0:00";
    randomizedDelaySec = "45min";
  };