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