fork/home-manager

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

Robert HelgessonMerge PR #421575790441

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