Unverified Commit 6b26d36b authored by Ramses's avatar Ramses Committed by GitHub
Browse files

nixos/power-management: Handle resume correctly with `sleep.target` (#507185)

parents 40c95f55 5513c368
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -149,6 +149,8 @@

- `services.uptime` has been removed because the package it relies on does not exist anymore in nixpkgs.

- `post-resume.target` has been removed. See {manpage}`systemd.special(7)` about `sleep.target` for instructions on ordering a process after resume with `ExecStop=`.

- `services.kubernetes.addons.dns.coredns` has been renamed to `services.kubernetes.addons.dns.corednsImage` and now expects a
package instead of attrs. Now, by default, nixpkgs.coredns in conjunction with dockerTools.buildImage is used, instead
of pulling the upstream container image from Docker Hub. If you want the old behavior, you can set:
+15 −42
Original line number Diff line number Diff line
@@ -90,68 +90,35 @@ in
      https://www.freedesktop.org/software/systemd/man/latest/systemd.special.html#sleep.target
    '';

    systemd.targets.post-resume = {
      description = "Post-Resume Actions";
      requires = [ "post-resume.service" ];
      after = [ "post-resume.service" ];
      wantedBy = [ "sleep.target" ];
      unitConfig.StopWhenUnneeded = true;
    };

    systemd.services = {
      # Service executed before suspending/hibernating.
      pre-sleep = {
        description = "Pre-Sleep Actions";
      sleep-actions = {
        description = "Sleep Actions";
        wantedBy = [ "sleep.target" ];
        before = [ "sleep.target" ];
        unitConfig.StopWhenUnneeded = true;
        script = ''
          # NixOS pre-sleep script

          # config.powerManagement.powerDownCommands
          ${cfg.powerDownCommands}
        '';
        serviceConfig.Type = "oneshot";
      };

      # Service executed after resuming from suspend/hibernate
      post-resume = {
        description = "Post-Resume Actions";
        # Pulled in by post-resume.service above
        after = [ "sleep.target" ];
        script = ''
        preStop = ''
          # NixOS pre-resume script

          /run/current-system/systemd/bin/systemctl try-restart --no-block post-resume.target

          # config.powerManagement.resumeCommands
          ${cfg.resumeCommands}

          # config.powerManagement.powerUpCommands
          ${cfg.powerUpCommands}
        '';
        serviceConfig.Type = "oneshot";
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
        };

      # Service executed before shutdown
      pre-shutdown = {
        description = "Pre-Shutdown Actions";
        wantedBy = [
          "shutdown.target"
        ];
        before = [
          "shutdown.target"
        ];
        script = ''
          # NixOS pre-shutdown script

          # config.powerManagement.powerDownCommands
          ${cfg.powerDownCommands}
        '';
        serviceConfig.Type = "oneshot";
        unitConfig.DefaultDependencies = false;
      };

      # Service executed after boot
      # Service executed after boot, and stopped during shutdown
      post-boot = {
        description = "Post-Boot Actions";
        # It's not well defined at what point in the bootup sequence this should run
@@ -167,6 +134,12 @@ in
          # config.powerManagement.powerUpCommands
          ${cfg.powerUpCommands}
        '';
        preStop = ''
          # NixOS pre-shutdown script

          # config.powerManagement.powerDownCommands
          ${cfg.powerDownCommands}
        '';
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
+16 −6
Original line number Diff line number Diff line
@@ -194,12 +194,7 @@ in
    systemd.services.undervolt = {
      description = "Intel Undervolting Service";

      # Apply undervolt on boot, nixos generation switch and resume
      wantedBy = [
        "multi-user.target"
        "post-resume.target"
      ];
      after = [ "post-resume.target" ]; # Not sure why but it won't work without this
      wantedBy = [ "multi-user.target" ];

      serviceConfig = {
        Type = "oneshot";
@@ -208,6 +203,21 @@ in
      };
    };

    systemd.services.undervolt-sleep = {
      description = "Preserve Intel Undervolting After Sleep";

      wantedBy = [ "sleep.target" ];
      before = [ "sleep.target" ];

      unitConfig.StopWhenUnneeded = true;
      serviceConfig = {
        Type = "oneshot";
        Restart = "no";
        RemainAfterExit = true;
        ExecStop = "${cfg.package}/bin/undervolt ${toString cliArgs}";
      };
    };

    systemd.timers.undervolt = lib.mkIf cfg.useTimer {
      description = "Undervolt timer to ensure voltage settings are always applied";
      partOf = [ "undervolt.service" ];