1 { config, lib, pkgs, ... }:
4 cfg = config.security.polkit;
12 security.polkit.enable = lib.mkEnableOption "polkit";
14 security.polkit.package = lib.mkPackageOption pkgs "polkit" { };
16 security.polkit.debug = lib.mkEnableOption "debug logs from polkit. This is required in order to see log messages from rule definitions";
18 security.polkit.extraConfig = lib.mkOption {
19 type = lib.types.lines;
23 /* Log authorization checks. */
24 polkit.addRule(function(action, subject) {
25 // Make sure to set { security.polkit.debug = true; } in configuration.nix
26 polkit.log("user " + subject.user + " is attempting action " + action.id + " from PID " + subject.pid);
29 /* Allow any local user to do anything (dangerous!). */
30 polkit.addRule(function(action, subject) {
31 if (subject.local) return "yes";
36 Any polkit rules to be added to config (in JavaScript ;-). See:
37 <https://www.freedesktop.org/software/polkit/docs/latest/polkit.8.html#polkit-rules>
41 security.polkit.adminIdentities = lib.mkOption {
42 type = lib.types.listOf lib.types.str;
43 default = [ "unix-group:wheel" ];
44 example = [ "unix-user:alice" "unix-group:admin" ];
47 Specifies which users are considered “administrators”, for those
48 actions that require the user to authenticate as an
49 administrator (i.e. have an `auth_admin`
50 value). By default, this is all users in the `wheel` group.
57 config = lib.mkIf cfg.enable {
59 environment.systemPackages = [ cfg.package.bin cfg.package.out ];
61 systemd.packages = [ cfg.package.out ];
63 systemd.services.polkit.serviceConfig.ExecStart = [
65 "${cfg.package.out}/lib/polkit-1/polkitd ${lib.optionalString (!cfg.debug) "--no-debug"}"
68 systemd.services.polkit.restartTriggers = [ config.system.path ];
69 systemd.services.polkit.stopIfChanged = false;
71 # The polkit daemon reads action/rule files
72 environment.pathsToLink = [ "/share/polkit-1" ];
74 # PolKit rules for NixOS.
75 environment.etc."polkit-1/rules.d/10-nixos.rules".text =
77 polkit.addAdminRule(function(action, subject) {
78 return [${lib.concatStringsSep ", " (map (i: "\"${i}\"") cfg.adminIdentities)}];
82 ''; #TODO: validation on compilation (at least against typos)
84 services.dbus.packages = [ cfg.package.out ];
86 security.pam.services.polkit-1 = {};
93 source = "${cfg.package.bin}/bin/pkexec";
95 polkit-agent-helper-1 =
99 source = "${cfg.package.out}/lib/polkit-1/polkit-agent-helper-1";
103 systemd.tmpfiles.rules = [
104 # Probably no more needed, clean up
105 "R /var/lib/polkit-1"
106 "R /var/lib/PolicyKit"
109 users.users.polkituser = {
110 description = "PolKit daemon";
111 uid = config.ids.uids.polkituser;
112 group = "polkituser";
115 users.groups.polkituser = {};