From 7f2863538c4d5f9c08a9a4c0353b7f95f609351b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Tue, 4 Feb 2020 08:29:52 +0100 Subject: [PATCH] Add loginctl module --- modules/private/default.nix | 1 + modules/private/loginctl-linger.nix | 47 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 modules/private/loginctl-linger.nix diff --git a/modules/private/default.nix b/modules/private/default.nix index 47abec8..945a799 100644 --- a/modules/private/default.nix +++ b/modules/private/default.nix @@ -75,6 +75,7 @@ set = { environment = ./environment.nix; system = ./system.nix; + loginctl-linger = ./loginctl-linger.nix; }; in builtins.listToAttrs (map (attr: { name = "priv${attr}"; value = set.${attr}; }) (builtins.attrNames set)) diff --git a/modules/private/loginctl-linger.nix b/modules/private/loginctl-linger.nix new file mode 100644 index 0000000..e6b9f23 --- /dev/null +++ b/modules/private/loginctl-linger.nix @@ -0,0 +1,47 @@ +{ config, lib, pkgs, ... }: + +# https://github.com/michalrus/dotfiles/commit/ebd5fa9583f82589f23531647aa677feb3f8d344#diff-4d353005ef5b3e37f33c07332b8523edR1 +# 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 = pkgs.writeScript "update-lingering" '' + if [ ! -e ${dataDir} ]; then + install -m 0755 -o root -g root -d ${dataDir} + fi + 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 + ''; + +in + +{ + options = { + users.users = mkOption { + options = [{ + linger = mkEnableOption "lingering for the user"; + }]; + }; + }; + + config = { + system.activationScripts.update-lingering = + stringAfter [ "users" ] updateLingering; + }; +} -- 2.41.0