-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathbiome.nix
More file actions
168 lines (158 loc) · 3.84 KB
/
biome.nix
File metadata and controls
168 lines (158 loc) · 3.84 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
161
162
163
164
165
166
167
168
{
lib,
pkgs,
config,
options,
mkFormatterModule,
...
}:
let
l = lib // builtins;
t = l.types;
p = pkgs;
json = p.formats.json { };
cfg = config.programs.biome;
opts = options.programs.biome;
biomeVersion =
if builtins.match "^1\\." pkgs.biome.version != null then
"1.9.4"
else if builtins.match "^2\\.3\\." pkgs.biome.version != null then
"2.3.6"
else
"2.1.2";
schemaHashes = {
"1.9.4" = "sha256-SkkULLRk4CQzk+j0h8PAqOY6vGOrdG5ja7Z/tSAAKnY=";
"2.1.2" = "sha256-n4Y16J7g34e0VdQzRItu/P7n5oppkY4Vm4P1pQxOILU=";
"2.3.6" = "sha256-eBBUomh9qBkl47tp73vsgWeOPZdVVGR3CAQ5eBs8eNw=";
};
ext.js = [
"*.js"
"*.ts"
"*.mjs"
"*.mts"
"*.cjs"
"*.cts"
"*.jsx"
"*.tsx"
"*.d.ts"
"*.d.cts"
"*.d.mts"
];
ext.json = [
"*.json"
"*.jsonc"
];
ext.css = [
"*.css"
];
in
{
meta.maintainers = [
"NixOS/nixpkgs-ci"
"andrea11"
];
imports = [
(mkFormatterModule {
name = "biome";
args = [
cfg.formatCommand
"--write"
"--no-errors-on-unmatched"
];
includes = ext.js ++ ext.json ++ ext.css;
})
];
options.programs.biome = {
formatCommand = l.mkOption {
type = t.enum [
"format"
"lint"
"check"
];
description = "The command to run Biome with.";
default = "check";
example = "format";
};
formatUnsafe = l.mkOption {
type = t.bool;
description = "Allows to format a document that has unsafe fixes.";
default = false;
};
settings = l.mkOption {
inherit (json) type;
description = "Raw Biome configuration (must conform to Biome JSON schema)";
default = { };
example = {
formatter = {
indentStyle = "space";
lineWidth = 100;
};
javascript = {
formatter = {
quoteStyle = "single";
lineWidth = 120;
};
};
json = {
formatter = {
enabled = false;
};
};
};
};
validate = {
enable = l.mkOption {
type = t.bool;
description = "Whether to validate `${opts.settings}`.";
default = true;
example = false;
};
schema = l.mkOption {
type = t.path;
description = "The biome schema file to validate against";
defaultText = l.literalMD ''
Fetches `"https://biomejs.dev/schemas/''${biomeVersion}/schema.json"` using `pkgs.fetchurl`.
'';
default = p.fetchurl {
url = "https://biomejs.dev/schemas/${biomeVersion}/schema.json";
hash = schemaHashes.${biomeVersion};
};
example = l.literalExpression ''
pkgs.fetchurl {
url = "https://biomejs.dev/schemas/2.1.2/schema.json"
hash = "sha256-n4Y16J7g34e0VdQzRItu/P7n5oppkY4Vm4P1pQxOILU=";
}
'';
};
};
};
config = l.mkIf cfg.enable {
settings.formatter.biome.options =
let
jsonFile = json.generate "biome.json" cfg.settings;
validatedConfig =
p.runCommand "validated-biome-config.json"
{
nativeBuildInputs = [
p.check-jsonschema
];
env = {
json = jsonFile;
schema = cfg.validate.schema;
schemaPath = cfg.validate.schema.url or (toString cfg.validate.schema);
};
}
''
echo "Validating biome.json against schema $schemaPath..."
export HOME=$TMPDIR
check-jsonschema --schemafile "$schema" "$json"
cp "$json" $out
'';
in
[
"--config-path"
"${if cfg.validate.enable then validatedConfig else jsonFile}"
]
++ l.optional cfg.formatUnsafe "--unsafe";
};
}