How to Make a Python Script Unkillable on NixOS | Tried Running as Root but Failed

Salam everybody,

I am working on a Python script to block any NSFW (Not Safe For Work) content by taking screenshots at intervals. My main goal is to make it impossible for me to kill or disable the script, as I struggle with a porn addiction.

Approaches I tried but failed:

  1. Running the script as a service under the root account: This caused problems with the Grim tool used in my code for taking screenshots on Hyprland.

What is the most robust approach that is reliable on a NixOS & Hyprland setup?

Additional Context:

  • I have an accountable partner who has all the root and sudo passwords.
  • I’m a beginner. I found some suggestions about running the code like a kernel module, but that seems very complex for me.

Project File Tree:

.
├── main.py
├── nsfw_mobilenet2.224x224.h5
├── poetry.lock
├── pyproject.toml
├── README.md
├── run.sh
└── shell.nix

1 directory, 7 files

Note: This is for personal use, so I don’t intend to publish or package it.

Any advice or guidance would be greatly appreciated!

Have you considered running the python script under systemd with a timer?

Things to overcome:

  1. on timer tick establish that your user has a graphical session
  2. export the wayland socket and other bits
  3. run grim
1 Like

I think running as root, or any other user that isn’t yours, is a way to go. You probably need the process to switch to your user and inherit your compositor’s environment to take screenshots.

So first try to figure out the environment used by your compositor, copy that environment and then use sudo (or any other tool like that of your choice) to run grim.

Edit: You don’t even need to switch to the right user I think. I just tried the following as root and it worked:

env WAYLAND_DISPLAY=/run/user/1000/wayland-1 grim

So you just need to figure out $XDG_RUNTIME_DIR and $WAYLAND_DISPLAY of your user.

1 Like

Thanks a lot for all of these replies, I will try them and return.

Thanks a lot, Man I can’t say how you helped me, Thanks again.