Unverified Commit 7ea2eb1f authored by K900's avatar K900 Committed by GitHub
Browse files

Revert "nixos/auto-upgrade: fix for systems without initrd/kernel" (#476320)

parents 21a961e6 f62bcc9d
Loading
Loading
Loading
Loading
+116 −209
Original line number Diff line number Diff line
@@ -114,27 +114,6 @@ in
        '';
      };

      rebootTriggers = lib.mkOption {
        type = lib.types.listOf lib.types.pathInStore;
        default = [ ];
        description = ''
          List of derivations that will cause an auto-reboot when changed when
          {option}`system.autoUpgrade.allowReboot` is set to true.
        '';
        defaultText = lib.literalExpression ''
          [
            (config.system.build.initialRamdisk or null)
            (config.system.build.kernel or null)
            config.hardware.firmware
            (pkgs.writeTextFile {
              name = "kernel-params";
              text = lib.concatStringsSep " " config.boot.kernelParams;
            })
          ]
          ++ config.system.switch.inhibitors
        '';
      };

      randomizedDelaySec = lib.mkOption {
        default = "0";
        type = lib.types.str;
@@ -218,33 +197,8 @@ in

  };

  config = lib.mkMerge [
  config = lib.mkIf cfg.enable {

    {
      system = {
        autoUpgrade.rebootTriggers = [
          (config.system.build.initialRamdisk or null)
          (config.system.build.kernel or null)
          config.hardware.firmware
          (pkgs.writeTextFile {
            name = "kernel-params";
            text = lib.concatStringsSep " " config.boot.kernelParams;
          })
        ]
        ++ config.system.switch.inhibitors;

        systemBuilderCommands = ''
          ln -s ${config.system.build.rebootTriggers} $out/reboot-triggers
        '';

        build.rebootTriggers = pkgs.writeTextFile {
          name = "reboot-triggers";
          text = lib.concatMapStringsSep "\n" (drv: drv.outPath) config.system.autoUpgrade.rebootTriggers;
        };
      };
    }

    (lib.mkIf cfg.enable {
    assertions = [
      {
        assertion = !((cfg.channel != null) && (cfg.flake != null));
@@ -301,44 +255,31 @@ in
        gitMinimal
        config.nix.package.out
        config.programs.ssh.package
          config.system.build.nixos-rebuild
          config.systemd.package
      ];

      script =
        let
            upgradeFlag = lib.optional (cfg.channel == null && cfg.flake == null) "--upgrade";
          nixos-rebuild = "${config.system.build.nixos-rebuild}/bin/nixos-rebuild";
          date = "${pkgs.coreutils}/bin/date";
          readlink = "${pkgs.coreutils}/bin/readlink";
          shutdown = "${config.systemd.package}/bin/shutdown";
          upgradeFlag = lib.optional (cfg.channel == null && cfg.upgrade) "--upgrade";
        in
        if cfg.allowReboot then
            # bash
          ''
              echo "Running nixos-rebuild boot..."
              new_configuration="$(
                # For some reason we still get a newline here in the journal between the
                # nixos-rebuild stderr output and us echoing the store path that was
                # printed on stdout.
                # This might have to do with the particular way in which systemd handles
                # stdout/stderr, they are unix sockets and not normal streams.
                store_path="$(nixos-rebuild boot ${toString (cfg.flags ++ upgradeFlag)})"
                echo "$store_path" >&2
                echo "$store_path"
              )"
              if [ -z "$new_configuration" ]; then
                echo "Looks like nixos-rebuild failed... Aborting"
                exit 1
              fi
              echo "New configuration is $new_configuration"
              switch_to_new_configuration="$new_configuration"/bin/switch-to-configuration
            ${nixos-rebuild} boot ${toString (cfg.flags ++ upgradeFlag)}
            booted="$(${readlink} /run/booted-system/{initrd,kernel,kernel-modules})"
            built="$(${readlink} /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"

              ${lib.optionalString (cfg.rebootWindow != null) # bash
                ''
                  current_time="$(date +%H:%M)"
            ${lib.optionalString (cfg.rebootWindow != null) ''
              current_time="$(${date} +%H:%M)"

              lower="${cfg.rebootWindow.lower}"
              upper="${cfg.rebootWindow.upper}"

              if [[ "''${lower}" < "''${upper}" ]]; then
                    if [[ "''${current_time}" > "''${lower}" ]] && [[ "''${current_time}" < "''${upper}" ]]; then
                if [[ "''${current_time}" > "''${lower}" ]] && \
                   [[ "''${current_time}" < "''${upper}" ]]; then
                  do_reboot="true"
                else
                  do_reboot="false"
@@ -346,61 +287,28 @@ in
              else
                # lower > upper, so we are crossing midnight (e.g. lower=23h, upper=6h)
                # we want to reboot if cur > 23h or cur < 6h
                    if [[ "''${current_time}" < "''${upper}" ]] || [[ "''${current_time}" > "''${lower}" ]]; then
                if [[ "''${current_time}" < "''${upper}" ]] || \
                   [[ "''${current_time}" > "''${lower}" ]]; then
                  do_reboot="true"
                else
                  do_reboot="false"
                fi
              fi
                ''
              }

              booted_triggers="$(realpath /run/booted-system)/reboot-triggers"
              booted_triggers_sha="$(
                if [ -f "$booted_triggers" ]; then
                  sha256sum - < "$booted_triggers"
                else
                  echo 'none'
                fi
              )"
            ''}

              new_triggers="$(realpath "$new_configuration")/reboot-triggers"
              new_triggers_sha="$(
                if [ -f "$new_triggers" ]; then
                  sha256sum - < "$new_triggers"
                else
                  echo 'none'
                fi
              )"

              ${lib.optionalString (cfg.operation == "switch") # bash
                ''
                  echo "Running switch-to-configuration check..."
                  if "$switch_to_new_configuration" check; then
                    echo "Checking reboot triggers..."
                    if [ "$new_triggers_sha" == "$booted_triggers_sha" ]; then
                      echo "Switching into the new generation..."
                      "$switch_to_new_configuration" ${cfg.operation}
                      exit 0
                    fi
                  fi
                ''
              }
              ${lib.optionalString (cfg.rebootWindow != null) # bash
                ''
                  if [ "''${do_reboot}" != true ]; then
            if [ "''${booted}" = "''${built}" ]; then
              ${nixos-rebuild} ${cfg.operation} ${toString cfg.flags}
            ${lib.optionalString (cfg.rebootWindow != null) ''
              elif [ "''${do_reboot}" != true ]; then
                echo "Outside of configured reboot window, skipping."
                    exit 0
            ''}
            else
              ${shutdown} -r +1
            fi
          ''
              }
              echo "Scheduling a reboot to activate the new generation"
              systemctl reboot --when="+2min"
            ''
        else
            # bash
          ''
              nixos-rebuild ${cfg.operation} ${toString (cfg.flags ++ upgradeFlag)}
            ${nixos-rebuild} ${cfg.operation} ${toString (cfg.flags ++ upgradeFlag)}
          '';

      startAt = cfg.dates;
@@ -416,7 +324,6 @@ in
        Persistent = cfg.persistent;
      };
    };
    })
  };

  ];
}