[Solved] Shutdown command dosn't run in a sytemd service

I have read the above answer, but it didn’t work for me.
So now I have this systemd service working correctly -Thanks to ALLAH- :


#The-p*r-blocker--------------------------------#

 systemd.services.pornblocker = {
    description = "PornBlocker";
    after = [ "multi-user.target" ];

    serviceConfig = {
      Type = "simple";
      ExecStart = "/home/mwalid/Security/PornBlocker/run.sh";
      User = "mwalid";
      WorkingDirectory = "/home/mwalid/Security/PornBlocker";
      Restart = "always";
      EnvironmentFile="/home/mwalid/Security/PornBlocker/env";
      PermissionsStartOnly=true;
    };

    wantedBy = [ "multi-user.target" ];
    path = [ pkgs.bash pkgs.nix ];
  };

 security.polkit = { # This  is the snippet that didn't work for me wich is suggested by the answer that I referred to it above.
  extraConfig = ''
    polkit.addRule(function(action, subject) {
        if (action.id == "org.freedesktop.systemd1.manage-units" ||
            action.id == "org.freedesktop.systemd1.manage-unit-files") {
            if (action.lookup("unit") == "poweroff.target") {
                return polkit.Result.YES;
            }
        }
    });
  '';
};
#----------------------------------------------#

But the only problem is : The Python script has this line:

        subprocess.run(['sudo', 'systemctl', 'poweroff'])

And this line don’t execute at all because of this error :

Jun 20 13:55:24 mohamedwalid-laptop shutdown[26283]: Call to PowerOff failed: Interactive authentication required.

Even though I edited the sudoers file, also didn’t work at all.


Any help will be appreciated, Thanks in advance.

Thanks to ALLAH , I solved the problem by these tow changes :

User = "root"; 
PermissionsStartOnly=true;