# slock as a system service, so the OOM killer leaves it alone, started # by its user through polkit. With enstate, the password hash can come # from the slot secrets/slock.hashed-password: then slock runs as its # user, with that hash. Without a hash, slock runs setuid root and reads # /etc/shadow, like nixpkgs' programs.slock. { config, lib, mylib, pkgs, ... }: let cfg = config.hrm.slock; slot = config.enstate.channels.secrets.slots."slock.hashed-password"; preload = pkgs.runCommandCC "slock-preload.so" {} /* sh */ '' gcc -Wall -Wextra -Werror -shared -fPIC -o $out ${./preload.c} ''; validate = pkgs.runCommandCC "slock-validate" { buildInputs = [ pkgs.libxcrypt ]; } /* sh */ '' gcc -Wall -Wextra -Werror -o $out ${./validate.c} -lcrypt ''; in { options.hrm.slock = { enable = lib.mkEnableOption "hrm.slock"; package = lib.mkOption { default = pkgs.writeDashBin "slock" '' set -efu display=''${DISPLAY#:} service=slock-$LOGNAME@$display.service exec ${pkgs.systemd}/bin/systemctl start "$service" ''; type = lib.types.package; }; user = lib.mkOption { type = mylib.types.user; }; }; config = lib.mkIf cfg.enable (lib.mkMerge [ { security.polkit.extraConfig = /* js */ '' polkit.addRule(function(action, subject) { if (action.id === "org.freedesktop.systemd1.manage-units" && subject.user === ${builtins.toJSON cfg.user.name} && /^slock-${cfg.user.name}@[0-9]+\.service$/.test(action.lookup("unit")) ) { return polkit.Result.YES; } }); ''; security.wrappers.slock = { setuid = true; owner = "root"; group = "root"; source = "${pkgs.slock}/bin/slock"; }; systemd.services."slock-${cfg.user.name}@" = { environment = { DISPLAY = ":%I"; }; restartIfChanged = false; serviceConfig = { ExecStart = pkgs.writeDash "slock-start" '' if test "$(${pkgs.coreutils}/bin/cat "$CREDENTIALS_DIRECTORY/hashed-password")" != none; then LD_PRELOAD=${preload} exec ${pkgs.slock}/bin/slock fi # Setuid slock opens the display as root. ${pkgs.xhost}/bin/xhost +SI:localuser:root >/dev/null exec /run/wrappers/bin/slock ''; # The fallback when there's no hash: LoadCredential comes first. SetCredential = [ "hashed-password:none" ]; OOMScoreAdjust = -1000; Restart = "on-failure"; RestartSec = "100ms"; StartLimitBurst = 0; SyslogIdentifier = "slock"; User = cfg.user.name; }; }; } (lib.mkIf config.enstate.enable { # A hash the validator objects to is left out, so slock runs setuid. enstate.types."text/x-crypt-hash".validator = validate; enstate.channels.secrets.slots."slock.hashed-password" = { type = "text/x-crypt-hash"; required = false; secret = true; }; systemd.services."slock-${cfg.user.name}@".serviceConfig.LoadCredential = [ "hashed-password:${slot.path}" ]; }) ]); }