fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
5.4 KiB177 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.xsession.windowManager.xmonad;
8
9  xmonad = pkgs.xmonad-with-packages.override {
10    ghcWithPackages = cfg.haskellPackages.ghcWithPackages;
11    packages = self:
12      cfg.extraPackages self ++ optionals cfg.enableContribAndExtras [
13        self.xmonad-contrib
14        self.xmonad-extras
15      ];
16  };
17
18in {
19  options = {
20    xsession.windowManager.xmonad = {
21      enable = mkEnableOption "xmonad window manager";
22
23      haskellPackages = mkOption {
24        default = pkgs.haskellPackages;
25        defaultText = literalExpression "pkgs.haskellPackages";
26        example = literalExpression "pkgs.haskell.packages.ghc784";
27        description = ''
28          The {var}`haskellPackages` used to build xmonad
29          and other packages. This can be used to change the GHC
30          version used to build xmonad and the packages listed in
31          {var}`extraPackages`.
32        '';
33      };
34
35      extraPackages = mkOption {
36        default = self: [ ];
37        defaultText = "self: []";
38        example = literalExpression ''
39          haskellPackages: [
40            haskellPackages.xmonad-contrib
41            haskellPackages.monad-logger
42          ]
43        '';
44        description = ''
45          Extra packages available to GHC when rebuilding xmonad. The
46          value must be a function which receives the attribute set
47          defined in {var}`haskellPackages` as the sole
48          argument.
49        '';
50      };
51
52      enableContribAndExtras = mkOption {
53        default = false;
54        type = types.bool;
55        description = "Enable xmonad-{contrib,extras} in xmonad.";
56      };
57
58      config = mkOption {
59        type = types.nullOr types.path;
60        default = null;
61        example = literalExpression ''
62          pkgs.writeText "xmonad.hs" '''
63            import XMonad
64            main = xmonad defaultConfig
65                { terminal    = "urxvt"
66                , modMask     = mod4Mask
67                , borderWidth = 3
68                }
69          '''
70        '';
71        description = ''
72          The configuration file to be used for xmonad. This must be
73          an absolute path or `null` in which case
74          {file}`~/.xmonad/xmonad.hs` will not be managed
75          by Home Manager.
76
77          If this option is set to a non-`null` value,
78          recompilation of xmonad outside of Home Manager (e.g. via
79          {command}`xmonad --recompile`) will fail.
80        '';
81      };
82
83      libFiles = mkOption {
84        type = types.attrsOf (types.oneOf [ types.path ]);
85        default = { };
86        example = literalExpression ''
87          {
88            "Tools.hs" = pkgs.writeText "Tools.hs" '''
89               module Tools where
90               screenshot = "scrot"
91             ''';
92          }
93        '';
94        description = ''
95          Additional files that will be saved in
96          {file}`~/.xmonad/lib/` and included in the configuration
97          build. The keys are the file names while the values are paths to the
98          contents of the files.
99        '';
100      };
101    };
102  };
103
104  config = let
105
106    xmonadBin = "${
107        pkgs.runCommandLocal "xmonad-compile" {
108          nativeBuildInputs = [ xmonad ];
109        } ''
110          mkdir -p $out/bin
111
112          export XMONAD_CONFIG_DIR="$(pwd)/xmonad-config"
113          export XMONAD_DATA_DIR="$(pwd)/data"
114          export XMONAD_CACHE_DIR="$(pwd)/cache"
115
116          mkdir -p "$XMONAD_CONFIG_DIR/lib" "$XMONAD_CACHE_DIR" "$XMONAD_DATA_DIR"
117
118          cp ${cfg.config} xmonad-config/xmonad.hs
119
120          declare -A libFiles
121          libFiles=(${
122            concatStringsSep " "
123            (mapAttrsToList (name: value: "['${name}']='${value}'")
124              cfg.libFiles)
125          })
126          for key in "''${!libFiles[@]}"; do
127            cp "''${libFiles[$key]}" "xmonad-config/lib/$key";
128          done
129
130          xmonad --recompile
131
132          # The resulting binary name depends on the arch and os
133          # https://github.com/xmonad/xmonad/blob/56b0f850bc35200ec23f05c079eca8b0a1f90305/src/XMonad/Core.hs#L565-L572
134          if [ -f "$XMONAD_DATA_DIR/xmonad-${pkgs.stdenv.hostPlatform.system}" ]; then
135            # xmonad 0.15.0
136            mv "$XMONAD_DATA_DIR/xmonad-${pkgs.stdenv.hostPlatform.system}" $out/bin/
137          else
138            # xmonad 0.17.0 (https://github.com/xmonad/xmonad/commit/9813e218b034009b0b6d09a70650178980e05d54)
139            mv "$XMONAD_CACHE_DIR/xmonad-${pkgs.stdenv.hostPlatform.system}" $out/bin/
140          fi
141        ''
142      }/bin/xmonad-${pkgs.stdenv.hostPlatform.system}";
143
144  in mkIf cfg.enable (mkMerge [
145    {
146      assertions = [
147        (hm.assertions.assertPlatform "xsession.windowManager.xmonad" pkgs
148          platforms.linux)
149      ];
150
151      home.packages = [ (lowPrio xmonad) ];
152
153      home.file = mapAttrs' (name: value:
154        attrsets.nameValuePair (".xmonad/lib/" + name) { source = value; })
155        cfg.libFiles;
156    }
157
158    (mkIf (cfg.config == null) {
159      xsession.windowManager.command = "${xmonad}/bin/xmonad";
160    })
161
162    (mkIf (cfg.config != null) {
163      xsession.windowManager.command = xmonadBin;
164      home.file.".xmonad/xmonad.hs".source = cfg.config;
165      home.file.".xmonad/xmonad-${pkgs.stdenv.hostPlatform.system}" = {
166        source = xmonadBin;
167        onChange = ''
168          # Attempt to restart xmonad if X is running.
169          if [[ -v DISPLAY ]]; then
170            ${config.xsession.windowManager.command} --restart
171          fi
172        '';
173      };
174    })
175
176  ]);
177}