···22 config,
33 lib,
44 pkgs,
55- self,
65 ...
76}:
87let
98 flake = "github:lassulus/superconfig";
1091111- # Timestamp of the flake revision this system was built from, baked into the
1212- # closure so the running system knows how old its own source is.
1313- stampFile = "/run/current-system/source-lastModified";
1414-1510 # system.autoUpgrade switches to whatever the flake currently evaluates to,
1611 # forwards or backwards. A machine deployed from an unpushed local checkout
1712 # therefore gets reverted to the older remote HEAD on the next timer run.
1818- # Refuse to move backwards in time.
1313+ # Refuse to move backwards in time: only upgrade to a revision committed
1414+ # after the running system was deployed. The deploy time is when its profile
1515+ # generation was created (/run/current-system's mtime if no generation points
1616+ # at it); the source's own lastModified is no use, since `clan machines
1717+ # update` builds from a path: flake, which has none.
1918 guard = pkgs.writeShellApplication {
2019 name = "autoupgrade-forward-only";
2120 runtimeInputs = [
···2423 pkgs.jq
2524 ];
2625 text = ''
2727- stamp=''${1:-${stampFile}}
2828- current=$(cat "$stamp" 2>/dev/null || echo 0)
2626+ system=$(readlink -f /run/current-system)
2727+ deployed=0
2828+ for link in /nix/var/nix/profiles/system-*-link; do
2929+ if [ "$(readlink -f "$link")" = "$system" ]; then
3030+ created=$(stat -c %Y "$link")
3131+ if [ "$created" -gt "$deployed" ]; then deployed=$created; fi
3232+ fi
3333+ done
3434+ if [ "$deployed" -eq 0 ]; then deployed=$(stat -c %Y /run/current-system); fi
2935 if ! meta=$(nix flake metadata --refresh --json ${lib.escapeShellArg flake}); then
3036 echo "autoupgrade: cannot reach ${flake}; letting nixos-upgrade report the error"
3137 exit 0
3238 fi
3339 remote=$(jq -r '.lastModified // 0' <<<"$meta")
3434- if [ "$remote" -gt "$current" ]; then
4040+ if [ "$remote" -gt "$deployed" ]; then
3541 exit 0
3642 fi
3737- echo "autoupgrade: ${flake} is at $remote, running system is at $current; skipping"
4343+ echo "autoupgrade: ${flake} is at $remote, running system was deployed at $deployed; skipping"
3844 exit 1
3945 '';
4046 };
···4551 inherit flake;
4652 randomizedDelaySec = "6h";
4753 };
4848-4949- system.extraSystemBuilderCmds = ''
5050- echo -n ${toString (self.lastModified or 0)} > $out/source-lastModified
5151- '';
52545355 systemd.services.nixos-upgrade.serviceConfig.ExecCondition = lib.getExe guard;
5456}