From 834de347f177f881c9cccdec8305152a0036733f Mon Sep 17 00:00:00 2001
From: Thiago Kenji Okada <thiagokokada@gmail.com>
Date: Wed, 11 Dec 2024 14:00:13 +0000
Subject: [PATCH] patches: init

---
 lib/flake-helpers.nix | 42 ++++--------------------------------------
 patches/default.nix   | 20 ++++++++++++++++++++
 patches/patches.nix   | 24 ++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 38 deletions(-)
 create mode 100644 patches/default.nix
 create mode 100644 patches/patches.nix

diff --git a/lib/flake-helpers.nix b/lib/flake-helpers.nix
index 48537bb0..98515519 100644
--- a/lib/flake-helpers.nix
+++ b/lib/flake-helpers.nix
@@ -69,46 +69,12 @@ in
       extraModules ? [ ],
     }:
     let
-      inherit (self.outputs.nixosConfigurations.${hostname}) config pkgs;
-      inherit (nixpkgs.legacyPackages.${system}) applyPatches fetchpatch;
-
-      patches = [
-        # https://github.com/NixOS/nixpkgs/pull/363922
-        (fetchpatch {
-          url = "https://github.com/NixOS/nixpkgs/commit/ae6664cc5110afc4aa214c1bd9013899f6324a07.patch";
-          hash = "sha256-H7RXLGDwtWtLeXfrMVSd0Mab89JNoqhumKu/dE3lqVc=";
-        })
-        (fetchpatch {
-          url = "https://github.com/NixOS/nixpkgs/commit/43e6aa5bd29e0a330462127b96c694392291bd49.patch";
-          hash = "sha256-GirvMkd5T1dKDxaQrWWAOqTtSLvHzd7+GhjzYvedI5c=";
-        })
-        (fetchpatch {
-          url = "https://github.com/NixOS/nixpkgs/commit/d4c1d6c482f920bbd070304ed573631339258f37.patch";
-          hash = "sha256-iimCxs7LvJWrFTGWTeFvMzguf0s3ZKRfJfB2hBn0qFQ=";
-        })
-        (fetchpatch {
-          url = "https://github.com/NixOS/nixpkgs/commit/14ab7a484d2c044d344aa2a41072e34f531515e8.patch";
-          hash = "sha256-8SQUWkhBkdq7IA7Ln6Z9YMu0LqkiZy9PGHcppEYHwjM=";
-        })
-        (fetchpatch {
-          url = "https://github.com/NixOS/nixpkgs/commit/c27b1c401ae659d20e4ed5bd6a50d31f7872fdce.patch";
-          hash = "sha256-eYhDc4LbpGxk7nvpghivfYYXuZ9Goq9Sf63hy75HItE=";
-        })
-      ];
+      inherit (self.outputs.nixosConfigurations.${hostname}) config;
+      n = import ../patches { inherit nixpkgs system; };
 
       nixosSystem =
         args:
-        if patches != [ ] then
-          let
-            nixpkgs' = applyPatches {
-              inherit patches;
-              name = "nixpkgs-patched";
-              src = nixpkgs;
-            };
-          in
-          import (nixpkgs' + "/nixos/lib/eval-config.nix") args
-        else
-          nixpkgs.lib.nixosSystem args;
+        if n.patched then import (n.nixpkgs + "/nixos/lib/eval-config.nix") else n.nixpkgs.lib.nixosSystem;
     in
     {
       nixosConfigurations.${hostname} = nixosSystem {
@@ -128,7 +94,7 @@ in
         };
       };
 
-      apps.${pkgs.system} = {
+      apps.${system} = {
         "nixosActivations/${hostname}" = mkApp {
           drv = config.system.build.toplevel;
           exePath = "/activate";
diff --git a/patches/default.nix b/patches/default.nix
new file mode 100644
index 00000000..a154e48e
--- /dev/null
+++ b/patches/default.nix
@@ -0,0 +1,20 @@
+{ nixpkgs, system, ... }:
+let
+  inherit (nixpkgs.legacyPackages.${system}) applyPatches callPackage;
+  patches = callPackage ./patches.nix { };
+  nixpkgs' = applyPatches {
+    inherit patches;
+    name = "nixpkgs-patched";
+    src = nixpkgs;
+  };
+in
+if patches != [ ] then
+  {
+    patched = true;
+    nixpkgs = nixpkgs';
+  }
+else
+  {
+    patched = false;
+    inherit nixpkgs;
+  }
diff --git a/patches/patches.nix b/patches/patches.nix
new file mode 100644
index 00000000..d7379482
--- /dev/null
+++ b/patches/patches.nix
@@ -0,0 +1,24 @@
+{ fetchpatch, ... }:
+[
+  # https://github.com/NixOS/nixpkgs/pull/363922
+  (fetchpatch {
+    url = "https://github.com/NixOS/nixpkgs/commit/ae6664cc5110afc4aa214c1bd9013899f6324a07.patch";
+    hash = "sha256-H7RXLGDwtWtLeXfrMVSd0Mab89JNoqhumKu/dE3lqVc=";
+  })
+  (fetchpatch {
+    url = "https://github.com/NixOS/nixpkgs/commit/43e6aa5bd29e0a330462127b96c694392291bd49.patch";
+    hash = "sha256-GirvMkd5T1dKDxaQrWWAOqTtSLvHzd7+GhjzYvedI5c=";
+  })
+  (fetchpatch {
+    url = "https://github.com/NixOS/nixpkgs/commit/d4c1d6c482f920bbd070304ed573631339258f37.patch";
+    hash = "sha256-iimCxs7LvJWrFTGWTeFvMzguf0s3ZKRfJfB2hBn0qFQ=";
+  })
+  (fetchpatch {
+    url = "https://github.com/NixOS/nixpkgs/commit/14ab7a484d2c044d344aa2a41072e34f531515e8.patch";
+    hash = "sha256-8SQUWkhBkdq7IA7Ln6Z9YMu0LqkiZy9PGHcppEYHwjM=";
+  })
+  (fetchpatch {
+    url = "https://github.com/NixOS/nixpkgs/commit/c27b1c401ae659d20e4ed5bd6a50d31f7872fdce.patch";
+    hash = "sha256-eYhDc4LbpGxk7nvpghivfYYXuZ9Goq9Sf63hy75HItE=";
+  })
+]