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<name>.wantedBy = ""; 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

This systemd ticket also has some useful information.

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

Description=TLP suspend/resume




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 If you trace it through, the only place that systemdsleepdir is used is here. Patching is not without precedent as NixOS/systemd has a patched for similar reasons.

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


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

Uhh, there’s 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


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

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.