From 6acf70dfee749179d8767d3556e1ae8424568001 Mon Sep 17 00:00:00 2001
From: Fabian Haas <29468630+hfxbse@users.noreply.github.com>
Date: Thu, 23 Jan 2025 17:47:10 +0100
Subject: [PATCH] use Brother printer driver derivation flake

---
 flake.lock                             | 22 ++++++++
 flake.nix                              | 13 ++++-
 modules/desktop/desktop.nix            |  3 +
 modules/hardware/Brother/hl3172cdw.nix | 77 --------------------------
 modules/hardware/printers.nix          | 10 ----
 modules/printing.nix                   | 10 ++++
 6 files changed, 45 insertions(+), 90 deletions(-)
 delete mode 100644 modules/hardware/Brother/hl3172cdw.nix
 delete mode 100644 modules/hardware/printers.nix
 create mode 100644 modules/printing.nix

diff --git a/flake.lock b/flake.lock
index 01b8591..f7e0f85 100644
--- a/flake.lock
+++ b/flake.lock
@@ -1,5 +1,26 @@
 {
   "nodes": {
+    "cups-brother-hl3172cdw": {
+      "inputs": {
+        "nixpkgs": [
+          "nixpkgs"
+        ]
+      },
+      "locked": {
+        "lastModified": 1737643318,
+        "narHash": "sha256-0lUyboqnPCqSHeiLDeNSxpAgaNpuatshGZqJli4qyxE=",
+        "owner": "hfxbse",
+        "repo": "nixos-config",
+        "rev": "6d5abaced343b549f02b5fd24e98e970a59f0f7d",
+        "type": "github"
+      },
+      "original": {
+        "owner": "hfxbse",
+        "ref": "derivation/cups-brother-hl3172cdw",
+        "repo": "nixos-config",
+        "type": "github"
+      }
+    },
     "devshell": {
       "inputs": {
         "nixpkgs": [
@@ -285,6 +306,7 @@
     },
     "root": {
       "inputs": {
+        "cups-brother-hl3172cdw": "cups-brother-hl3172cdw",
         "disko": "disko",
         "nixpkgs": "nixpkgs",
         "nixvim": "nixvim"
diff --git a/flake.nix b/flake.nix
index b16274a..e951c1b 100644
--- a/flake.nix
+++ b/flake.nix
@@ -13,9 +13,14 @@
       url = "github:nix-community/disko";
       inputs.nixpkgs.follows = "nixpkgs";
     };
+
+    cups-brother-hl3172cdw = {
+      url = "github:hfxbse/nixos-config?ref=derivation/cups-brother-hl3172cdw";
+      inputs.nixpkgs.follows = "nixpkgs";
+    };
   };
 
-  outputs = { self, nixpkgs, nixvim, disko }@attrs:
+  outputs = { self, cups-brother-hl3172cdw, disko, nixpkgs, nixvim }@attrs:
   let
     defaultModules = [
       # Nixvim needs to be an top level import
@@ -26,7 +31,7 @@
       ./modules/localization.nix
       ./modules/text-processing.nix
       ./modules/workplace-compliance.nix
-      ./modules/hardware/printers.nix
+      ./modules/printing.nix
       ./modules/hardware/Wooting/wootility.nix
       ./modules/development.nix
     ];
@@ -34,6 +39,7 @@
   {
     nixosConfigurations.home-pc = nixpkgs.lib.nixosSystem {
       specialArgs = with attrs; {
+        cups-brother-hl3172cdw = cups-brother-hl3172cdw.packages.x86_64-linux.default;
         host = {
           user.name = "fxbse";
           user.description = "Fabian Haas";
@@ -46,7 +52,8 @@
     };
 
     nixosConfigurations.nt-laptop = nixpkgs.lib.nixosSystem {
-      specialArgs = with attrs; {
+      specialArgs = with attrs; rec {
+        cups-brother-hl3172cdw = cups-brother-hl3172cdw.packages.x86_64-linux.default;
         host = rec {
           user.name = "fhs";
           user.description = "Fabian Haas";
diff --git a/modules/desktop/desktop.nix b/modules/desktop/desktop.nix
index 3662058..1923e53 100644
--- a/modules/desktop/desktop.nix
+++ b/modules/desktop/desktop.nix
@@ -53,11 +53,14 @@ in
 
     services.libinput.enable = lib.mkDefault cfg.touchpad.enable;
 
+    services.printing.enable = true;
+
     # Enable automatic login for the user.
     services.displayManager.autoLogin.enable = cfg.login == "auto";
     services.displayManager.autoLogin.user = lib.mkDefault username;
 
     boot.supportedFilesystems = [ "ntfs"  ];
+    # Support mounting MTP devices
     services.gvfs.enable = lib.mkDefault true;
 
     nixpkgs.config.allowUnfree = true;
diff --git a/modules/hardware/Brother/hl3172cdw.nix b/modules/hardware/Brother/hl3172cdw.nix
deleted file mode 100644
index 9f36916..0000000
--- a/modules/hardware/Brother/hl3172cdw.nix
+++ /dev/null
@@ -1,77 +0,0 @@
-{ pkgsi686Linux, fetchurl, lib, dpkg, gnused, coreutils, gawk, ghostscript, a2ps, file, makeWrapper, cups, autoPatchelfHook, glibc, ... }:
-let
-  model = "hl3172cdw";
-  cupsVersion = "1.1.4-0";
-  lprVersion = "1.1.3-0";
-  version = "1.1.4-0";
-  cupsDeb = fetchurl {
-    url = "https://download.brother.com/welcome/dlf101637/${model}cupswrapper-${cupsVersion}.i386.deb";
-    sha256 = "39eed80ddb5c7432e0aceffcb3cb0378fc773c50f8bbe528db7d11bd3b9ceb10";
-  };
-  srcDir = "${model}_cupswrapper_GPL_source_${version}";
-  cupsSrc = fetchurl {
-    url = "https://download.brother.com/welcome/dlf101646/${srcDir}.tar.gz";
-    sha256 = "1844ede1865bbffbefb3b167672168cc50129e57ce02688a2a9d22af54d7c060";
-  };
-  lprDeb = fetchurl {
-    url = "https://download.brother.com/welcome/dlf101636/${model}lpr-1.1.3-0.i386.deb";
-    sha256 = "e651ec39297daf7e5d5e36537c836b9d1c52d36873f2c5a4c28b46226b3e0d9d";
-  };
-in
-pkgsi686Linux.stdenv.mkDerivation {
-  pname = "cups-brother-${model}";
-  version = cupsVersion;
-
-  nativeBuildInputs = [ makeWrapper dpkg autoPatchelfHook ];
-  buildInputs = [ cups ghostscript a2ps glibc ];
-
-  unpackPhase = ''
-    tar -xvf ${cupsSrc}
-  '';
-
-  buildPhase = ''
-    gcc -Wall ${srcDir}/brcupsconfig/brcupsconfig.c -o brcupsconfpt1
-  '';
-
-  installPhase = ''
-    mkdir -p $out/lib/cups/filter
-
-    # Install LPR
-    dpkg-deb -x ${lprDeb} $out
-    
-    substituteInPlace $out/opt/brother/Printers/${model}/lpd/filter${model}  --replace /opt "$out/opt"
-    substituteInPlace $out/opt/brother/Printers/${model}/inf/setupPrintcapij --replace /opt "$out/opt"
-    
-    sed -i '/GHOST_SCRIPT=/c\GHOST_SCRIPT=gs' $out/opt/brother/Printers/${model}/lpd/psconvertij2
-    
-    wrapProgram $out/opt/brother/Printers/${model}/lpd/psconvertij2 --prefix PATH ":" ${ lib.makeBinPath [ gnused coreutils gawk ] }
-    wrapProgram $out/opt/brother/Printers/${model}/lpd/filter${model} --prefix PATH ":" ${ lib.makeBinPath [ ghostscript a2ps file gnused coreutils ] }
-
-    ln -s $out/opt/brother/Printers/${model}/lpd/filter${model} $out/lib/cups/filter/brother_lpdwrapper_${model}
-
-    # Install CUPS
-    dpkg-deb -x ${cupsDeb} $out
-    substituteInPlace $out/opt/brother/Printers/${model}/cupswrapper/cupswrapper${model} --replace /opt "$out/opt"
-
-    ln -s $out/opt/brother/Printers/${model}/cupswrapper/cupswrapper${model} $out/lib/cups/filter/cupswrapper${model}
-
-    cp brcupsconfpt1 $out/opt/brother/Printers/${model}/cupswrapper/
-    ln -s $out/opt/brother/Printers/${model}/cupswrapper/brcupsconfpt1 $out/lib/cups/filter/brcupsconfpt1
-    
-    wrapProgram $out/opt/brother/Printers/${model}/cupswrapper/cupswrapper${model} --prefix PATH ":" ${ lib.makeBinPath [ gnused coreutils gawk ] }
-
-    # Install PPD file
-    mkdir -p $out/share/cups/model/
-    cp ${srcDir}/PPD/brother_${model}_printer_en.ppd $out/share/cups/model/
-  '';
-
-  meta = {
-    homepage = "http://www.brother.com/";
-    description = "Brother ${model} printer driver";
-    sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
-    license = lib.licenses.unfree;
-    platforms = lib.platforms.linux;
-    architectures = [ "x86" ];
-    downloadPage = "https://support.brother.com/g/b/downloadlist.aspx?c=eu_ot&lang=en&prod=${model}_eu&os=128";
-  };
-}
diff --git a/modules/hardware/printers.nix b/modules/hardware/printers.nix
deleted file mode 100644
index 748b98d..0000000
--- a/modules/hardware/printers.nix
+++ /dev/null
@@ -1,10 +0,0 @@
-{ pkgs, ... }:
-{
-    services.printing = {
-        enable = true;
-        logLevel = "debug";
-        drivers = with pkgs; [
-            (callPackage ./Brother/hl3172cdw.nix {})
-        ];
-    };
-}
diff --git a/modules/printing.nix b/modules/printing.nix
new file mode 100644
index 0000000..79f9a79
--- /dev/null
+++ b/modules/printing.nix
@@ -0,0 +1,10 @@
+{ config, cups-brother-hl3172cdw, lib, pkgs, ... }:
+let
+  allowUnfree = config.nixpkgs.config.allowUnfree;
+in
+{
+    services.printing = {
+        logLevel = "debug";
+        drivers = lib.optional allowUnfree cups-brother-hl3172cdw;
+    };
+}