Replacement for nixos-upgrade when using a /etc/nixos/flake.nix

I have successfully migrated my configuration.nix to flake.nix,except for 1 detail: the nixos-upgrade.service
I have replaced with


  systemd.services.nixos-upgrade={
    startAt="03:00";
    serviceConfig.ExecStart=
      "${nix}/bin/nix shell --recreate-lock-file --commit-lock-file /etc/nixos#nixosConfigurations.nixos.toplevel -c switch-to-configuration switch";
  };

but it does not seem to create boot configurations.What is a command that updates flake.lock,create a boot entry and switches to the new config?

1 Like

I am very late but maybe it is still interesting for you:

I am using

  system.autoUpgrade = {
    enable = true;
    flags = ["--update-input" "nixpkgs" "--commit-lock-file"];
    # this tricks nixos-rebuild into using /etc/nixos/flake.nix
    flake = "''";
  };

and that generates the nixos-upgrade.{service,timer} for me. The resulting command in the systemd unit is nixos-rebuild switch --update-input nixpkgs --commit-lock-file --flake ''.

As far as I can tell this switches to the new configuration and also generates a boot entry for me.

BTW I am using nixos unstable in my system flake:

  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
2 Likes

Ok, I’ve changed the config for auto updates since then Now it is:

  systemd.services.nixos-upgrade = {
    startAt = "19:30";
    serviceConfig = rec{
      CPUWeight = 20;
      IOWeight = CPUWeight;
    };
    path = [ nix config.system.build.nixos-rebuild pkgs.git ];
    script = ''
      cd /etc/nixos

      sysLink(){
        readlink /nix/var/nix/profiles/system
      }
      prev=$(sysLink)

      gg(){
        git commit -am "nixos-upgrade: $(date)"
      }
      fixup(){
        #sometimes nixos-rebuild exit with 1
        #even when build is successful if some
        #servoce dodn't restart successfully
        if [[ $prev = $(sysLink) ]]
        then
          #update did really fail
          git restore .
        else
          gg
        fi
      }
      trap fixup err

      nix flake update .
      nixos-rebuild switch --flake .
      gg
    '';
  };