From b6fa1ee2217b596e69bc38283e68dfc73e18c598 Mon Sep 17 00:00:00 2001
From: Myned <dev@bjork.tech>
Date: Tue, 11 Feb 2025 18:39:03 -0600
Subject: [PATCH] programs: add nixgl

Signed-off-by: Myned <dev@bjork.tech>
---
 flake.in.nix                        |  7 +++++
 flake.nix                           |  8 ++++++
 options/custom/programs/default.nix |  1 +
 options/custom/programs/nixgl.nix   | 41 +++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+)
 create mode 100644 options/custom/programs/nixgl.nix

diff --git a/flake.in.nix b/flake.in.nix
index 8c6486a..c2b2269 100644
--- a/flake.in.nix
+++ b/flake.in.nix
@@ -33,6 +33,7 @@
     aagl-gtk-on-nix-stable = flake "github:ezKEa/aagl-gtk-on-nix/release-24.11" // stable "nixpkgs";
     home-manager-stable = flake "github:nix-community/home-manager/release-24.11" // stable "nixpkgs";
     nix-index-database-stable = flake "github:nix-community/nix-index-database" // stable "nixpkgs";
+    nixgl-stable = flake "github:nix-community/nixGL" // stable "nixpkgs";
     stylix-stable = flake "github:danth/stylix/release-24.11" // stable "nixpkgs";
 
     ### Unstable
@@ -64,6 +65,7 @@
     nix-index-database-unstable = flake "github:nix-community/nix-index-database" // unstable "nixpkgs";
     nix-vscode-extensions = flake "github:nix-community/nix-vscode-extensions" // unstable "nixpkgs";
     nixd = flake "github:nix-community/nixd" // unstable "nixpkgs";
+    nixgl-unstable = flake "github:nix-community/nixGL" // unstable "nixpkgs";
     stylix-unstable = flake "github:danth/stylix" // unstable "nixpkgs";
     walker = flake "github:abenz1267/walker?ref=v0.12.8" // unstable "nixpkgs";
 
@@ -139,6 +141,11 @@
                       inputs.nix-flatpak.homeManagerModules.nix-flatpak
                       inputs.walker.homeManagerModules.default
                     ];
+
+                    # Branch-specific overlays
+                    nixpkgs.overlays = [
+                      inputs."nixgl-${branch}".overlays.default
+                    ];
                   }
               )
             ];
diff --git a/flake.nix b/flake.nix
index d0ccb17..de99163 100644
--- a/flake.nix
+++ b/flake.nix
@@ -128,6 +128,14 @@
       inputs.nixpkgs.follows = "nixpkgs-unstable";
       url = "github:nix-community/nixd";
     };
+    nixgl-stable = {
+      inputs.nixpkgs.follows = "nixpkgs-stable";
+      url = "github:nix-community/nixGL";
+    };
+    nixgl-unstable = {
+      inputs.nixpkgs.follows = "nixpkgs-unstable";
+      url = "github:nix-community/nixGL";
+    };
     nixos-hardware.url = "github:NixOS/nixos-hardware";
     nixpkgs-master.url = "github:NixOS/nixpkgs/master";
     nixpkgs-stable.url = "github:NixOS/nixpkgs/nixos-24.11";
diff --git a/options/custom/programs/default.nix b/options/custom/programs/default.nix
index c5fb3d7..a8f43d5 100644
--- a/options/custom/programs/default.nix
+++ b/options/custom/programs/default.nix
@@ -18,6 +18,7 @@ with lib; {
       nano.enable = true;
       nh.enable = true;
       nix-index.enable = true;
+      nixgl.enable = true;
       nushell.enable = true;
       polkit.enable = true;
       ssh.enable = true;
diff --git a/options/custom/programs/nixgl.nix b/options/custom/programs/nixgl.nix
new file mode 100644
index 0000000..832ca1e
--- /dev/null
+++ b/options/custom/programs/nixgl.nix
@@ -0,0 +1,41 @@
+{
+  config,
+  lib,
+  pkgs,
+  ...
+}:
+with lib; let
+  cfg = config.custom.programs.nixgl;
+in {
+  options.custom.programs.nixgl = {
+    enable = mkOption {default = false;};
+    wrapper = mkOption {default = "mesa";};
+  };
+
+  config = mkIf cfg.enable {
+    # https://github.com/nix-community/nixGL
+    #?? nixGL PROGRAM
+    environment.systemPackages = with pkgs.nixgl;
+      optionals (cfg.wrapper == "auto") [
+        #!! Impure autodetection
+        auto.nixGLDefault
+      ]
+      ++ optionals (cfg.wrapper == "mesa") [
+        nixGLIntel
+        nixVulkanIntel
+
+        # Wrapper for the wrappers
+        #?? nixgl PROGRAM
+        (pkgs.writeShellApplication {
+          name = "nixgl";
+          runtimeInputs = [nixGLIntel nixVulkanIntel];
+          text = ''exec nixGLIntel nixVulkanIntel "$@"'';
+        })
+      ]
+      ++ optionals (cfg.wrapper == "nvidia") [
+        #!! Impure autodetection
+        auto.nixGLNvidia
+        auto.nixVulkanNvidia
+      ];
+  };
+}