How to add a file to `${pkgs.systemd}/lib/systemd/system-sleep/`?

I need to add a file to ${pkgs.systemd}/lib/systemd/system-sleep/ in order to rmmod / modprobe some kernel modules on sleep / wake.

(See the systemd-sleep docs for more info.)

I’ve had a look through the module and the derivation and can’t spot anything obvious.

At the moment the only idea I’ve got is to override the systemd derivation…

I will also admit to being somewhat of a beginner when it comes to Nix / NixOS, so any help is greatly appreciated!


edit: somehow systemd.generator-packages and systemd/cryptsetup-generator.nix feel like they might be getting me in the right direction…

I did that once, and it did exactly what I needed, but it invalidated the binary cache and I couldn’t afford rebuilding the world all the time back in the days.

Can’t you just write a systemd unit and hook it up with systemd.services.<name>.wantedBy = "systemd-suspend.target"; or something?

1 Like

I think I can just set systemd.package = myAwesomeSystemd, the commit adding it indicates it’s there to make it ‘cheaper to test a new systemd’.

Thanks, that’s a good suggestion, and one that I didn’t give enough thought to, it is however not entirely entirely straightforward as there is no wakeup.target.

This systemd ticket also has some useful information.

Looking through those tickets, the best solution might be to adopt the method used by TLP:

[Unit]
Description=TLP suspend/resume
Before=sleep.target
StopWhenUnneeded=yes


[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/path/to/sleep.sh
ExecStop=/path/to/wake.sh

[Install]
WantedBy=sleep.target

source

The next question is how to abort sleep (for example if an rmmod is unsuccesful – else my laptop won’t wake from sleep). I think the exit code from scripts in lib/systemd/system-sleep is able to do this… otherwise I’m not sure, perhaps I’d need to look at systemd-inhibit.


Ultimately though, the idea of placing “local use only” scripts in /usr/lib/systemd/system-sleep is a bit daft, and is perhaps more problematic on NixOS then elsewhere.

We could patch systemd.pc.in? If you trace it through, the only place that systemdsleepdir is used is here. Patching systemd.pc.in is not without precedent as NixOS/systemd has a patched systemd.pc.in for similar reasons.

We could then add suitable options to the systemd module to allow setting your own sleep scripts.

Thoughts?

Sweet goodness! Wish I knew that back in the days. =/

Uhh, there’s post-resume.target. And powerManagement.resumeCommands.

TLP method looks relatively sane anyway. Will failing such a service abort sleep?

I’ve also found a relevant discussion about hooking up to /usr/lib/systemd/system-sleep back from 2014: Add systemd system sleep hook dir by coreyoconnor · Pull Request #5162 · NixOS/nixpkgs · GitHub

The conclusion was along those lines: An suitably equivalent interface to system-sleep hooks can be made that is a one-shot service.

1 Like

Thanks!

So I’ve got several avenues of approach now… from least to most intrusive…

  1. Use powerManagement.powerDownCommnands and powerManagement.resumeCommands (see this example for the FaceTime HD camera).
  1. Write my own unit files

  2. Use a custom systemd via systemd.package by either:

    a. Adding a file to lib/systemd/system-sleep (symlinkJoin to the rescue…?)

    b. Patching systemd.pc.in

Hopefully the first option will be all that is necessary, I’m tempted to have a go with all of them as an exercise in hacking on NixOS.

I’ll try and report back with my successes or failures.