Update loginctl-linger to work with removing users
diff --git a/flake.nix b/flake.nix
index c897bc8..2dafc74 100644
--- a/flake.nix
+++ b/flake.nix
@@ -51,6 +51,7 @@
./modules/grafana.nix
./modules/home-manager-users.nix
./modules/kitty.nix
+ ./modules/loginctl-linger.nix
./modules/matrix.nix
./modules/mongodb.nix
./modules/node.nix
diff --git a/modules/home-manager-users.nix b/modules/home-manager-users.nix
index a3f450e..9189240 100644
--- a/modules/home-manager-users.nix
+++ b/modules/home-manager-users.nix
@@ -16,13 +16,6 @@
);
in
{
- imports = [
- (builtins.fetchurl {
- url = "https://gist.githubusercontent.com/graham33/fdbdcc18317a621d9dd54beb36be6683/raw/776ed252749313470f1c9a286a0419ba9746d133/loginctl-linger.nix";
- sha256 = "sha256:0hwm4f13dwd27gbdn5ddvbrmcvfb70jr658jz4nbkzwzh8c02qj8";
- })
- ];
-
users.users = lib.pipe ../services [
builtins.readDir
(lib.filterAttrs (_name: value: value == "directory"))
diff --git a/modules/loginctl-linger.nix b/modules/loginctl-linger.nix
new file mode 100644
index 0000000..6fe7c60
--- /dev/null
+++ b/modules/loginctl-linger.nix
@@ -0,0 +1,50 @@
+{ 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.
+# Originally from
+# https://gist.githubusercontent.com/graham33/fdbdcc18317a621d9dd54beb36be6683/raw/776ed252749313470f1c9a286a0419ba9746d133/loginctl-linger.nix,
+# modified by Minion3665
+
+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
+ ${pkgs.gawk}/bin/awk -F':' '{ print $1}' /etc/passwd | sort > /tmp/users-that-actually-exist
+ ls ${dataDir} | sort | comm -3 -1 ${lingeringUsersFile} - | comm -3 -2 /tmp/users-that-actually-exist - | xargs -r ${pkgs.systemd}/bin/loginctl disable-linger
+ ls ${dataDir} | sort | comm -3 -2 ${lingeringUsersFile} - | xargs -r ${pkgs.systemd}/bin/loginctl enable-linger
+ ls ${dataDir} | sort | comm -3 -1 /tmp/users-that-actually-exist - | ${pkgs.gawk}/bin/awk '{print "${dataDir}/"$1}' | xargs -r rm
+ rm -f /tmp/users-that-actually-exist
+ 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;
+ };
+}