system.autoUpgrade failed to run

It seems using the wrong path of nixos-rebuild. This is my investigation:

My flake is located in ~/mynixosrepo/flakes/x86_64/flake.nix
This is part of the config in ~/mynixosrepo/hosts/machine1/configuration.nix

nix.package = pkgs.nixVersions.nix_2_19;
nix.settings.experimental-features = [ "nix-command" "flakes" ];
system.autoUpgrade = {
	enable = true;
	allowReboot = false;
	flake = inputs.self.outPath;
	flags = [
		"--update-input" "nixpkgs"
	dates = "Sat,Tue,Thu *-*-* 04:40:00";
	randomizedDelaySec = "45min";

I found the nixos-upgrade.service failed with the error message

nixos-upgrade-start[472019]: /nix/store/y9921x7sj1p7za2a9rm7jwxg6q3baxdy-nixos-rebuild/bin/nixos-rebuild: line 174: /nix/store/zlws4p5azdj27g1nmri6k1c53bzv0psc-source/bin/nixos-rebuild: No such file or directory

Then I type sudo systemctl cat nixos-upgrade and find the script path in ExecStart, then I cat that path and get the script:

set -e
/nix/store/y9921x7sj1p7za2a9rm7jwxg6q3baxdy-nixos-rebuild/bin/nixos-rebuild switch --update-input nixpkgs -L --flake /nix/store/k4qmifsqnwx8lcx5mfivgbzj821nc96p-source/flakes/x86_64 --upgrade

Then I copied the last line and run it in terminal appended with -vv and this is the error message:

$ nix --extra-experimental-features nix-command flakes build --out-link /tmp/nixos-rebuild.yknAV5/nixos-rebuild /nix/store/k4qmifsqnwx8lcx5mfivgbzj821nc96p-source/flakes/x86_64#nixosConfigurations."machine1" -L -vv --update-input nixpkgs
warning: '--update-input' is a deprecated alias for 'flake update' and will be removed in a future version.
$ exec /nix/store/k4qmifsqnwx8lcx5mfivgbzj821nc96p-source/bin/nixos-rebuild switch --update-input nixpkgs -L --flake /nix/store/k4qmifsqnwx8lcx5mfivgbzj821nc96p-source/flakes/x86_64 --upgrade -vv
/nix/store/y9921x7sj1p7za2a9rm7jwxg6q3baxdy-nixos-rebuild/bin/nixos-rebuild: line 174: /nix/store/k4qmifsqnwx8lcx5mfivgbzj821nc96p-source/bin/nixos-rebuild: No such file or directory

The $ exec /nix/store/k4qmifsqnwx8lcx5mfivgbzj821nc96p-source/bin/nixos-rebuild shows that it is no longer using /nix/store/y9921x7sj1p7za2a9rm7jwxg6q3baxdy-nixos-rebuild/bin/nixos-rebuild.

How can I fix this? Thanks for your help!

Solved by:

  1. Move ~/mynixosrepo/flakes/x86_64/flake.nix to ~/mynixosrepo/flake.nix (have to combine all architecture into 1 flake.nix, sad).
  2. Change system.autoUpgrade.flake = inputs.self.outPath; to system.autoUpgrade.flake = "path:${rootPath}";
  3. Set rootPath = ./. in flake.nix and pass it to specialArgs as suggested here.

Sorry I didn’t metion it is a git repo.