3 nixosModule = { config, lib, pkgs, ... }:
4 # https://github.com/michalrus/dotfiles/commit/ebd5fa9583f82589f23531647aa677feb3f8d344#diff-4d353005ef5b3e37f33c07332b8523edR1
5 # A temporary hack to `loginctl enable-linger $somebody` (for
6 # multiplexer sessions to last), until this one is unresolved:
7 # https://github.com/NixOS/nixpkgs/issues/3702
9 # Usage: `users.extraUsers.somebody.linger = true` or slt.
15 dataDir = "/var/lib/systemd/linger";
17 lingeringUsers = map (u: u.name) (attrValues (flip filterAttrs config.users.users (n: u: u.linger)));
19 lingeringUsersFile = builtins.toFile "lingering-users"
20 (concatStrings (map (s: "${s}\n")
21 (sort (a: b: a < b) lingeringUsers))); # this sorting is important for `comm` to work correctly
23 updateLingering = pkgs.writeScript "update-lingering" ''
24 if [ ! -e ${dataDir} ]; then
25 install -m 0755 -o root -g root -d ${dataDir}
27 if [ -e ${dataDir} ] ; then
28 ls ${dataDir} | sort | comm -3 -1 ${lingeringUsersFile} - | xargs -r ${pkgs.systemd}/bin/loginctl disable-linger
29 ls ${dataDir} | sort | comm -3 -2 ${lingeringUsersFile} - | xargs -r ${pkgs.systemd}/bin/loginctl enable-linger
36 # Necessary for situations where flake gets included multiple times
37 key = builtins.hashString "sha256" (builtins.path { path = self.sourceInfo.outPath; name = "source"; });
39 users.users = mkOption {
40 type = lib.types.attrsOf (lib.types.submodule {
42 linger = mkEnableOption "lingering for the user";
49 system.activationScripts.update-lingering = {
51 text = "${updateLingering}";