-
Notifications
You must be signed in to change notification settings - Fork 10
/
flake.nix
476 lines (427 loc) · 16.4 KB
/
flake.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
{
description = "A scrollable-tiling Wayland compositor.";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
crate2nix.url = "github:nix-community/crate2nix";
niri-unstable.url = "github:YaLTeR/niri";
niri-unstable.flake = false;
niri-stable.url = "github:YaLTeR/niri/v0.1.4";
niri-stable.flake = false;
};
outputs = inputs @ {
self,
flake-parts,
crate2nix,
niri-unstable,
niri-stable,
nixpkgs,
...
}: let
call = nixpkgs.lib.flip import {
inherit inputs kdl docs binds;
inherit (nixpkgs) lib;
};
kdl = call ./kdl.nix;
binds = call ./parse-binds.nix;
docs = call ./generate-docs.nix;
settings = call ./settings.nix;
stylix-module = call ./stylix.nix;
stable-revs = import ./refs.nix;
date = {
year = builtins.substring 0 4;
month = builtins.substring 4 2;
day = builtins.substring 6 2;
hour = builtins.substring 8 2;
minute = builtins.substring 10 2;
second = builtins.substring 12 2;
};
fmt-date = raw: "${date.year raw}-${date.month raw}-${date.day raw}";
fmt-time = raw: "${date.hour raw}:${date.minute raw}:${date.second raw}";
version-string = orig: src:
if stable-revs ? ${src.rev}
then "stable ${orig.version}"
else "unstable ${fmt-date src.lastModifiedDate} (commit ${src.rev})";
package-version = orig: src:
if stable-revs ? ${src.rev}
then orig.version
else "${orig.version}-unstable-${src.shortRev}";
make-niri = nixpkgs.lib.makeOverridable ({
src,
pkgs,
patches ? [],
}: let
tools = crate2nix.tools.${pkgs.stdenv.system};
manifest = tools.generatedCargoNix {
inherit src;
name = "niri";
};
workspace = import manifest {
inherit pkgs;
buildRustCrateForPkgs = pkgs:
pkgs.buildRustCrate.override {
defaultCrateOverrides =
pkgs.defaultCrateOverrides
// (with pkgs; {
# Q: Why do we need to override these?
# (nixpkgs)/(niri's dev flake) doesn't do this!
#
# A: crate2nix builds each crate in a separate derivation.
# This is to avoid building the same crate multiple times.
# Ultimately, that speeds up the build.
# But it also means that each crate has separate build inputs.
#
# Many popular crates have "default overrides" in nixpkgs.
# But it doesn't cover all crates niri depends on.
# So we need to fix those last few ourselves.
#
# (nixpkgs)/(niri's dev flake) uses `cargo` to build.
# And this builds all crates in the same derivation.
# That's why they don't override individual crates.
libspa-sys = attrs: {
nativeBuildInputs = [pkg-config rustPlatform.bindgenHook];
buildInputs = [pipewire];
};
libspa = attrs: {
nativeBuildInputs = [pkg-config];
buildInputs = [pipewire];
};
pipewire-sys = attrs: {
nativeBuildInputs = [pkg-config rustPlatform.bindgenHook];
buildInputs = [pipewire];
};
gobject-sys = attrs: {
nativeBuildInputs = [pkg-config glib];
};
gio-sys = attrs: {
nativeBuildInputs = [pkg-config glib];
};
# For all niri crates, the hash of the source is different in CI than on my system.
# KiaraGrouwstra reports identical hash to my system, so it really is only in CI.
#
# We suspect it might be due to the fact that CI uses a different version of nix.
# But that shouldn't matter, because the hash is not derived from the nix version used!
# It might also be some symptom of import-from-derivation, but i don't care to investigate.
#
# Ultimately, the solution looks stupid, but it does work:
# Just override `src` attr to be the correct path based on the `src` argument.
# This causes them to be predictable and based on the flake inputs, which is what we want.
#
# Everything builds the same way without this. But the hash is different.
# And for binary caching to work, the hash must be identical.
#
# ---
#
# I'm also overriding the version.
# This is unrelated to reproducibility, but looks similarly weird to src
# The reason for overriding the version is:
#
# For stable: follow nix convention `X.Y.Z` instead of `vX.Y.Z`
#
# For unstable: include the date and commit hash;
# => otherwise tools like `nix profile diff-closures` will miss differences in the niri version
# and might show an empty diff, even when niri version (commit) changes
niri-ipc = attrs: {
src = "${src}/niri-ipc";
version = package-version attrs src;
};
niri-config = attrs: {
src = "${src}/niri-config";
version = package-version attrs src;
postPatch = "substituteInPlace src/lib.rs --replace ../.. ${src}";
};
niri = attrs: {
src = "${src}";
version = package-version attrs src;
inherit patches;
postPatch =
"substituteInPlace src/utils/mod.rs --replace "
+ nixpkgs.lib.escapeShellArgs [
''pub fn version() -> String {''
''
#[allow(unreachable_code)]
pub fn version() -> String {
return "${version-string attrs src}".into();
''
];
buildInputs = [libxkbcommon libinput mesa libglvnd wayland pixman];
# we want backtraces to be readable
dontStrip = true;
extraRustcOpts = [
"-C link-arg=-Wl,--push-state,--no-as-needed"
"-C link-arg=-lEGL"
"-C link-arg=-lwayland-client"
"-C link-arg=-Wl,--pop-state"
"-C debuginfo=line-tables-only"
# "/source/" is not very readable. "./" is better, and it matches default behaviour of cargo.
"--remap-path-prefix $NIX_BUILD_TOP/source=./"
];
passthru.providedSessions = ["niri"];
postInstall = ''
mkdir -p $out/share/systemd/user
mkdir -p $out/share/wayland-sessions
mkdir -p $out/share/xdg-desktop-portal
cp ${src}/resources/niri-session $out/bin/niri-session
cp ${src}/resources/niri.service $out/share/systemd/user/niri.service
cp ${src}/resources/niri-shutdown.target $out/share/systemd/user/niri-shutdown.target
cp ${src}/resources/niri.desktop $out/share/wayland-sessions/niri.desktop
cp ${src}/resources/niri-portals.conf $out/share/xdg-desktop-portal/niri-portals.conf
'';
postFixup = "substituteInPlace $out/share/systemd/user/niri.service --replace /usr $out";
};
});
};
};
in
workspace.workspaceMembers.niri.build
// {
binds = abort "<package>.binds has been removed. use config.lib.niri.actions instead. it works even when using niri from nixpkgs.";
inherit workspace;
});
make-niri-stable = pkgs:
make-niri {
inherit pkgs;
src = niri-stable;
patches = [];
};
make-niri-unstable = pkgs:
make-niri {
inherit pkgs;
src = niri-unstable;
};
validated-config-for = pkgs: package: config:
pkgs.runCommand "config.kdl" {
inherit config;
passAsFile = ["config"];
buildInputs = [package];
} ''
niri validate -c $configPath
cp $configPath $out
'';
in
flake-parts.lib.mkFlake {inherit inputs;} {
systems = ["x86_64-linux" "aarch64-linux"];
perSystem = {
self',
config,
system,
pkgs,
...
}: {
packages = {
niri-unstable = make-niri-unstable pkgs;
niri-stable = make-niri-stable pkgs;
};
apps = {
niri-stable = {
type = "app";
program = "${self'.packages.niri-stable}/bin/niri";
};
niri-unstable = {
type = "app";
program = "${self'.packages.niri-unstable}/bin/niri";
};
default = self'.apps.niri-stable;
};
checks = {
empty-config-valid-stable = let
eval = nixpkgs.lib.evalModules {
modules = [
settings.module
{
config.programs.niri.settings = {};
}
];
};
in
validated-config-for pkgs self'.packages.niri-stable eval.config.programs.niri.finalConfig;
};
formatter = pkgs.alejandra;
};
flake = {
__docs = docs.make-docs (settings.fake-docs {inherit fmt-date fmt-time nixpkgs;});
inherit kdl;
overlays.niri = final: prev: {
niri-unstable = make-niri-unstable final;
niri-stable = make-niri-stable final;
};
homeModules.stylix = stylix-module;
homeModules.config = {
lib,
config,
pkgs,
...
}:
with lib; let
cfg = config.programs.niri;
in {
imports = [
settings.module
];
options.programs.niri = {
package = mkOption {
type = types.package;
default = make-niri-stable pkgs;
};
};
config.lib.niri = {
actions = mergeAttrsList (map ({
name,
fn,
...
}: {
${name} = fn;
}) (binds cfg.package.src));
};
config.xdg.configFile.niri-config = {
enable = cfg.finalConfig != null;
target = "niri/config.kdl";
source = validated-config-for pkgs cfg.package cfg.finalConfig;
};
config.warnings =
pipe {
# the prefix here helps ensure that the cartesian product is taken in the correct order
a_decoration = ["border" "focus-ring"];
b_state = ["active" "inactive"];
c_field = ["color" "gradient"];
} [
cartesianProductOfSets
(map (concatMapAttrs (name: value: {${substring 2 (stringLength name - 2) name} = value;})))
(filter ({
decoration,
state,
field,
}:
cfg.settings.layout.${decoration}."${state}-${field}" or null != null))
(used:
mkIf (used != []) [
''
Usage of deprecated options:
${concatStrings (forEach used ({
decoration,
state,
field,
...
}: ''
- `programs.niri.settings.layout.${decoration}.${state}-${field}`
''))}
They will be removed in a future version.
The reasoning for this is that the previous structure is incorrectly typed.
They are superseded by the following options:
${concatStrings (forEach used ({
decoration,
state,
field,
...
}: ''
- `programs.niri.settings.layout.${decoration}.${state}.${field}`
''))}
Note that you cannot set `color` and `gradient` for the same field anymore.
Previously, the gradient always took priority when non-null.
''
])
];
};
nixosModules.niri = {
lib,
config,
options,
pkgs,
...
}: let
cfg = config.programs.niri;
in
with lib; {
options.programs.niri = {
enable = mkEnableOption "niri";
package = mkOption {
type = types.package;
default = make-niri-stable pkgs;
};
};
options.niri-flake.cache.enable = mkOption {
type = types.bool;
default = true;
};
config = mkMerge [
(mkIf config.niri-flake.cache.enable {
nix.settings = {
substituters = ["https://niri.cachix.org"];
trusted-public-keys = ["niri.cachix.org-1:Wv0OmO7PsuocRKzfDoJ3mulSl7Z6oezYhGhR+3W2964="];
};
})
{
environment.systemPackages = [pkgs.xdg-utils];
xdg = {
autostart.enable = mkDefault true;
menus.enable = mkDefault true;
mime.enable = mkDefault true;
icons.enable = mkDefault true;
};
}
(mkIf cfg.enable {
environment.systemPackages = [cfg.package];
services.xserver.displayManager.sessionPackages = [cfg.package];
xdg.portal = {
enable = true;
extraPortals = [pkgs.xdg-desktop-portal-gnome];
configPackages = [cfg.package];
};
security.polkit.enable = true;
services.gnome.gnome-keyring.enable = true;
systemd.user.services.niri-flake-polkit = {
description = "PolicyKit Authentication Agent provided by niri-flake";
wantedBy = ["niri.service"];
wants = ["graphical-session.target"];
after = ["graphical-session.target"];
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.libsForQt5.polkit-kde-agent}/libexec/polkit-kde-authentication-agent-1";
Restart = "on-failure";
RestartSec = 1;
TimeoutStopSec = 10;
};
};
security.pam.services.swaylock = {};
hardware.opengl.enable = mkDefault true;
programs.dconf.enable = mkDefault true;
fonts.enableDefaultPackages = mkDefault true;
})
(optionalAttrs (options ? home-manager) {
home-manager.sharedModules =
[
self.homeModules.config
{programs.niri.package = mkForce cfg.package;}
]
++ optionals (options ? stylix) [self.homeModules.stylix];
})
];
};
homeModules.niri = {
lib,
config,
pkgs,
...
}:
with lib; let
cfg = config.programs.niri;
in {
imports = [
self.homeModules.config
];
options.programs.niri = {
enable = mkEnableOption "niri";
};
config = mkIf cfg.enable {
home.packages = [cfg.package];
services.gnome-keyring.enable = true;
xdg.portal = {
enable = true;
extraPortals = [pkgs.xdg-desktop-portal-gnome];
configPackages = [cfg.package];
};
};
};
};
};
}