From d20837bef8a41d6bf1732b0e8968b7f2feb9a93f Mon Sep 17 00:00:00 2001
From: Jacob Birkett <spikespaz@outlook.com>
Date: Sat, 12 Aug 2023 07:22:37 -0500
Subject: [PATCH] Nix: corrections for overlays, overrideable systems (#2929)

* nix: overlays: move waybar-hyprland to own overlay

* flake: use legacyPackages for formatter

Run `nix fmt` for all files.

* flake: move default overlay to nix/overlays.nix

* nix: lib: remove lib

* nix: overlays: extras: explicitly include xdph overlays

* nix: use interpolation for versions

* nix: overlays: include deps with hyprland-packages

* flake: make systems overrideable

* flake: packages: inherit from overlaid pkgsFor
---
 flake.lock        |  34 ++++++++++++---
 flake.nix         |  73 ++++++++++++++++---------------
 nix/default.nix   |   2 +-
 nix/hm-module.nix |  26 ++++++++---
 nix/lib.nix       |   8 ----
 nix/overlays.nix  | 107 ++++++++++++++++++++++++----------------------
 6 files changed, 143 insertions(+), 107 deletions(-)
 delete mode 100644 nix/lib.nix

diff --git a/flake.lock b/flake.lock
index 45fa80109..33b7b10c5 100644
--- a/flake.lock
+++ b/flake.lock
@@ -4,14 +4,17 @@
       "inputs": {
         "nixpkgs": [
           "nixpkgs"
+        ],
+        "systems": [
+          "systems"
         ]
       },
       "locked": {
-        "lastModified": 1684265364,
-        "narHash": "sha256-AxNnWbthsuNx73HDQr0eBxrcE3+yfl/WsaXZqUFmkpQ=",
+        "lastModified": 1691753796,
+        "narHash": "sha256-zOEwiWoXk3j3+EoF3ySUJmberFewWlagvewDRuWYAso=",
         "owner": "hyprwm",
         "repo": "hyprland-protocols",
-        "rev": "8c279b9fb0f2b031427dc5ef4eab53f2ed835530",
+        "rev": "0c2ce70625cb30aef199cb388f99e19a61a6ce03",
         "type": "github"
       },
       "original": {
@@ -40,10 +43,26 @@
       "inputs": {
         "hyprland-protocols": "hyprland-protocols",
         "nixpkgs": "nixpkgs",
+        "systems": "systems",
         "wlroots": "wlroots",
         "xdph": "xdph"
       }
     },
+    "systems": {
+      "locked": {
+        "lastModified": 1689347949,
+        "narHash": "sha256-12tWmuL2zgBgZkdoB6qXZsgJEH9LR3oUgpaQq2RbI80=",
+        "owner": "nix-systems",
+        "repo": "default-linux",
+        "rev": "31732fcf5e8fea42e59c2488ad31a0e651500f68",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nix-systems",
+        "repo": "default-linux",
+        "type": "github"
+      }
+    },
     "wlroots": {
       "flake": false,
       "locked": {
@@ -70,14 +89,17 @@
         ],
         "nixpkgs": [
           "nixpkgs"
+        ],
+        "systems": [
+          "systems"
         ]
       },
       "locked": {
-        "lastModified": 1691082525,
-        "narHash": "sha256-C5AO0KnyAFJaCkOn+5nJfWm0kyiPn/Awh0lKTjhgr7Y=",
+        "lastModified": 1691841170,
+        "narHash": "sha256-RCTm1/MVWYPnReMgyp7tr2ogGYo/pvw38jZaFwemgPU=",
         "owner": "hyprwm",
         "repo": "xdg-desktop-portal-hyprland",
-        "rev": "42747d267ab4345c4ceb78cd4a4fe99f072d80fc",
+        "rev": "57a3a41ba6b358109e4fc25c6a4706b5f7d93c6b",
         "type": "github"
       },
       "original": {
diff --git a/flake.nix b/flake.nix
index 5908f8c40..33643b47b 100644
--- a/flake.nix
+++ b/flake.nix
@@ -4,6 +4,9 @@
   inputs = {
     nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
 
+    # <https://github.com/nix-systems/nix-systems>
+    systems.url = "github:nix-systems/default-linux";
+
     wlroots = {
       type = "gitlab";
       host = "gitlab.freedesktop.org";
@@ -16,11 +19,13 @@
     hyprland-protocols = {
       url = "github:hyprwm/hyprland-protocols";
       inputs.nixpkgs.follows = "nixpkgs";
+      inputs.systems.follows = "systems";
     };
 
     xdph = {
       url = "github:hyprwm/xdg-desktop-portal-hyprland";
       inputs.nixpkgs.follows = "nixpkgs";
+      inputs.systems.follows = "systems";
       inputs.hyprland-protocols.follows = "hyprland-protocols";
     };
   };
@@ -28,39 +33,23 @@
   outputs = inputs @ {
     self,
     nixpkgs,
+    systems,
     ...
   }: let
-    lib = nixpkgs.lib.extend (import ./nix/lib.nix);
-    genSystems = lib.genAttrs [
-      # Add more systems if they are supported
-      "aarch64-linux"
-      "x86_64-linux"
-    ];
-
-    pkgsFor = genSystems (system:
+    inherit (nixpkgs) lib;
+    eachSystem = lib.genAttrs (import systems);
+    pkgsFor = eachSystem (system:
       import nixpkgs {
-        inherit system;
-        overlays = [
-          self.overlays.hyprland-packages
-          self.overlays.wlroots-hyprland
-          inputs.hyprland-protocols.overlays.default
+        localSystem = system;
+        overlays = with self.overlays; [
+          hyprland-packages
+          hyprland-extras
         ];
       });
   in {
-    overlays =
-      (import ./nix/overlays.nix {inherit self lib inputs;})
-      // {
-        default =
-          lib.mkJoinedOverlays
-          (with self.overlays; [
-            hyprland-packages
-            hyprland-extras
-            wlroots-hyprland
-            inputs.hyprland-protocols.overlays.default
-          ]);
-      };
+    overlays = import ./nix/overlays.nix {inherit self lib inputs;};
 
-    checks = genSystems (system:
+    checks = eachSystem (system:
       (lib.filterAttrs
         (n: _: (lib.hasPrefix "hyprland" n) && !(lib.hasSuffix "debug" n))
         self.packages.${system})
@@ -68,16 +57,32 @@
         inherit (self.packages.${system}) xdg-desktop-portal-hyprland;
       });
 
-    packages = genSystems (system:
-      (self.overlays.default pkgsFor.${system} pkgsFor.${system})
-      // {
-        default = self.packages.${system}.hyprland;
-      });
+    packages = eachSystem (system: {
+      default = self.packages.${system}.hyprland;
+      inherit
+        (pkgsFor.${system})
+        # hyprland-packages
+        hyprland
+        hyprland-unwrapped
+        hyprland-debug
+        hyprland-hidpi
+        hyprland-nvidia
+        hyprland-no-hidpi
+        # hyprland-extras
+        xdg-desktop-portal-hyprland
+        hyprland-share-picker
+        waybar-hyprland
+        # dependencies
+        hyprland-protocols
+        wlroots-hyprland
+        udis86
+        ;
+    });
 
-    devShells = genSystems (system: {
+    devShells = eachSystem (system: {
       default = pkgsFor.${system}.mkShell {
         name = "hyprland-shell";
-        nativeBuildInputs = with pkgsFor.${system}; [ cmake python3 ];
+        nativeBuildInputs = with pkgsFor.${system}; [cmake python3];
         buildInputs = [self.packages.${system}.wlroots-hyprland];
         inputsFrom = [
           self.packages.${system}.wlroots-hyprland
@@ -86,7 +91,7 @@
       };
     });
 
-    formatter = genSystems (system: pkgsFor.${system}.alejandra);
+    formatter = eachSystem (system: nixpkgs.legacyPackages.${system}.alejandra);
 
     nixosModules.default = import ./nix/module.nix inputs;
     homeManagerModules.default = import ./nix/hm-module.nix self;
diff --git a/nix/default.nix b/nix/default.nix
index 8c3c63642..3fb3545bf 100644
--- a/nix/default.nix
+++ b/nix/default.nix
@@ -123,7 +123,7 @@ in
         ln -s ${wlroots}/include/wlr $dev/include/hyprland/wlroots
         ${lib.optionalString wrapRuntimeDeps ''
           wrapProgram $out/bin/Hyprland \
-            --suffix PATH : ${lib.makeBinPath [ binutils pciutils ]}
+            --suffix PATH : ${lib.makeBinPath [binutils pciutils]}
         ''}
       '';
 
diff --git a/nix/hm-module.nix b/nix/hm-module.nix
index 133c0ef6d..705ddf860 100644
--- a/nix/hm-module.nix
+++ b/nix/hm-module.nix
@@ -119,10 +119,14 @@ in {
 
   config = lib.mkIf cfg.enable {
     warnings =
-      if (cfg.systemdIntegration || cfg.plugins != []) && cfg.extraConfig == null then
-        [ ''You have enabled hyprland.systemdIntegration or listed plugins in hyprland.plugins.
-            Your Hyprland config will be linked by home manager.
-            Set hyprland.extraConfig or unset hyprland.systemdIntegration and hyprland.plugins to remove this warning.'' ]
+      if (cfg.systemdIntegration || cfg.plugins != []) && cfg.extraConfig == null
+      then [
+        ''
+          You have enabled hyprland.systemdIntegration or listed plugins in hyprland.plugins.
+          Your Hyprland config will be linked by home manager.
+          Set hyprland.extraConfig or unset hyprland.systemdIntegration and hyprland.plugins to remove this warning.
+        ''
+      ]
       else [];
 
     home.packages =
@@ -138,9 +142,17 @@ in {
           exec-once=${pkgs.dbus}/bin/dbus-update-activation-environment --systemd DISPLAY WAYLAND_DISPLAY HYPRLAND_INSTANCE_SIGNATURE XDG_CURRENT_DESKTOP && systemctl --user start hyprland-session.target
         '')
         + lib.concatStrings (builtins.map (entry: let
-            plugin = if lib.types.package.check entry then "${entry}/lib/lib${entry.pname}.so" else entry;
-          in "plugin = ${plugin}\n") cfg.plugins)
-        + (if cfg.extraConfig != null then cfg.extraConfig else "");
+          plugin =
+            if lib.types.package.check entry
+            then "${entry}/lib/lib${entry.pname}.so"
+            else entry;
+        in "plugin = ${plugin}\n")
+        cfg.plugins)
+        + (
+          if cfg.extraConfig != null
+          then cfg.extraConfig
+          else ""
+        );
 
       onChange = let
         hyprlandPackage =
diff --git a/nix/lib.nix b/nix/lib.nix
deleted file mode 100644
index 1a4133522..000000000
--- a/nix/lib.nix
+++ /dev/null
@@ -1,8 +0,0 @@
-final: prev: let
-  lib = final;
-
-  mkJoinedOverlays = overlays: final: prev:
-    lib.foldl' (attrs: overlay: attrs // (overlay final prev)) {} overlays;
-in prev // {
-  inherit mkJoinedOverlays;
-}
diff --git a/nix/overlays.nix b/nix/overlays.nix
index 8a61fa104..6516c755c 100644
--- a/nix/overlays.nix
+++ b/nix/overlays.nix
@@ -10,70 +10,75 @@
     (builtins.substring 4 2 longDate)
     (builtins.substring 6 2 longDate)
   ]);
+
+  mkJoinedOverlays = overlays: final: prev:
+    lib.foldl' (attrs: overlay: attrs // (overlay final prev)) {} overlays;
 in {
-  # Packages for variations of Hyprland, and its dependencies.
-  hyprland-packages = final: prev: {
-    hyprland = final.callPackage ./default.nix {
-      version =
-        props.version
-        + "+date="
-        + (mkDate (self.lastModifiedDate or "19700101"))
-        + "_"
-        + (self.shortRev or "dirty");
-      wlroots = final.wlroots-hyprland;
-      commit = self.rev or "";
-      inherit (final) udis86 hyprland-protocols;
-    };
+  # Contains what a user is most likely to care about:
+  # Hyprland itself, XDPH, the Share Picker, and patched Waybar.
+  default = mkJoinedOverlays (with self.overlays; [
+    hyprland-packages
+    hyprland-extras
+  ]);
 
-    hyprland-unwrapped = final.hyprland.override {wrapRuntimeDeps = false;};
-    hyprland-debug = final.hyprland.override {debug = true;};
-    hyprland-hidpi = final.hyprland.override {hidpiXWayland = true;};
-    hyprland-nvidia = final.hyprland.override {nvidiaPatches = true;};
-    hyprland-no-hidpi =
-      builtins.trace
-      "hyprland-no-hidpi was removed. Please use the default package."
-      final.hyprland;
+  # Packages for variations of Hyprland, dependencies included.
+  hyprland-packages = mkJoinedOverlays [
+    # Dependencies
+    inputs.hyprland-protocols.overlays.default
+    self.overlays.wlroots-hyprland
+    self.overlays.udis86
+    # Hyprland packages themselves
+    (final: prev: {
+      hyprland = final.callPackage ./default.nix {
+        version = "${props.version}+date=${mkDate (self.lastModifiedDate or "19700101")}_${self.shortRev or "dirty"}";
+        wlroots = final.wlroots-hyprland;
+        commit = self.rev or "";
+        inherit (final) udis86 hyprland-protocols;
+      };
 
-    udis86 = final.callPackage ./udis86.nix {};
-  };
+      hyprland-unwrapped = final.hyprland.override {wrapRuntimeDeps = false;};
+      hyprland-debug = final.hyprland.override {debug = true;};
+      hyprland-hidpi = final.hyprland.override {hidpiXWayland = true;};
+      hyprland-nvidia = final.hyprland.override {nvidiaPatches = true;};
+      hyprland-no-hidpi =
+        builtins.trace
+        "hyprland-no-hidpi was removed. Please use the default package."
+        final.hyprland;
+    })
+  ];
 
   # Packages for extra software recommended for usage with Hyprland,
   # including forked or patched packages for compatibility.
-  hyprland-extras = lib.mkJoinedOverlays [
-    # Include any inputs' specific overlays whose attributes should
-    # be re-exported by the Hyprland flake.
-    #
-    inputs.xdph.overlays.default
-    # Provides:
-    # - xdg-desktop-portal-hyprland
-    # - hyprland-share-picker
-    #
-    # Attributes for `hyprland-extras` defined by this flake can
-    # go in the oberlay below.
-    (final: prev: {
-      waybar-hyprland = prev.waybar.overrideAttrs (old: {
-        postPatch = ''
-          # use hyprctl to switch workspaces
-          sed -i 's/zext_workspace_handle_v1_activate(workspace_handle_);/const std::string command = "hyprctl dispatch workspace " + name_;\n\tsystem(command.c_str());/g' src/modules/wlr/workspace_manager.cpp
-        '';
-        postFixup = ''
-          wrapProgram $out/bin/waybar \
-            --suffix PATH : ${lib.makeBinPath [final.hyprland]}
-        '';
-        mesonFlags = old.mesonFlags ++ ["-Dexperimental=true"];
-      });
-    })
+  hyprland-extras = mkJoinedOverlays [
+    inputs.xdph.overlays.xdg-desktop-portal-hyprland
+    inputs.xdph.overlays.hyprland-share-picker
+    self.overlays.waybar-hyprland
   ];
 
+  waybar-hyprland = final: prev: {
+    waybar-hyprland = prev.waybar.overrideAttrs (old: {
+      postPatch = ''
+        # use hyprctl to switch workspaces
+        sed -i 's/zext_workspace_handle_v1_activate(workspace_handle_);/const std::string command = "hyprctl dispatch workspace " + name_;\n\tsystem(command.c_str());/g' src/modules/wlr/workspace_manager.cpp
+      '';
+      postFixup = ''
+        wrapProgram $out/bin/waybar \
+          --suffix PATH : ${lib.makeBinPath [final.hyprland]}
+      '';
+      mesonFlags = old.mesonFlags ++ ["-Dexperimental=true"];
+    });
+  };
+
+  udis86 = final: prev: {
+    udis86 = final.callPackage ./udis86.nix {};
+  };
+
   # Patched version of wlroots for Hyprland.
   # It is under a new package name so as to not conflict with
   # the standard version in nixpkgs.
   wlroots-hyprland = final: prev: {
     wlroots-hyprland = final.callPackage ./wlroots.nix {
-      version =
-        mkDate (inputs.wlroots.lastModifiedDate or "19700101")
-        + "_"
-        + (inputs.wlroots.shortRev or "dirty");
+      version = "${mkDate (inputs.wlroots.lastModifiedDate or "19700101")}_${inputs.wlroots.shortRev or "dirty"}";
       src = inputs.wlroots;
       libdisplay-info = prev.libdisplay-info.overrideAttrs (old: {
         version = "0.1.1+date=2023-03-02";