atagen

monolith

public
Star0
Watch0
ec80bfaf4c68f1de229668de937b42d38f44e2ed
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git
flake.nixNix219 lines6.3 KB
1
{
2
  inputs = {
3
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
4
    crane.url = "github:ipetkov/crane";
5
  };
6
7
  outputs =
8
    {
9
      self,
10
      nixpkgs,
11
      crane,
12
    }:
13
    let
14
      lib = nixpkgs.lib;
15
      systems = [
16
        "x86_64-linux"
17
        "aarch64-linux"
18
      ];
19
      forAllSystems = f: lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
20
21
      mkProject =
22
        pkgs:
23
        let
24
          craneLib = crane.mkLib pkgs;
25
          src = lib.cleanSourceWith {
26
            src = ./.;
27
            filter =
28
              path: type:
29
              (craneLib.filterCargoSources path type) || lib.hasInfix "/shaders" path || lib.hasInfix "/res" path;
30
            name = "source";
31
          };
32
          lavapipeIcd = "${pkgs.mesa}/share/vulkan/icd.d/lvp_icd.${pkgs.stdenv.hostPlatform.parsed.cpu.name}.json";
33
          commonArgs = {
34
            inherit src;
35
            pname = "monolith";
36
            version = "0.0.0";
37
            strictDeps = true;
38
            nativeBuildInputs = [
39
              pkgs.pkg-config
40
              pkgs.glslang
41
            ];
42
            buildInputs = with pkgs; [
43
              libxkbcommon
44
              vulkan-loader
45
              libinput
46
              seatd
47
              libgbm
48
              libdrm
49
              udev
50
            ];
51
          };
52
          cargoArtifacts = craneLib.buildDepsOnly commonArgs;
53
        in
54
        {
55
          inherit craneLib lavapipeIcd commonArgs;
56
57
          package = craneLib.buildPackage (
58
            commonArgs
59
            // {
60
              inherit cargoArtifacts;
61
              doCheck = false;
62
              postInstall = ''
63
                install -Dm644 res/monolith-portals.conf -t $out/share/xdg-desktop-portal
64
                install -Dm644 res/monolith.desktop -t $out/share/wayland-sessions
65
              '';
66
              # ash opens libvulkan at runtime, so it is not in the linked rpath.
67
              postFixup = ''
68
                patchelf --add-rpath ${lib.makeLibraryPath [ pkgs.vulkan-loader ]} $out/bin/monolith
69
              '';
70
              passthru.providedSessions = [ "monolith" ];
71
              meta = {
72
                description = "A tiling Wayland compositor";
73
                license = lib.licenses.eupl12;
74
                platforms = lib.platforms.linux;
75
                mainProgram = "monolith";
76
              };
77
            }
78
          );
79
80
          checks = {
81
            clippy = craneLib.cargoClippy (
82
              commonArgs
83
              // {
84
                inherit cargoArtifacts;
85
                cargoClippyExtraArgs = "--all-targets -- --deny warnings";
86
              }
87
            );
88
            fmt = craneLib.cargoFmt {
89
              inherit src;
90
              pname = "monolith";
91
              version = "0.0.0";
92
            };
93
            test = craneLib.cargoTest (
94
              commonArgs
95
              // {
96
                inherit cargoArtifacts;
97
                VK_DRIVER_FILES = lavapipeIcd;
98
                LD_LIBRARY_PATH = lib.makeLibraryPath [ pkgs.vulkan-loader ];
99
              }
100
            );
101
          };
102
        };
103
      nixosModule =
104
        {
105
          config,
106
          lib,
107
          pkgs,
108
          ...
109
        }:
110
        let
111
          inherit (lib)
112
            getExe
113
            literalExpression
114
            mkDefault
115
            mkEnableOption
116
            mkIf
117
            mkOption
118
            optional
119
            types
120
            ;
121
122
          cfg = config.programs.monolith;
123
        in
124
        {
125
          options.programs.monolith = {
126
            enable = mkEnableOption "monolith, a tiling Wayland compositor";
127
128
            package = mkOption {
129
              type = types.package;
130
              default = (mkProject pkgs).package;
131
              defaultText = literalExpression "monolith.packages.\${system}.default";
132
              description = "The monolith package to use.";
133
            };
134
135
            realtime-scheduling = mkOption {
136
              type = types.bool;
137
              default = true;
138
              description = ''
139
                Wrap the monolith binary with CAP_SYS_NICE so its compositor thread runs
140
                SCHED_RR and its Vulkan queue gets realtime global priority. monolith clears
141
                the capability from everything it launches.
142
              '';
143
            };
144
145
            xwayland.enable = mkEnableOption "Xwayland, started on the first X11 client" // {
146
              default = true;
147
            };
148
          };
149
150
          config = mkIf cfg.enable {
151
            environment.systemPackages = optional (!cfg.realtime-scheduling) cfg.package;
152
153
            programs = {
154
              dconf.enable = mkDefault true;
155
              xwayland.enable = mkIf cfg.xwayland.enable (mkDefault true);
156
            };
157
158
            security = {
159
              polkit.enable = true;
160
              pam.services.swaylock = { };
161
162
              wrappers = mkIf cfg.realtime-scheduling {
163
                monolith = {
164
                  owner = "root";
165
                  group = "root";
166
                  permissions = "a+rx";
167
                  source = getExe cfg.package;
168
                  capabilities = "cap_sys_nice+p";
169
                };
170
              };
171
            };
172
173
            xdg.portal = {
174
              enable = mkDefault true;
175
              configPackages = mkDefault [ cfg.package ];
176
              extraPortals = [
177
                pkgs.xdg-desktop-portal-gtk
178
                pkgs.xdg-desktop-portal-wlr
179
              ];
180
            };
181
182
            services.displayManager.sessionPackages = [ cfg.package ];
183
          };
184
        };
185
    in
186
    {
187
      packages = forAllSystems (pkgs: {
188
        default = (mkProject pkgs).package;
189
      });
190
191
      overlays.default = final: _: { monolith = (mkProject final).package; };
192
193
      nixosModules.default = nixosModule;
194
195
      checks = forAllSystems (pkgs: (mkProject pkgs).checks);
196
197
      devShells = forAllSystems (
198
        pkgs:
199
        let
200
          project = mkProject pkgs;
201
        in
202
        {
203
          default = project.craneLib.devShell {
204
            inputsFrom = [ project.package ];
205
            packages = with pkgs; [
206
              rust-analyzer
207
              vulkan-tools
208
              vulkan-validation-layers
209
              tracy
210
            ];
211
            LD_LIBRARY_PATH = lib.makeLibraryPath [ pkgs.vulkan-loader ];
212
            LAVAPIPE_ICD = project.lavapipeIcd;
213
          };
214
        }
215
      );
216
217
      formatter = forAllSystems (pkgs: pkgs.nixfmt);
218
    };
219
}