Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
455 lines (398 loc) · 22.5 KB
/
Copy pathflake.nix
File metadata and controls
455 lines (398 loc) · 22.5 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
{
description = "tkbar - GTK4 layer-shell status bar";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
systems.url = "github:nix-systems/default";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
dagger.url = "github:dagger/nix";
dagger.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, flake-utils, rust-overlay, dagger, ... }:
let
nixosModule = { config, lib, pkgs, ... }:
let
cfg = config.programs.tkbar;
bl = cfg.backlight;
backlightUdevRules = pkgs.writeTextDir
"etc/udev/rules.d/99-tkbar-backlight.rules"
''
ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="*", RUN+="${pkgs.coreutils}/bin/chgrp ${bl.group} /sys/class/backlight/%k/brightness"
ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="*", RUN+="${pkgs.coreutils}/bin/chmod g+rw /sys/class/backlight/%k/brightness"
'';
in
{
options.programs.tkbar = {
enable = lib.mkEnableOption "tkbar";
package = lib.mkOption {
type = lib.types.package;
default = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
defaultText = lib.literalMD "the flake's default package";
description = ''
The tkbar package to install.
Use `tkbar.packages.${pkgs.stdenv.hostPlatform.system}.default.override { ... }`
to select the compiled-in theme, compositor backend, and components.
'';
};
backlight = {
enable = lib.mkEnableOption ''
Install a udev rule giving a dedicated group write access to
/sys/class/backlight/*/brightness.
This avoids setuid binaries.
'';
group = lib.mkOption {
type = lib.types.str;
default = "tkbar-backlight";
description = ''
Group that will get write access to backlight brightness files.
You can also set this to "video" if you prefer using the
conventional Linux video group.
'';
};
users = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = ''
Users to add to the backlight group.
Example:
programs.tkbar.backlight.users = [ "alice" ];
'';
};
};
};
config = lib.mkIf cfg.enable (lib.mkMerge [
{
environment.systemPackages = [ cfg.package ];
}
(lib.mkIf bl.enable {
users.groups.${bl.group} = { };
services.udev.packages = [ backlightUdevRules ];
})
(lib.mkIf (bl.enable && bl.users != [ ]) {
users.groups.${bl.group}.members = bl.users;
})
]);
};
homeManagerModule = { config, lib, pkgs, ... }:
let
cfg = config.programs.tkbar;
toml = pkgs.formats.toml { };
settingsType = lib.types.submodule {
options = {
style = lib.mkOption {
type = lib.types.nullOr (lib.types.submodule {
options = {
position = lib.mkOption {
type = lib.types.nullOr (lib.types.enum [ "left" "right" "top" "bottom" ]);
default = null;
description = "Bar edge. `left`/`right` are vertical, `top`/`bottom` horizontal.";
};
bar_size_px = lib.mkOption {
type = lib.types.nullOr lib.types.ints.unsigned;
default = null;
description = "Bar thickness in pixels (width for vertical bars, height for horizontal ones).";
};
outputs = lib.mkOption {
type = lib.types.nullOr (lib.types.listOf lib.types.str);
default = null;
description = "Outputs to draw a bar on. Null or an empty list shows a bar on every output.";
};
};
});
default = null;
description = "Bar geometry.";
};
behaviour = lib.mkOption {
type = lib.types.nullOr (lib.types.submodule {
options = {
components = lib.mkOption {
type = lib.types.nullOr (lib.types.listOf (lib.types.either
(lib.types.enum [ "workspaces" "spacer" "battery" "wifi" "brightness" "volume" "clock" ])
(lib.types.submodule {
options.logo = lib.mkOption {
type = lib.types.str;
description = "Single glyph to display.";
};
})
));
default = null;
description = ''
Ordered widgets to render.
`workspaces` requires a compositor backend, and the
feature-gated components require the matching
compiled-in feature.
'';
};
should_show_empty_workspace = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = null;
description = "Show a button for empty workspaces.";
};
on_scroll_brightness_step = lib.mkOption {
type = lib.types.nullOr lib.types.ints.u8;
default = null;
description = "Percentage points added or removed per scroll notch on the brightness widget.";
};
on_scroll_volume_step = lib.mkOption {
type = lib.types.nullOr lib.types.ints.u8;
default = null;
description = "Percentage points added or removed per scroll notch on the volume widget.";
};
workspace_count = lib.mkOption {
type = lib.types.nullOr lib.types.ints.u8;
default = null;
description = "Number of workspace slots to synthesize when fewer real workspaces exist.";
};
};
});
default = null;
description = "Bar behaviour.";
};
security = lib.mkOption {
type = lib.types.nullOr (lib.types.submodule {
options.should_allow_workspace_label = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
default = null;
description = "Display compositor-provided workspace names, which are untrusted input.";
};
});
default = null;
description = "Security-related options.";
};
};
};
settings = lib.filterAttrsRecursive (_: value: value != null) cfg.settings;
hasSettings = settings != { };
in
{
options.programs.tkbar = {
enable = lib.mkEnableOption "tkbar";
package = lib.mkOption {
type = lib.types.package;
default = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
defaultText = lib.literalMD "the flake's default package";
description = ''
The tkbar package to install.
Use `tkbar.packages.${pkgs.stdenv.hostPlatform.system}.default.override { ... }`
to select the compiled-in theme, compositor backend, and components.
'';
};
settings = lib.mkOption {
type = settingsType;
default = { };
description = ''
Configuration written to
{file}`$XDG_CONFIG_HOME/tkbar/config.toml`.
Unset fields fall back to the built-in defaults.
See <https://github.com/tkr-sh/tkbar/blob/main/docs/CONFIGURATION.md>
for the available options.
'';
example = lib.literalExpression ''
{
style.position = "top";
behaviour.components = [ "workspaces" "spacer" "clock" ];
}
'';
};
style = lib.mkOption {
type = lib.types.nullOr (lib.types.either lib.types.path lib.types.lines);
default = null;
description = ''
User CSS written to
{file}`$XDG_CONFIG_HOME/tkbar/style.css`, or a path to
an existing CSS file.
See <https://github.com/tkr-sh/tkbar/blob/main/docs/CONFIGURATION.md#css-overloading>
for details.
'';
example = lib.literalExpression "./style.css";
};
systemd = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to install a systemd user service for tkbar.";
};
targets = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ config.wayland.systemd.target ];
defaultText = lib.literalExpression "[ config.wayland.systemd.target ]";
example = [ "niri-session.target" ];
description = ''
systemd user targets that start tkbar.
Make sure the compositor starts the selected target,
otherwise the service never runs.
'';
};
};
};
config = lib.mkIf cfg.enable (lib.mkMerge [
{
assertions = [
(lib.hm.assertions.assertPlatform "programs.tkbar" pkgs lib.platforms.linux)
];
home.packages = [ cfg.package ];
xdg.configFile."tkbar/config.toml" = lib.mkIf hasSettings {
source = toml.generate "tkbar-config.toml" settings;
};
xdg.configFile."tkbar/style.css" = lib.mkIf (cfg.style != null) {
source =
if builtins.isPath cfg.style || lib.isStorePath cfg.style
then cfg.style
else pkgs.writeText "tkbar-style.css" cfg.style;
};
}
(lib.mkIf cfg.systemd.enable {
systemd.user.services.tkbar = {
Unit = {
Description = "tkbar status bar";
Documentation = [ "https://github.com/tkr-sh/tkbar" ];
PartOf = cfg.systemd.targets;
After = cfg.systemd.targets;
ConditionEnvironment = "WAYLAND_DISPLAY";
X-Restart-Triggers =
lib.optional hasSettings config.xdg.configFile."tkbar/config.toml".source
++ lib.optional (cfg.style != null) config.xdg.configFile."tkbar/style.css".source;
};
Service = {
ExecStart = "${cfg.package}/bin/tkbar";
Restart = "on-failure";
};
Install.WantedBy = cfg.systemd.targets;
};
})
]);
};
in
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
# Nightly is only needed for the dev shell (advanced rustfmt/clippy);
# release artifacts are built with the stable toolchain.
rustPlatform = pkgs.makeRustPlatform {
cargo = pkgs.rust-bin.stable.latest.default;
rustc = pkgs.rust-bin.stable.latest.default;
};
in
{
devShells.default = with pkgs; mkShell {
buildInputs = [
(rust-bin.nightly.latest.default.override {
extensions = [
"rust-src"
"rust-analyzer"
"clippy"
"rustfmt"
];
})
just
nushell
taplo
watchexec
cargo-nextest
cargo-machete
cargo-deny
dart-sass
dagger.packages.${system}.dagger
# Nice utilities
fd
ripgrep
# GTK stack
gtk4
gtk4-layer-shell
glib
gsettings-desktop-schemas
pipewire
];
nativeBuildInputs = with pkgs; [
pkg-config
wrapGAppsHook4
rustPlatform.bindgenHook
];
shellHook = ''
export GSETTINGS_SCHEMA_DIR=${gsettings-desktop-schemas}/share/gsettings-schemas/${gsettings-desktop-schemas.name}:${gtk4}/share/gsettings-schemas/${gtk4.name}
'';
};
packages.dagger = dagger.packages.${system}.dagger;
# tkbar.packages.${system}.default.override {
# color = "purple"; # one of: black blue cyan green orange pink purple red white yellow
# compositor = "hyprland"; # one of: niri hyprland sway, or null for no compositor backend
# withConfig = false; # set false to drop the optional TOML/CSS config
# withCli = true; # set true to enable the clap CLI (--config, --help, --version)
# components = [ "battery" "brightness" "clock" "wifi" "volume" ]; # feature-gated components to build in
# }
packages.default = pkgs.lib.makeOverridable
({ color ? "black", compositor ? "niri", withConfig ? true, withCli ? false, components ? [ "battery" "brightness" "clock" "wifi" "volume" ] }:
let
# Component names mapping 1:1 to Cargo features.
allowedComponents = [ "battery" "brightness" "clock" "volume" "wifi" ];
allowedCompositors = [ "niri" "hyprland" "sway" ];
withVolume = builtins.elem "volume" components;
invalidComponents =
builtins.filter (c: !(builtins.elem c allowedComponents)) components;
invalidCompositor =
compositor != null && !(builtins.elem compositor allowedCompositors);
in
if invalidCompositor
then throw (
"tkbar: unsupported compositor backend '${toString compositor}'. "
+ "Allowed: " + builtins.concatStringsSep ", " allowedCompositors
+ ", or null."
)
else if invalidComponents != []
then throw (
"tkbar: unsupported component(s) in 'components': "
+ builtins.concatStringsSep ", " invalidComponents
+ ". Allowed: " + builtins.concatStringsSep ", " allowedComponents
)
else rustPlatform.buildRustPackage {
pname = "tkbar";
version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version;
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
buildNoDefaultFeatures = true;
buildFeatures = [ color ]
++ pkgs.lib.optional (compositor != null) compositor
++ pkgs.lib.optional withConfig "config"
++ pkgs.lib.optional withCli "cli"
++ components;
# Mirror `harden_gtk_env` in src/main.rs at the wrapper
# level as well (defense in depth): even if the in-binary
# calls were bypassed, the wrapper still disables image
# loaders, AT-SPI, GL/Vulkan and the X11 fallback.
# Pointing gdk-pixbuf at /dev/null keeps GTK from
# dlopening image loader modules (e.g. librsvg's SVG/XML
# parser) through gdk-pixbuf's default loader cache.
preFixup = ''
gappsWrapperArgs+=(
"--set" "GDK_PIXBUF_MODULE_FILE" "/dev/null"
"--set" "GTK_A11Y" "none"
"--set" "GSK_RENDERER" "cairo"
"--set" "GDK_DISABLE" "gl,vulkan,dmabuf"
"--set" "GDK_BACKEND" "wayland"
"--set" "GDK_DEBUG" "no-portals"
)
'';
nativeBuildInputs = with pkgs; [ pkg-config wrapGAppsHook4 ] ++ pkgs.lib.optional withVolume rustPlatform.bindgenHook;
buildInputs = with pkgs; [
gtk4
gtk4-layer-shell
glib
gsettings-desktop-schemas
]
++ pkgs.lib.optional withVolume pipewire;
})
{};
apps.default = {
type = "app";
program = "${self.packages.${system}.default}/bin/tkbar";
};
}) // {
nixosModules.default = nixosModule;
nixosModules.tkbar = nixosModule;
homeModules.default = homeManagerModule;
homeModules.tkbar = homeManagerModule;
};
}