forked from nix-community/home-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsession.nix
218 lines (184 loc) · 6.23 KB
/
xsession.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.xsession;
in {
meta.maintainers = [ maintainers.rycee ];
options = {
xsession = {
enable = mkEnableOption "X Session";
scriptPath = mkOption {
type = types.str;
default = ".xsession";
example = ".xsession-hm";
description = ''
Path, relative to <envar>HOME</envar>, where Home Manager
should write the X session script.
'';
};
profilePath = mkOption {
type = types.str;
default = ".xprofile";
example = ".xprofile-hm";
description = ''
Path, relative to <envar>HOME</envar>, where Home Manager
should write the X profile script.
'';
};
windowManager.command = mkOption {
type = types.str;
example = literalExpression ''
let
xmonad = pkgs.xmonad-with-packages.override {
packages = self: [ self.xmonad-contrib self.taffybar ];
};
in
"''${xmonad}/bin/xmonad";
'';
default = ''test -n "$1" && eval "$@"'';
description = ''
Command to use to start the window manager.
</para><para>
The default value allows integration with NixOS' generated xserver configuration.
</para><para>
Extra actions and commands can be specified in <option>xsession.initExtra</option>.
'';
};
preferStatusNotifierItems = mkOption {
type = types.bool;
default = false;
example = true;
description = ''
Whether tray applets should prefer using the Status Notifier
Items (SNI) protocol, commonly called App Indicators. Note,
not all tray applets or status bars support SNI.
'';
};
profileExtra = mkOption {
type = types.lines;
default = "";
description = "Extra shell commands to run before session start.";
};
initExtra = mkOption {
type = types.lines;
default = "";
description = "Extra shell commands to run during initialization.";
};
importedVariables = mkOption {
type = types.listOf (types.strMatching "[a-zA-Z_][a-zA-Z0-9_]*");
example = [ "GDK_PIXBUF_ICON_LOADER" ];
visible = false;
description = ''
Environment variables to import into the user systemd
session. The will be available for use by graphical
services.
'';
};
};
};
config = mkIf cfg.enable {
assertions =
[ (hm.assertions.assertPlatform "xsession" pkgs platforms.linux) ];
xsession.importedVariables = [
"DBUS_SESSION_BUS_ADDRESS"
"DISPLAY"
"SSH_AUTH_SOCK"
"XAUTHORITY"
"XDG_DATA_DIRS"
"XDG_RUNTIME_DIR"
"XDG_SESSION_ID"
];
systemd.user = {
services = mkIf (config.home.keyboard != null) {
setxkbmap = {
Unit = {
Description = "Set up keyboard in X";
After = [ "graphical-session-pre.target" ];
PartOf = [ "graphical-session.target" ];
};
Install = { WantedBy = [ "graphical-session.target" ]; };
Service = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = with config.home.keyboard;
let
args = optional (layout != null) "-layout '${layout}'"
++ optional (variant != null) "-variant '${variant}'"
++ optional (model != null) "-model '${model}'"
++ [ "-option ''" ] ++ map (v: "-option '${v}'") options;
in "${pkgs.xorg.setxkbmap}/bin/setxkbmap ${toString args}";
};
};
xplugd = {
Unit = {
Description = "Rerun setxkbmap.service when I/O is changed";
After = [ "graphical-session-pre.target" ];
PartOf = [ "graphical-session.target" ];
};
Install = { WantedBy = [ "graphical-session.target" ]; };
Service = {
Type = "forking";
ExecStart = let
script = pkgs.writeShellScript "xplugrc" ''
case "$1,$3" in
keyboard,connected)
systemctl --user restart setxkbmap.service
;;
esac
'';
in "${pkgs.xplugd}/bin/xplugd ${script}";
};
};
};
targets = {
# A basic graphical session target for Home Manager.
hm-graphical-session = {
Unit = {
Description = "Home Manager X session";
Requires = [ "graphical-session-pre.target" ];
BindsTo = [ "graphical-session.target" "tray.target" ];
};
};
tray = {
Unit = {
Description = "Home Manager System Tray";
Requires = [ "graphical-session-pre.target" ];
};
};
};
};
home.file.${cfg.profilePath}.text = ''
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"
if [ -e "$HOME/.profile" ]; then
. "$HOME/.profile"
fi
# If there are any running services from a previous session.
# Need to run this in xprofile because the NixOS xsession
# script starts up graphical-session.target.
systemctl --user stop graphical-session.target graphical-session-pre.target
${optionalString (cfg.importedVariables != [ ])
("systemctl --user import-environment "
+ toString (unique cfg.importedVariables))}
${cfg.profileExtra}
export HM_XPROFILE_SOURCED=1
'';
home.file.${cfg.scriptPath} = {
executable = true;
text = ''
if [ -z "$HM_XPROFILE_SOURCED" ]; then
. "${config.home.homeDirectory}/${cfg.profilePath}"
fi
unset HM_XPROFILE_SOURCED
systemctl --user start hm-graphical-session.target
${cfg.initExtra}
${cfg.windowManager.command}
systemctl --user stop graphical-session.target
systemctl --user stop graphical-session-pre.target
# Wait until the units actually stop.
while [ -n "$(systemctl --user --no-legend --state=deactivating list-units)" ]; do
sleep 0.5
done
'';
};
};
}