2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.security.polkit;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
security.polkit.enable = lib.mkEnableOption "polkit";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
security.polkit.package = lib.mkPackageOption pkgs "polkit" { };
|
2024-07-01 15:47:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
security.polkit.debug = lib.mkEnableOption "debug logs from polkit. This is required in order to see log messages from rule definitions";
|
2022-10-30 15:09:59 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
security.polkit.extraConfig = lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
|
|
|
example =
|
|
|
|
''
|
|
|
|
/* Log authorization checks. */
|
|
|
|
polkit.addRule(function(action, subject) {
|
2022-10-30 15:09:59 +00:00
|
|
|
// Make sure to set { security.polkit.debug = true; } in configuration.nix
|
2020-04-24 23:36:52 +00:00
|
|
|
polkit.log("user " + subject.user + " is attempting action " + action.id + " from PID " + subject.pid);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Allow any local user to do anything (dangerous!). */
|
|
|
|
polkit.addRule(function(action, subject) {
|
|
|
|
if (subject.local) return "yes";
|
|
|
|
});
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description =
|
2020-04-24 23:36:52 +00:00
|
|
|
''
|
|
|
|
Any polkit rules to be added to config (in JavaScript ;-). See:
|
2023-11-16 04:20:00 +00:00
|
|
|
<https://www.freedesktop.org/software/polkit/docs/latest/polkit.8.html#polkit-rules>
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
security.polkit.adminIdentities = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = [ "unix-group:wheel" ];
|
|
|
|
example = [ "unix-user:alice" "unix-group:admin" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description =
|
2020-04-24 23:36:52 +00:00
|
|
|
''
|
|
|
|
Specifies which users are considered “administrators”, for those
|
|
|
|
actions that require the user to authenticate as an
|
2022-08-12 12:06:08 +00:00
|
|
|
administrator (i.e. have an `auth_admin`
|
|
|
|
value). By default, this is all users in the `wheel` group.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
environment.systemPackages = [ cfg.package.bin cfg.package.out ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
systemd.packages = [ cfg.package.out ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-10-30 15:09:59 +00:00
|
|
|
systemd.services.polkit.serviceConfig.ExecStart = [
|
|
|
|
""
|
2024-09-19 14:19:46 +00:00
|
|
|
"${cfg.package.out}/lib/polkit-1/polkitd ${lib.optionalString (!cfg.debug) "--no-debug"}"
|
2022-10-30 15:09:59 +00:00
|
|
|
];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
systemd.services.polkit.restartTriggers = [ config.system.path ];
|
|
|
|
systemd.services.polkit.stopIfChanged = false;
|
|
|
|
|
|
|
|
# The polkit daemon reads action/rule files
|
|
|
|
environment.pathsToLink = [ "/share/polkit-1" ];
|
|
|
|
|
|
|
|
# PolKit rules for NixOS.
|
|
|
|
environment.etc."polkit-1/rules.d/10-nixos.rules".text =
|
|
|
|
''
|
|
|
|
polkit.addAdminRule(function(action, subject) {
|
2024-09-19 14:19:46 +00:00
|
|
|
return [${lib.concatStringsSep ", " (map (i: "\"${i}\"") cfg.adminIdentities)}];
|
2020-04-24 23:36:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
${cfg.extraConfig}
|
|
|
|
''; #TODO: validation on compilation (at least against typos)
|
|
|
|
|
2024-07-01 15:47:52 +00:00
|
|
|
services.dbus.packages = [ cfg.package.out ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
security.pam.services.polkit-1 = {};
|
|
|
|
|
|
|
|
security.wrappers = {
|
2021-09-22 15:38:15 +00:00
|
|
|
pkexec =
|
|
|
|
{ setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
2024-07-01 15:47:52 +00:00
|
|
|
source = "${cfg.package.bin}/bin/pkexec";
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
|
|
|
polkit-agent-helper-1 =
|
|
|
|
{ setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
2024-07-01 15:47:52 +00:00
|
|
|
source = "${cfg.package.out}/lib/polkit-1/polkit-agent-helper-1";
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
# Probably no more needed, clean up
|
|
|
|
"R /var/lib/polkit-1"
|
|
|
|
"R /var/lib/PolicyKit"
|
|
|
|
];
|
|
|
|
|
|
|
|
users.users.polkituser = {
|
|
|
|
description = "PolKit daemon";
|
|
|
|
uid = config.ids.uids.polkituser;
|
2021-08-08 23:34:03 +00:00
|
|
|
group = "polkituser";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2023-03-04 12:14:45 +00:00
|
|
|
users.groups.polkituser = {};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|