Created
March 21, 2023 08:49
-
-
Save graham33/fdbdcc18317a621d9dd54beb36be6683 to your computer and use it in GitHub Desktop.
NixOS module to enable systemd linger for users. Adapted from https://github.com/michalrus/dotfiles/commit/ebd5fa9583f82589f23531647aa677feb3f8d344#diff-4d353005ef5b3e37f33c07332b8523edR1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ 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"; | |
lingeringUsers = map (u: u.name) (attrValues (flip filterAttrs config.users.users (n: u: u.linger))); | |
lingeringUsersFile = builtins.toFile "lingering-users" | |
(concatStrings (map (s: "${s}\n") | |
(sort (a: b: a < b) lingeringUsers))); # this sorting is important for `comm` to work correctly | |
updateLingering = '' | |
if [ -e ${dataDir} ] ; then | |
ls ${dataDir} | sort | comm -3 -1 ${lingeringUsersFile} - | xargs -r ${pkgs.systemd}/bin/loginctl disable-linger | |
ls ${dataDir} | sort | comm -3 -2 ${lingeringUsersFile} - | xargs -r ${pkgs.systemd}/bin/loginctl enable-linger | |
fi | |
''; | |
userOptions = { | |
options.linger = mkEnableOption "Lingering for the user"; | |
}; | |
in | |
{ | |
options = { | |
users.users = mkOption { | |
type = with types; attrsOf (submodule userOptions); | |
}; | |
}; | |
config = { | |
system.activationScripts.update-lingering = stringAfter [ "users" ] updateLingering; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment