-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemacs-init.nix
215 lines (188 loc) · 6.1 KB
/
emacs-init.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# TODO: early init
{ config, lib, pkgs, inputs, ... }:
with lib;
let
cfg = config.programs.emacs.init;
parse = import "${inputs.emacs-overlay}/parse.nix" { inherit lib pkgs; };
in {
options.programs.emacs.init = let
packagesType = mkOptionType {
name = "packages";
description = "function from epkgs to listOf package";
check = isFunction;
merge = mergeOneOption;
};
moduleType = { name, ... }: {
options = {
enable = mkEnableOption "Emacs module ${name}";
name = mkOption {
type = types.str;
readOnly = true;
description = ''
Name of the module.
'';
};
config = mkOption {
type = with types; either path lines;
default = "";
description = ''
Configuration for this module. Either a path to a file that contains
the configuration or a string that contains the configuration.
'';
};
configText = mkOption {
type = types.str;
readOnly = true;
internal = true;
description = ''
Configuration text for this module. This value is computed from
config.
'';
};
extraPackages = mkOption {
type = packagesType;
default = _: [ ];
description = ''
Extra packages to install.
'';
};
packages = mkOption {
type = packagesType;
readOnly = true;
internal = true;
description = ''
Computed packages for this module.
'';
};
feature = mkOption {
type = with types; nullOr str;
default = null;
description = ''
The feature to require for this module. The feature should be
provided by the module.
'';
};
};
config = let
moduleCfg = cfg.modules.${name};
inherit (moduleCfg) config extraPackages;
configText = let type = builtins.typeOf config;
in if type == "string" then
config
else if type == "path" then
builtins.readFile config
else
throw ''
Invalid for programs.emacs.init.modules."${name}".config: expected ‘path’ or ‘str’, got ‘${type}’'';
in {
name = mkDefault name;
inherit configText;
packages = epkgs:
let
usePackageNames = parse.parsePackagesFromUsePackage {
inherit configText;
alwaysEnsure = cfg.alwaysEnsure;
# TODO: support org-mode files
isOrgModeFile = false;
alwaysTangle = false;
};
getPackageFromName = name:
let notFound = "Cannot find Emacs pacakge for ‘${name}’";
in epkgs.${name} or (trace notFound null);
usePackages = map getPackageFromName usePackageNames;
in usePackages ++ extraPackages epkgs;
};
};
in {
enable = mkEnableOption "Emacs init configuration";
alwaysEnsure = mkOption {
type = types.bool;
default = false;
description = ''
Whether to interpret use-package declarations as always containing
`:ensure t`.
'';
};
byteCompile = mkOption {
type = types.bool;
default = false;
description = ''
Compile configuration using byte compilation.
'';
};
nativeCompile = mkOption {
type = types.bool;
default = false;
description = ''
Compile configuration using native compilation.
'';
};
emacsDir = mkOption {
type = types.str;
default = ".emacs.d";
description = ''
Directory of Emacs configuration, relative to $HOME.
'';
};
modules = mkOption {
type = with types; attrsOf (submodule moduleType);
default = { };
description = ''
Attribute-set of Emacs init modules.
'';
};
featureFile = mkOption {
type = with types; nullOr str;
default = null;
description = ''
Name of file relative to emacsDir to write the list of features of
enabled modules to.
'';
};
};
config = let
emacs = config.programs.emacs.finalPackage;
enabledModules = filterAttrs (_: module: module.enable) cfg.modules;
moduleFiles =
mapAttrs (name: module: pkgs.writeTextDir name module.configText)
enabledModules;
featureFile = let
features = map (module: module.feature)
(filter (module: module.enable && module.feature != null)
(attrValues cfg.modules));
in pkgs.writeTextDir cfg.featureFile ''
(${concatStringsSep " " features})
'';
maybeFeatureFile = if cfg.featureFile == null then [ ] else [ featureFile ];
emacsDirDerivation = pkgs.symlinkJoin {
name = cfg.emacsDir;
paths = attrValues moduleFiles ++ maybeFeatureFile;
};
# The Emacs directory under $HOME.
emacsDirHome = "${config.home.homeDirectory}/${cfg.emacsDir}";
# Command to compile the Emacs configuration directory. ‘mode’ must be one
# of "byte" and "native".
# TODO: specify location/module of init.el file to load.
compile = mode: files:
"${emacs}/bin/emacs --quick --no-window-system --load ${emacsDirHome}/init.el -batch --funcall batch-${mode}-compile ${
concatStringsSep " " files
}";
in {
programs.emacs.extraPackages = epkgs:
concatMap (module: if module.enable then module.packages epkgs else [ ])
(attrValues cfg.modules);
home.file.${cfg.emacsDir} = {
recursive = true;
source = emacsDirDerivation.outPath;
onChange = let
# TODO: add .el if it does not already contain .el.
files = map (name: "${emacsDirHome}/${name}") (attrNames enabledModules);
in ''
# Remove .elc files. Supply -f to ignore non-existent files.
rm -f ${concatStringsSep " " (map (name: "${name}c") files)}
${if cfg.byteCompile then compile "byte" files else ""}
${if cfg.nativeCompile then compile "native" files else ""}
'';
};
};
}