How to Disable Suspend on Laptop Lid Close on NixOS: A Comprehensive Guide

As a dedicated user of NixOS, you’re likely familiar with its unique approach to system configuration. The immutability of the Nix store, while providing unparalleled reproducibility, can present challenges when attempting seemingly straightforward tasks, such as disabling automatic suspension when closing your laptop lid. The conventional method of directly editing /etc/systemd/logind.conf, as suggested in many online resources, simply doesn’t apply within the NixOS ecosystem due to the symbolic link structure and the read-only nature of the Nix store. Fear not, for a NixOS-idiomatic solution exists. This guide will provide you with the necessary steps to achieve your desired configuration, leveraging NixOS’s declarative configuration system.

Understanding the NixOS Way

Before diving into the specifics, it’s crucial to grasp the core philosophy behind NixOS. Instead of directly modifying system files, NixOS relies on a configuration file, typically located at /etc/nixos/configuration.nix, to define the desired state of the system. This configuration is then used to build a new system generation, ensuring consistency and reproducibility. Applying changes directly to files within the Nix store would violate this fundamental principle and could lead to an inconsistent and unmanageable system. The changes that we are applying are designed to be persistent across reboots.

Modifying the configuration.nix File

The key to disabling suspend on lid close in NixOS lies in modifying your configuration.nix file. Open this file with your preferred text editor (e.g., nano, vim, emacs) as a user with sudo privileges. You’ll be adding lines to this file that tell NixOS how to configure the systemd login manager. This is the core of the NixOS configuration, which affects how the system handles user sessions, power management, and other essential settings.

sudo nano /etc/nixos/configuration.nix

Locate the section within your configuration.nix file where you define systemd settings. If such a section does not yet exist, you can add it. Within this section, you will add the configuration options to modify the behavior of the systemd-logind service.

Configuring systemd.services.logind.settings

The crucial setting you need to modify is within the systemd.services.logind.settings attribute. This allows you to override specific settings within the logind.conf file without directly editing it. Add or modify the following lines within your configuration.nix file:

  systemd.services.logind.settings = {
    HandleLidSwitch = "ignore";
    HandleLidSwitchExternalPower = "ignore";
    HandleLidSwitchDocked = "ignore";
  };

Let’s break down what each of these lines does:

  • HandleLidSwitch = "ignore";: This setting instructs systemd-logind to ignore the lid switch event when the laptop is running on battery power. The system will no longer automatically suspend or hibernate when the lid is closed.

  • HandleLidSwitchExternalPower = "ignore";: This setting ensures that the lid switch is also ignored when the laptop is connected to an external power source (e.g., plugged into a wall outlet). This prevents the system from suspending even when it’s charging.

  • HandleLidSwitchDocked = "ignore";: This directive tells systemd-logind to ignore the lid switch when the laptop is docked. This can be particularly useful in scenarios where you use your laptop as a desktop replacement while docked, allowing you to close the lid without interrupting your workflow.

These three settings collectively cover the most common scenarios where you might want to prevent automatic suspension on lid close. By setting them all to "ignore", you ensure that the system remains active regardless of the power source or docking status.

Applying the Configuration Changes

After modifying your configuration.nix file, you need to apply the changes and rebuild your system. This is achieved using the nixos-rebuild command. Run the following command in your terminal:

sudo nixos-rebuild switch

This command will:

  1. Read your updated configuration.nix file.
  2. Determine the necessary changes to bring your system to the desired state.
  3. Build a new system generation in the Nix store.
  4. Switch your system to the new generation.

The switch argument ensures that the changes are applied immediately, without requiring a reboot. However, it is always a good practice to reboot your system after major configuration changes to ensure that everything is working as expected. The rebuilding process may take some time, depending on the complexity of your configuration and the speed of your hardware.

Verifying the Changes

After the rebuild process is complete, it’s essential to verify that the changes have been applied successfully. You can do this by closing your laptop lid and observing whether the system suspends. If you followed the steps correctly, the system should remain active.

You can further confirm the changes by inspecting the active logind.conf settings. While you shouldn’t directly edit the file in the Nix store, you can view the effective configuration by running the following command:

systemd-analyze cat-config systemd/logind.conf

This command will output the merged configuration from all relevant logind.conf files, including any overrides defined in your configuration.nix. You should see the HandleLidSwitch, HandleLidSwitchExternalPower, and HandleLidSwitchDocked settings set to ignore.

Alternative Configurations and Considerations

While setting all three HandleLidSwitch options to "ignore" is the most straightforward approach to disabling suspend on lid close, you may want to explore alternative configurations to fine-tune the behavior to your specific needs.

Conditional Disabling Based on Battery State

You might want to disable suspend only when the laptop is plugged in, allowing it to suspend on battery to conserve power. In this case, you would set HandleLidSwitchExternalPower = "ignore"; and leave HandleLidSwitch at its default value (typically suspend). This will cause the system to suspend on lid close when running on battery but remain active when plugged in.

  systemd.services.logind.settings = {
    HandleLidSwitch = "suspend"; # Or the default value
    HandleLidSwitchExternalPower = "ignore";
    HandleLidSwitchDocked = "ignore";
  };

Using HandleLidSwitchDocked for Docking Stations

If you frequently use your laptop with a docking station, you can use the HandleLidSwitchDocked setting to prevent suspend only when the laptop is docked. This allows you to close the lid without interrupting your work while using an external monitor and keyboard.

  systemd.services.logind.settings = {
    HandleLidSwitch = "suspend"; # Or the default value
    HandleLidSwitchExternalPower = "suspend"; # Or the default value
    HandleLidSwitchDocked = "ignore";
  };

Understanding Default Values

It is important to understand the default values for these settings, as they may vary depending on your system configuration and desktop environment. The default value for HandleLidSwitch is typically suspend, but it could also be hibernate or lock, depending on your system settings.

To determine the default value for your system, you can consult the systemd-logind.service man page or inspect the default logind.conf file in the Nix store (though you shouldn’t edit it).

Graphical Configuration Tools

While this guide focuses on the NixOS-idiomatic approach using configuration.nix, some desktop environments may provide graphical tools for configuring power management settings. However, it is crucial to understand that these tools may not always be fully compatible with NixOS’s declarative configuration system. Changes made through graphical tools might be overwritten by the settings in your configuration.nix file. Therefore, we highly recommend using the configuration.nix method for consistent and reliable configuration.

Troubleshooting

If you encounter issues after applying the changes, consider the following troubleshooting steps:

  1. Double-Check Syntax: Ensure that the syntax in your configuration.nix file is correct. Even a small typo can prevent the system from building correctly. Use a Nix linter or syntax checker to verify your configuration.

  2. Review Error Messages: Carefully examine the output of the nixos-rebuild command for any error messages. These messages can provide valuable clues about the cause of the problem.

  3. Consult the NixOS Wiki: The NixOS wiki is an excellent resource for troubleshooting common issues and finding solutions to specific problems.

  4. Seek Community Support: If you’re still unable to resolve the issue, don’t hesitate to seek help from the NixOS community. You can ask questions on the NixOS forums, IRC channels, or other online communities.

  5. Reboot Your System: While the switch argument is designed to apply changes immediately, a reboot can sometimes be necessary to fully activate the new configuration, especially after making significant changes to systemd settings.

Conclusion

Disabling suspend on lid close in NixOS requires a slightly different approach compared to other Linux distributions due to its unique configuration system. By modifying your configuration.nix file and rebuilding your system, you can achieve the desired behavior in a NixOS-idiomatic way. This guide has provided you with a comprehensive overview of the process, including alternative configurations and troubleshooting tips. By following these steps, you can customize your NixOS system to perfectly suit your needs. Remember to always prioritize the declarative configuration approach, as it ensures consistency and reproducibility in your NixOS environment. We hope this article on revWhiteShadow helps you to understand and configure your system to your liking.