aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: 38477df3bb81225b62745ef4f918961000cd4a16 (plain)
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
# This flake file is community maintained
{
  description = "Niri: A scrollable-tiling Wayland compositor.";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    nix-filter.url = "github:numtide/nix-filter";

    # NOTE: This is not necessary for end users
    # You can omit it with `inputs.rust-overlay.follows = ""`
    rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs =
    {
      self,
      nixpkgs,
      nix-filter,
      rust-overlay,
    }:
    let
      inherit (nixpkgs) lib;
      # Support all Linux systems that the nixpkgs flake exposes
      systems = lib.intersectLists lib.systems.flakeExposed lib.platforms.linux;

      forAllSystems = lib.genAttrs systems;
      nixpkgsFor = forAllSystems (system: nixpkgs.legacyPackages.${system});
    in
    {
      checks = forAllSystems (system: {
        # We use the debug build here to save a bit of time
        inherit (self.packages.${system}) niri-debug;
      });

      devShells = forAllSystems (
        system:
        let
          pkgs = nixpkgsFor.${system};
          inherit (self.packages.${system}) niri;
        in
        {
          default = pkgs.mkShell {
            packages = [
              # NOTE: Nixpkgs' Rust toolchain isn't used here as we prefer
              # a nightly toolchain for development, and *require* a nightly
              # `rustfmt`
              rust-overlay.packages.${system}.rust-nightly

              pkgs.rust-analyzer
            ];

            nativeBuildInputs = [
              pkgs.clang
              pkgs.pkg-config
              pkgs.wrapGAppsHook4 # For `niri-visual-tests`
            ];

            buildInputs = niri.buildInputs ++ [
              pkgs.libadwaita # For `niri-visual-tests`
            ];

            env = {
              inherit (niri) LIBCLANG_PATH;

              # WARN: Do not overwrite this variable in your shell!
              # It is required for `dlopen()` to work on some libraries; see the comment
              # in the package expression
              #
              # This should only be set with `CARGO_BUILD_RUSTFLAGS="$CARGO_BUILD_RUSTFLAGS -C your-flags"`
              inherit (niri) CARGO_BUILD_RUSTFLAGS;
            };
          };
        }
      );

      formatter = forAllSystems (system: nixpkgsFor.${system}.nixfmt-rfc-style);

      packages = forAllSystems (
        system:
        let
          inherit (self.overlays.default nixpkgsFor.${system} null) niri;
        in
        {
          inherit niri;

          # NOTE: This is for development purposes only
          #
          # It is primarily to help with quickly iterating on
          # changes made to the above expression - though it is
          # also not stripped in order to better debug niri itself
          niri-debug = niri.overrideAttrs (
            newAttrs: oldAttrs: {
              pname = oldAttrs.pname + "-debug";

              cargoBuildType = "debug";
              cargoCheckType = newAttrs.cargoBuildType;

              dontStrip = true;
            }
          );

          default = niri;
        }
      );

      overlays.default = final: _: {
        niri = final.callPackage (
          {
            cairo,
            clang,
            dbus,
            libGL,
            libclang,
            libdisplay-info,
            libinput,
            libseat,
            libxkbcommon,
            mesa,
            pango,
            pipewire,
            pkg-config,
            rustPlatform,
            systemd,
            wayland,
            withDbus ? true,
            withSystemd ? true,
            withScreencastSupport ? true,
            withDinit ? false,
          }:

          rustPlatform.buildRustPackage {
            pname = "niri";
            version = self.shortRev or self.dirtyShortRev or "unknown";

            src = nix-filter.lib.filter {
              root = self;
              include = [
                "niri-config"
                "niri-ipc"
                "niri-visual-tests"
                "resources"
                "src"
                ./Cargo.lock
                ./Cargo.toml
              ];
            };

            postPatch = ''
              patchShebangs resources/niri-session
              substituteInPlace resources/niri.service \
                --replace-fail '/usr/bin' "$out/bin"
            '';

            cargoLock = {
              # NOTE: This is only used for Git dependencies
              allowBuiltinFetchGit = true;
              lockFile = ./Cargo.lock;
            };

            strictDeps = true;

            nativeBuildInputs = [
              clang
              pkg-config
            ];

            buildInputs =
              [
                cairo
                dbus
                libGL
                libdisplay-info
                libinput
                libseat
                libxkbcommon
                mesa # libgbm
                pango
                wayland
              ]
              ++ lib.optional (withDbus || withScreencastSupport || withSystemd) dbus
              ++ lib.optional withScreencastSupport pipewire
              # Also includes libudev
              ++ lib.optional withSystemd systemd;

            buildFeatures =
              lib.optional withDbus "dbus"
              ++ lib.optional withDinit "dinit"
              ++ lib.optional withScreencastSupport "xdp-gnome-screencast"
              ++ lib.optional withSystemd "systemd";
            buildNoDefaultFeatures = true;

            postInstall =
              ''
                install -Dm644 resources/niri.desktop -t $out/share/wayland-sessions
                install -Dm644 resources/niri-portals.conf -t $out/share/xdg-desktop-portal
              ''
              + lib.optionalString withSystemd ''
                install -Dm755 resources/niri-session $out/bin/niri-session
                install -Dm644 resources/niri{.service,-shutdown.target} -t $out/share/systemd/user
              '';

            env = {
              LIBCLANG_PATH = lib.getLib libclang + "/lib";

              # Force linking with libEGL and libwayland-client
              # so they can be discovered by `dlopen()`
              CARGO_BUILD_RUSTFLAGS = toString (
                map (arg: "-C link-arg=" + arg) [
                  "-Wl,--push-state,--no-as-needed"
                  "-lEGL"
                  "-lwayland-client"
                  "-Wl,--pop-state"
                ]
              );
            };

            passthru = {
              providedSessions = [ "niri" ];
            };

            meta = {
              description = "Scrollable-tiling Wayland compositor";
              homepage = "https://github.com/YaLTeR/niri";
              license = lib.licenses.gpl3Only;
              mainProgram = "niri";
              platforms = lib.platforms.linux;
            };
          }
        ) { };
      };
    };
}