-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathocamlformat.nix
56 lines (51 loc) · 1.5 KB
/
ocamlformat.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
{
lib,
pkgs,
config,
mkFormatterModule,
...
}:
let
cfg = config.programs.ocamlformat;
detectVersion =
configFile: pkgSet:
let
optionValue =
list:
assert lib.assertMsg (list != [ ]) "treefmt-nix: Unable to detect ocamlformat version from file";
lib.elemAt list (lib.length list - 1);
trim = lib.replaceStrings [ " " ] [ "" ];
in
lib.getAttr "ocamlformat_${lib.replaceStrings [ "." ] [ "_" ] (optionValue (lib.findFirst (option: lib.head option == "version") [ ] (lib.map (n: lib.splitString "=" (trim n)) (lib.splitString "\n" (lib.readFile configFile)))))}" pkgSet;
in
{
meta.maintainers = [ ];
imports = [
(mkFormatterModule {
name = "ocamlformat";
args = [ "-i" ];
includes = [
"*.ml"
"*.mli"
];
})
];
options.programs.ocamlformat = {
pkgs = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw;
description = "The package set used to get the ocamlformat package at a specific version.";
default = pkgs;
defaultText = lib.literalMD "Nixpkgs from context";
};
configFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
description = "Path to the .ocamlformat file. Used to pick the right version of ocamlformat if passed.";
default = null;
};
};
config = lib.mkIf cfg.enable {
settings.formatter.ocamlformat = {
command = if cfg.configFile == null then cfg.package else detectVersion cfg.configFile cfg.pkgs;
};
};
}