1
0
Fork 0
nixos-config/system/modules/loginctl-linger.nix

44 lines
1.3 KiB
Nix
Raw Normal View History

2018-06-06 22:36:55 +00:00
{ config, lib, pkgs, ... }:
# A temporary hack to `loginctl enable-linger $somebody` (for
# multiplexer sessions to last), until this one is unresolved:
# https://github.com/NixOS/nixpkgs/issues/3702
#
# Usage: `users.extraUsers.somebody.linger = true` or slt.
with lib;
let
dataDir = "/var/lib/systemd/linger";
2019-07-31 21:56:52 +00:00
lingeringUsers = map (u: u.name)
(attrValues (flip filterAttrs config.users.users (n: u: u.linger)));
2018-06-06 22:36:55 +00:00
2019-07-31 21:56:52 +00:00
lingeringUsersFile = builtins.toFile "lingering-users" (concatStrings (map
(s: ''
${s}
'') (sort (a: b: a < b)
lingeringUsers))); # this sorting is important for `comm` to work correctly
2018-06-06 22:36:55 +00:00
updateLingering = pkgs.writeScript "update-lingering" ''
# Stop when the system is not running, e.g. during nixos-install
[[ -e /run/booted-system ]] || exit 0
lingering=$(ls ${dataDir} 2> /dev/null | sort)
echo "$lingering" | comm -3 -1 ${lingeringUsersFile} - | xargs -r ${pkgs.systemd}/bin/loginctl disable-linger
echo "$lingering" | comm -3 -2 ${lingeringUsersFile} - | xargs -r ${pkgs.systemd}/bin/loginctl enable-linger
'';
2019-07-31 21:56:52 +00:00
in {
2018-06-06 22:36:55 +00:00
options = {
users.users = mkOption {
2019-07-31 21:56:52 +00:00
options = [{ linger = mkEnableOption "lingering for the user"; }];
2018-06-06 22:36:55 +00:00
};
};
config = {
system.activationScripts.update-lingering =
stringAfter [ "users" ] updateLingering;
};
}