git.iko.soy/typeable/nixpkgs
on master help sign in
XMonad: configured recompile (#107696)

* nixos/xmonad: xmonad config w/ghc+xmessage When the "config" option isn't set, we use xmonad-with-packages to provide xmonad with runtime access to an isolated ghc, ensuring it can recompile and exec a user's local config (e.g. $HOME/.xmonad/xmonad.hs) regardless of which ghc (if any) is on PATH. When the "config" option is set, however, we compile a configured xmonad executable upfront (during nixos-rebuild), and prior to this commit, it was not provided with runtime access to an isolated ghc. As a result, with the "config" option set, it was not possible to recompile and exec a user's local config unless there was a compatible version of ghc on PATH with the necessary packages (xmonad, xmonad-contrib, etc.) in its package database. Adding such a ghc to environment.systemPackages, e.g. (haskellPackages.ghcWithPackages (ps: with ps; [xmonad xmonad-contrib])) is problematic because it adds both ghc and an unconfigured xmonad to PATH, e.g. $ ls -l $(which xmonad ghc) lrwxrwxrwx ... /run/current-system/sw/bin/ghc -> /nix/store/...-ghc-8.10.2-with-packages/bin/ghc lrwxrwxrwx ... /run/current-system/sw/bin/xmonad -> /nix/store/...-ghc-8.10.2-with-packages/bin/xmonad Having the unconfigured xmonad on PATH is particularly bad because restarting xmonad will dump the user into the unconfigured version, and if no local config exists (e.g. in $HOME/.xmonad/xmonad.hs), they'll be left in this unconfigured state. In this commmit, we give the configured xmonad runtime access to ghc like xmonad-with-packages does for the unconfigured version. The aim is to allow the user to switch between the nixos module's config and a local config (e.g. $HOME/.xmonad/xmonad.hs) at will, so they can try out config changes without performing a nixos-rebuild. Since the xmonad on PATH is the configured executable, there's no danger a user could unwittingly restart into the unconfigured version, and because xmonad will refuse to recompile when no local config exists, there's no danger a user could unwittingly recompile into an unconfigured version. Given that a local config exists, the recompile/restart behavior depends on two factors: - which entry point is used * 'XMonad.xmonad' (default) * 'XMonad.launch' (recommended in "config" option description) - what operation is triggered (i.e. via mod+q) * `spawn "xmonad --recompile && xmonad --restart"` (default) * `restart "xmonad" True` * custom function If the default 'XMonad.xmonad' entrypoint and default mod+q operation are used, hitting mod+q will compile and exec the local config, which will remain in use until next time the display manager is restarted. If the entrypoint is changed to 'XMonad.launch' but mod+q left with its default operation, hitting mod+q will have no visible effect. The logs (as seen by running `journalctl --identifier xmonad --follow`) will show an error, X Error of failed request: BadAccess (attempt to access private resource denied) which indicates that the shell was unable to start xmonad because another window manager is already running (namely, the nixos-configured xmonad). https://wiki.haskell.org/Xmonad/Frequently_asked_questions#X_Error_of_failed_request:_BadAccess_.28attempt_to_access_private_resource_denied.29 Changing the mod+q operation to `restart "xmonad" True` (as recommended in the "config" option's description) will allow a restart of the nixos-configured xmonad to be triggeredy by hitting mod+q. Finally, if the entrypoint is 'XMonad.launch', mod+q has been bound to `restart "xmonad" True` and another key bound to a custom recompile/restart function (e.g. `compileRestart` as shown in the "config" option example), the user can switch between the nixos module's config and their local config, with the custom key switching to the local config and mod+q switching back. * nixos/xmonad: refactor let binding * nixos/xmonad: refactor (eliminate duplicate code) * nixos/xmonad: install man pages Prior to this commit, man pages were not installed if the "config" option was set. * nixos/xmonad: comment grammar fixups * nixos/xmonad: writeStateToFile in example config Calling writeStateToFile prior to recompiling and restarting allows state (workspaces, etc.) to be preserved across the restart. * nixos/xmonad: add ivanbrennan to maintainers * nixos/xmonad: adjust compileRestart example * nixos/xmonad: add missing import to example config

commitb90c5cb703da946f53097bc8c7fb1a5acd09c701 authorIvan <ivanbrennan@users.noreply.github.com> dateMon 28 Dec 2020, 11:27 (6 years ago) parent80e3ca6
1 file changed+38−20
nixos/modules/services/x11/window-managers/xmonad.nix+38−20
nixos/modules/services/x11/window-managers/xmonad.nix+38−20
@@ -5,25 +5,37 @@ let
55 inherit (lib) mkOption mkIf optionals literalExample;
66 cfg = config.services.xserver.windowManager.xmonad;
77
8+ ghcWithPackages = cfg.haskellPackages.ghcWithPackages;
9+ packages = self: cfg.extraPackages self ++
10+ optionals cfg.enableContribAndExtras
11+ [ self.xmonad-contrib self.xmonad-extras ];
12+
813 xmonad-vanilla = pkgs.xmonad-with-packages.override {
9− ghcWithPackages = cfg.haskellPackages.ghcWithPackages;
10− packages = self: cfg.extraPackages self ++
11− optionals cfg.enableContribAndExtras
12− [ self.xmonad-contrib self.xmonad-extras ];
14+ inherit ghcWithPackages packages;
1315 };
1416
15− xmonad-config = pkgs.writers.writeHaskellBin "xmonad" {
16− ghc = cfg.haskellPackages.ghc;
17− libraries = [ cfg.haskellPackages.xmonad ] ++
18− cfg.extraPackages cfg.haskellPackages ++
19− optionals cfg.enableContribAndExtras
20− (with cfg.haskellPackages; [ xmonad-contrib xmonad-extras ]);
21− inherit (cfg) ghcArgs;
22− } cfg.config;
17+ xmonad-config =
18+ let
19+ xmonadAndPackages = self: [ self.xmonad ] ++ packages self;
20+ xmonadEnv = ghcWithPackages xmonadAndPackages;
21+ configured = pkgs.writers.writeHaskellBin "xmonad" {
22+ ghc = cfg.haskellPackages.ghc;
23+ libraries = xmonadAndPackages cfg.haskellPackages;
24+ inherit (cfg) ghcArgs;
25+ } cfg.config;
26+ in
27+ pkgs.runCommandLocal "xmonad" {
28+ nativeBuildInputs = [ pkgs.makeWrapper ];
29+ } ''
30+ install -D ${xmonadEnv}/share/man/man1/xmonad.1.gz $out/share/man/man1/xmonad.1.gz
31+ makeWrapper ${configured}/bin/xmonad $out/bin/xmonad \
32+ --set NIX_GHC "${xmonadEnv}/bin/ghc" \
33+ --set XMONAD_XMESSAGE "${pkgs.xorg.xmessage}/bin/xmessage"
34+ '';
2335
2436 xmonad = if (cfg.config != null) then xmonad-config else xmonad-vanilla;
2537in {
26− meta.maintainers = with maintainers; [ lassulus xaverdh ];
38+ meta.maintainers = with maintainers; [ lassulus xaverdh ivanbrennan ];
2739
2840 options = {
2941 services.xserver.windowManager.xmonad = {
@@ -72,13 +84,13 @@ in {
7284 This setup is then analogous to other (non-NixOS) linux distributions.
7385
7486 If you do set this option, you likely want to use "launch" as your
75− entry point for xmonad (as in the example), to avoid xmonads
87+ entry point for xmonad (as in the example), to avoid xmonad's
7688 recompilation logic on startup. Doing so will render the default
7789 "mod+q" restart key binding dysfunctional though, because that attempts
7890 to call your binary with the "--restart" command line option, unless
7991 you implement that yourself. You way mant to bind "mod+q" to
8092 <literal>(restart "xmonad" True)</literal> instead, which will just restart
81− xmonad from PATH. This allows e.g. switching to the new xmonad binary,
93+ xmonad from PATH. This allows e.g. switching to the new xmonad binary
8294 after rebuilding your system with nixos-rebuild.
8395
8496 If you actually want to run xmonad with a config specified here, but
@@ -91,6 +103,7 @@ in {
91103 example = ''
92104 import XMonad
93105 import XMonad.Util.EZConfig (additionalKeys)
106+ import Control.Monad (when)
94107 import Text.Printf (printf)
95108 import System.Posix.Process (executeFile)
96109 import System.Info (arch,os)
@@ -99,16 +112,21 @@ in {
99112
100113 compiledConfig = printf "xmonad-%s-%s" arch os
101114
102− compileRestart = whenX (recompile True) . catchIO $ do
103− dir <- getXMonadDataDir
104− args <- getArgs
105− executeFile (dir </> compiledConfig) False args Nothing
115+ compileRestart resume =
116+ whenX (recompile True) $
117+ when resume writeStateToFile
118+ *> catchIO
119+ ( do
120+ dir <- getXMonadDataDir
121+ args <- getArgs
122+ executeFile (dir </> compiledConfig) False args Nothing
123+ )
106124
107125 main = launch defaultConfig
108126 { modMask = mod4Mask -- Use Super instead of Alt
109127 , terminal = "urxvt" }
110128 `additionalKeys`
111− [ ( (mod4Mask,xK_r), compileRestart )
129+ [ ( (mod4Mask,xK_r), compileRestart True)
112130 , ( (mod4Mask,xK_q), restart "xmonad" True ) ]
113131 '';
114132 };