fork/home-manager

git clone https://git.julia.blue/fork/home-manager

tristanservices.swayidle: change target to generic session (#3913)8e5416b4

master
3.4 KiB134 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.swayidle;
8
9  mkTimeout = t:
10    [ "timeout" (toString t.timeout) (escapeShellArg t.command) ]
11    ++ optionals (t.resumeCommand != null) [
12      "resume"
13      (escapeShellArg t.resumeCommand)
14    ];
15
16  mkEvent = e: [ e.event (escapeShellArg e.command) ];
17
18  args = cfg.extraArgs ++ (concatMap mkTimeout cfg.timeouts)
19    ++ (concatMap mkEvent cfg.events);
20
21in {
22  meta.maintainers = [ maintainers.c0deaddict ];
23
24  options.services.swayidle = let
25
26    timeoutModule = { ... }: {
27      options = {
28        timeout = mkOption {
29          type = types.ints.positive;
30          example = 60;
31          description = "Timeout in seconds.";
32        };
33
34        command = mkOption {
35          type = types.str;
36          description = "Command to run after timeout seconds of inactivity.";
37        };
38
39        resumeCommand = mkOption {
40          type = with types; nullOr str;
41          default = null;
42          description = "Command to run when there is activity again.";
43        };
44      };
45    };
46
47    eventModule = { ... }: {
48      options = {
49        event = mkOption {
50          type = types.enum [ "before-sleep" "after-resume" "lock" "unlock" ];
51          description = "Event name.";
52        };
53
54        command = mkOption {
55          type = types.str;
56          description = "Command to run when event occurs.";
57        };
58      };
59    };
60
61  in {
62    enable = mkEnableOption "idle manager for Wayland";
63
64    package = mkOption {
65      type = types.package;
66      default = pkgs.swayidle;
67      defaultText = literalExpression "pkgs.swayidle";
68      description = "Swayidle package to install.";
69    };
70
71    timeouts = mkOption {
72      type = with types; listOf (submodule timeoutModule);
73      default = [ ];
74      example = literalExpression ''
75        [
76          { timeout = 60; command = "${pkgs.swaylock}/bin/swaylock -fF"; }
77        ]
78      '';
79      description = "List of commands to run after idle timeout.";
80    };
81
82    events = mkOption {
83      type = with types; listOf (submodule eventModule);
84      default = [ ];
85      example = literalExpression ''
86        [
87          { event = "before-sleep"; command = "${pkgs.swaylock}/bin/swaylock"; }
88          { event = "lock"; command = "lock"; }
89        ]
90      '';
91      description = "Run command on occurrence of a event.";
92    };
93
94    extraArgs = mkOption {
95      type = with types; listOf str;
96      default = [ ];
97      description = "Extra arguments to pass to swayidle.";
98    };
99
100    systemdTarget = mkOption {
101      type = types.str;
102      default = "graphical-session.target";
103      example = "sway-session.target";
104      description = ''
105        Systemd target to bind to.
106      '';
107    };
108
109  };
110
111  config = mkIf cfg.enable {
112    assertions = [
113      (hm.assertions.assertPlatform "services.swayidle" pkgs platforms.linux)
114    ];
115
116    systemd.user.services.swayidle = {
117      Unit = {
118        Description = "Idle manager for Wayland";
119        Documentation = "man:swayidle(1)";
120        PartOf = [ "graphical-session.target" ];
121      };
122
123      Service = {
124        Type = "simple";
125        # swayidle executes commands using "sh -c", so the PATH needs to contain a shell.
126        Environment = [ "PATH=${makeBinPath [ pkgs.bash ]}" ];
127        ExecStart =
128          "${cfg.package}/bin/swayidle -w ${concatStringsSep " " args}";
129      };
130
131      Install = { WantedBy = [ cfg.systemdTarget ]; };
132    };
133  };
134}