-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathdprint.nix
More file actions
160 lines (154 loc) · 4.49 KB
/
dprint.nix
File metadata and controls
160 lines (154 loc) · 4.49 KB
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
{
lib,
pkgs,
config,
mkFormatterModule,
...
}:
let
inherit (lib)
filterAttrsRecursive
literalExpression
mkIf
mkOption
optionals
types
;
cfg = config.programs.dprint;
configFormat = pkgs.formats.json { };
# Definition taken from:
# https://raw.githubusercontent.com/dprint/dprint/5168db9ff0a9b5d42774f95edd58681fc67c9009/website/src/assets/schemas/v0.json
settingsSchema = mkOption {
default = { };
description = "Configuration to generate dprint.json with";
type = types.submodule {
freeformType = configFormat.type;
options = {
incremental = mkOption {
description = "Whether to format files only when they change.";
type = types.nullOr types.bool;
example = true;
default = null;
};
extends = mkOption {
description = "Configurations to extend.";
type = types.nullOr (
types.oneOf [
types.str
(types.listOf types.str)
]
);
example = "https://dprint.dev/path/to/config/file.v1.json";
default = null;
};
lineWidth = mkOption {
description = ''
The width of a line the printer will try to stay under. Note that the
printer may exceed this width in certain cases.
'';
type = types.nullOr types.int;
example = 80;
default = null;
};
indentWidth = mkOption {
description = "The number of characters for an indent";
type = types.nullOr types.int;
example = 2;
default = null;
};
useTabs = mkOption {
description = ''
Whether to use tabs (true) or spaces (false) for indentation.
'';
type = types.nullOr types.bool;
example = true;
default = null;
};
newLineKind = mkOption {
description = ''
The kind of newline to use (one of: auto, crlf, lf, system).
'';
type = types.nullOr types.str;
example = "auto";
default = null;
};
includes = mkOption {
internal = true;
description = "Set this value on program.dprint.includes";
type = types.nullOr (types.listOf types.str);
default = null;
};
excludes = mkOption {
internal = true;
description = "Set this value on program.dprint.excludes";
type = types.nullOr (types.listOf types.str);
default = null;
};
plugins = mkOption {
description = "Array of plugin URLs to format files.";
type = types.nullOr (types.listOf types.str);
example =
literalExpression
# nix
''
# (recommended) using plugins from nixpkgs
# plugins is the list of all available dprint plugins in nixpkgs
(pkgs.dprint-plugins.getPluginList (
plugins: with plugins; [
dprint-plugin-toml
dprint-plugin-dockerfile
g-plane-pretty_yaml
# (pkgs.callPackage ./dprint/plugins/sample.nix {})
]
))
# (not recommended) using url plugins
# treefmt then only works outside the nix sandbox, so treefmt nix flake check will fail
++ [
"https://plugins.dprint.dev/json-0.17.2.wasm"
"https://plugins.dprint.dev/markdown-0.15.2.wasm"
"https://plugins.dprint.dev/typescript-0.84.4.wasm"
]
'';
default = null;
};
};
};
};
settingsFile =
let
# remove all null values
settings = filterAttrsRecursive (_n: v: v != null) cfg.settings;
in
if settings != { } then
configFormat.generate "dprint.json" (
settings
// {
includes = cfg.includes;
excludes = cfg.excludes;
}
)
else
null;
in
{
meta.maintainers = [ ];
imports = [
(mkFormatterModule {
name = "dprint";
args = [ "fmt" ];
includes = [ ".*" ];
})
];
options.programs.dprint = {
# Represents the dprint.json config schema
settings = settingsSchema;
};
config = mkIf cfg.enable {
settings.formatter.dprint = {
options = optionals (settingsFile != null) [
"--config"
(toString settingsFile)
];
};
};
}