diff options
-rw-r--r-- | modules/private/default.nix | 1 | ||||
-rw-r--r-- | modules/private/loginctl-linger.nix | 47 |
2 files changed, 48 insertions, 0 deletions
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 = { | |||
75 | 75 | ||
76 | environment = ./environment.nix; | 76 | environment = ./environment.nix; |
77 | system = ./system.nix; | 77 | system = ./system.nix; |
78 | loginctl-linger = ./loginctl-linger.nix; | ||
78 | }; | 79 | }; |
79 | in | 80 | in |
80 | builtins.listToAttrs (map (attr: { name = "priv${attr}"; value = set.${attr}; }) (builtins.attrNames set)) | 81 | 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 @@ | |||
1 | { config, lib, pkgs, ... }: | ||
2 | |||
3 | # https://github.com/michalrus/dotfiles/commit/ebd5fa9583f82589f23531647aa677feb3f8d344#diff-4d353005ef5b3e37f33c07332b8523edR1 | ||
4 | # A temporary hack to `loginctl enable-linger $somebody` (for | ||
5 | # multiplexer sessions to last), until this one is unresolved: | ||
6 | # https://github.com/NixOS/nixpkgs/issues/3702 | ||
7 | # | ||
8 | # Usage: `users.extraUsers.somebody.linger = true` or slt. | ||
9 | |||
10 | with lib; | ||
11 | |||
12 | let | ||
13 | |||
14 | dataDir = "/var/lib/systemd/linger"; | ||
15 | |||
16 | lingeringUsers = map (u: u.name) (attrValues (flip filterAttrs config.users.users (n: u: u.linger))); | ||
17 | |||
18 | lingeringUsersFile = builtins.toFile "lingering-users" | ||
19 | (concatStrings (map (s: "${s}\n") | ||
20 | (sort (a: b: a < b) lingeringUsers))); # this sorting is important for `comm` to work correctly | ||
21 | |||
22 | updateLingering = pkgs.writeScript "update-lingering" '' | ||
23 | if [ ! -e ${dataDir} ]; then | ||
24 | install -m 0755 -o root -g root -d ${dataDir} | ||
25 | fi | ||
26 | if [ -e ${dataDir} ] ; then | ||
27 | ls ${dataDir} | sort | comm -3 -1 ${lingeringUsersFile} - | xargs -r ${pkgs.systemd}/bin/loginctl disable-linger | ||
28 | ls ${dataDir} | sort | comm -3 -2 ${lingeringUsersFile} - | xargs -r ${pkgs.systemd}/bin/loginctl enable-linger | ||
29 | fi | ||
30 | ''; | ||
31 | |||
32 | in | ||
33 | |||
34 | { | ||
35 | options = { | ||
36 | users.users = mkOption { | ||
37 | options = [{ | ||
38 | linger = mkEnableOption "lingering for the user"; | ||
39 | }]; | ||
40 | }; | ||
41 | }; | ||
42 | |||
43 | config = { | ||
44 | system.activationScripts.update-lingering = | ||
45 | stringAfter [ "users" ] updateLingering; | ||
46 | }; | ||
47 | } | ||