1 {pkgs, lib, config, ...}:
5 inherit (lib) mkOption mkIf optionals literalExpression optionalString;
6 cfg = config.services.xserver.windowManager.xmonad;
8 ghcWithPackages = cfg.haskellPackages.ghcWithPackages;
9 packages = self: cfg.extraPackages self ++
10 optionals cfg.enableContribAndExtras
11 [ self.xmonad-contrib self.xmonad-extras ];
13 xmonad-vanilla = pkgs.xmonad-with-packages.override {
14 inherit ghcWithPackages packages;
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;
27 pkgs.runCommandLocal "xmonad" {
28 nativeBuildInputs = [ pkgs.makeWrapper ];
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 '' + optionalString cfg.enableConfiguredRecompile ''
33 --set XMONAD_GHC "${xmonadEnv}/bin/ghc" \
35 --set XMONAD_XMESSAGE "${pkgs.xorg.xmessage}/bin/xmessage"
38 xmonad = if (cfg.config != null) then xmonad-config else xmonad-vanilla;
40 meta.maintainers = with maintainers; [ lassulus xaverdh ivanbrennan slotThe ];
43 services.xserver.windowManager.xmonad = {
44 enable = mkEnableOption "xmonad";
46 haskellPackages = mkOption {
47 default = pkgs.haskellPackages;
48 defaultText = literalExpression "pkgs.haskellPackages";
49 example = literalExpression "pkgs.haskell.packages.ghc810";
52 haskellPackages used to build Xmonad and other packages.
53 This can be used to change the GHC version used to build
54 Xmonad and the packages listed in
59 extraPackages = mkOption {
60 type = types.functionTo (types.listOf types.package);
62 defaultText = literalExpression "self: []";
63 example = literalExpression ''
65 haskellPackages.xmonad-contrib
66 haskellPackages.monad-logger
70 Extra packages available to ghc when rebuilding Xmonad. The
71 value must be a function which receives the attrset defined
72 in {var}`haskellPackages` as the sole argument.
76 enableContribAndExtras = mkOption {
78 type = lib.types.bool;
79 description = "Enable xmonad-{contrib,extras} in Xmonad.";
84 type = with lib.types; nullOr (either path str);
86 Configuration from which XMonad gets compiled. If no value is
87 specified, a vanilla xmonad binary is put in PATH, which will
88 attempt to recompile and exec your xmonad config from $HOME/.xmonad.
89 This setup is then analogous to other (non-NixOS) linux distributions.
91 If you do set this option, you likely want to use "launch" as your
92 entry point for xmonad (as in the example), to avoid xmonad's
93 recompilation logic on startup. Doing so will render the default
94 "mod+q" restart key binding dysfunctional though, because that attempts
95 to call your binary with the "--restart" command line option, unless
96 you implement that yourself. You way mant to bind "mod+q" to
97 `(restart "xmonad" True)` instead, which will just restart
98 xmonad from PATH. This allows e.g. switching to the new xmonad binary
99 after rebuilding your system with nixos-rebuild.
100 For the same reason, ghc is not added to the environment when this
101 option is set, unless {option}`enableConfiguredRecompile` is
104 If you actually want to run xmonad with a config specified here, but
105 also be able to recompile and restart it from a copy of that source in
106 $HOME/.xmonad on the fly, set {option}`enableConfiguredRecompile`
107 to `true` and implement something like "compileRestart"
109 This should allow you to switch at will between the local xmonad and
110 the one NixOS puts in your PATH.
114 import XMonad.Util.EZConfig (additionalKeys)
115 import Control.Monad (when)
116 import Text.Printf (printf)
117 import System.Posix.Process (executeFile)
118 import System.Info (arch,os)
119 import System.Environment (getArgs)
120 import System.FilePath ((</>))
122 compiledConfig = printf "xmonad-%s-%s" arch os
124 myConfig = defaultConfig
125 { modMask = mod4Mask -- Use Super instead of Alt
126 , terminal = "urxvt" }
128 [ ( (mod4Mask,xK_r), compileRestart True)
129 , ( (mod4Mask,xK_q), restart "xmonad" True ) ]
131 compileRestart resume = do
132 dirs <- asks directories
133 whenX (recompile dirs True) $ do
134 when resume writeStateToFile
138 executeFile (cacheDir dirs </> compiledConfig) False args Nothing
141 main = getDirectories >>= launch myConfig
143 --------------------------------------------
144 {- For versions before 0.17.0 use this instead -}
145 --------------------------------------------
146 -- compileRestart resume =
147 -- whenX (recompile True) $
148 -- when resume writeStateToFile
151 -- dir <- getXMonadDataDir
153 -- executeFile (dir </> compiledConfig) False args Nothing
156 -- main = launch myConfig
157 --------------------------------------------
162 enableConfiguredRecompile = mkOption {
164 type = lib.types.bool;
166 Enable recompilation even if {option}`config` is set to a
167 non-null value. This adds the necessary Haskell dependencies (GHC with
168 packages) to the xmonad binary's environment.
172 xmonadCliArgs = mkOption {
174 type = with lib.types; listOf str;
176 Command line arguments passed to the xmonad binary.
182 type = with lib.types; listOf str;
184 Command line arguments passed to the compiler (ghc)
185 invocation when xmonad.config is set.
191 config = mkIf cfg.enable {
192 services.xserver.windowManager = {
196 systemd-cat -t xmonad -- ${xmonad}/bin/xmonad ${lib.escapeShellArgs cfg.xmonadCliArgs} &
202 environment.systemPackages = [ xmonad ];