From f61917cbaa6dba317e757aefd0bbb56403aff2f8 Mon Sep 17 00:00:00 2001
From: afresquet <29437693+afresquet@users.noreply.github.com>
Date: Thu, 2 May 2024 02:23:11 +0200
Subject: [PATCH] fastfetch: add module

PR #5363
---
 modules/lib/maintainers.nix                   |  6 ++
 modules/misc/news.nix                         | 11 ++++
 modules/modules.nix                           |  1 +
 modules/programs/fastfetch.nix                | 65 +++++++++++++++++++
 tests/default.nix                             |  1 +
 .../fastfetch/basic-configuration.jsonc       | 28 ++++++++
 .../fastfetch/basic-configuration.nix         | 39 +++++++++++
 .../fastfetch/default-configuration.nix       |  9 +++
 tests/modules/programs/fastfetch/default.nix  |  4 ++
 9 files changed, 164 insertions(+)
 create mode 100644 modules/programs/fastfetch.nix
 create mode 100755 tests/modules/programs/fastfetch/basic-configuration.jsonc
 create mode 100644 tests/modules/programs/fastfetch/basic-configuration.nix
 create mode 100644 tests/modules/programs/fastfetch/default-configuration.nix
 create mode 100644 tests/modules/programs/fastfetch/default.nix

diff --git a/modules/lib/maintainers.nix b/modules/lib/maintainers.nix
index 1e988cc2e..b5175a875 100644
--- a/modules/lib/maintainers.nix
+++ b/modules/lib/maintainers.nix
@@ -13,6 +13,12 @@
     github = "abayomi185";
     githubId = 21103047;
   };
+  afresquet = {
+    name = "Alvaro Fresquet";
+    email = "alvarofresquet@gmail.com";
+    github = "afresquet";
+    githubId = 29437693;
+  };
   amesgen = {
     name = "amesgen";
     email = "amesgen@amesgen.de";
diff --git a/modules/misc/news.nix b/modules/misc/news.nix
index 16f372c82..6319d6353 100644
--- a/modules/misc/news.nix
+++ b/modules/misc/news.nix
@@ -1607,6 +1607,17 @@ in {
           between windows, and is also a widget engine.
         '';
       }
+
+      {
+        time = "2024-05-10T10:30:58+00:00";
+        message = ''
+          A new module is available: 'programs.fastfetch'.
+
+          Fastfetch is a Neofetch-like tool for fetching system information and
+          displaying them in a pretty way. See
+          https://github.com/fastfetch-cli/fastfetch for more.
+        '';
+      }
     ];
   };
 }
diff --git a/modules/modules.nix b/modules/modules.nix
index 98f7de807..22664091c 100644
--- a/modules/modules.nix
+++ b/modules/modules.nix
@@ -87,6 +87,7 @@ let
     ./programs/emacs.nix
     ./programs/eww.nix
     ./programs/eza.nix
+    ./programs/fastfetch.nix
     ./programs/fd.nix
     ./programs/feh.nix
     ./programs/firefox.nix
diff --git a/modules/programs/fastfetch.nix b/modules/programs/fastfetch.nix
new file mode 100644
index 000000000..32021611d
--- /dev/null
+++ b/modules/programs/fastfetch.nix
@@ -0,0 +1,65 @@
+{ pkgs, lib, config, ... }:
+
+let
+  inherit (lib) mkEnableOption mkPackageOption mkOption mkIf literalExpression;
+
+  cfg = config.programs.fastfetch;
+
+  jsonFormat = pkgs.formats.json { };
+in {
+  meta.maintainers = with lib.hm.maintainers; [ afresquet ];
+
+  options.programs.fastfetch = {
+    enable = mkEnableOption "Fastfetch";
+
+    package = mkPackageOption pkgs "fastfetch" { };
+
+    settings = mkOption {
+      type = jsonFormat.type;
+      default = { };
+      example = literalExpression ''
+        {
+          logo = {
+            source = "nixos_small";
+            padding = {
+              right = 1;
+            };
+          };
+          display = {
+            binaryPrefix = "si";
+            color = "blue";
+            separator = "  ";
+          };
+          modules = [
+            {
+              type = "datetime";
+              key = "Date";
+              format = "{1}-{3}-{11}";
+            }
+            {
+              type = "datetime";
+              key = "Time";
+              format = "{14}:{17}:{20}";
+            }
+            "break"
+            "player"
+            "media"
+          ];
+        };
+      '';
+      description = ''
+        Configuration written to {file}`$XDG_CONFIG_HOME/fastfetch/config.jsonc`.
+        See <https://github.com/fastfetch-cli/fastfetch/wiki/Json-Schema>
+        for the documentation.
+      '';
+    };
+  };
+
+  config = mkIf cfg.enable {
+    home.packages = [ cfg.package ];
+
+    xdg.configFile."fastfetch/config.jsonc" = mkIf (cfg.settings != { }) {
+      source = jsonFormat.generate "config.jsonc" cfg.settings;
+    };
+  };
+}
diff --git a/tests/default.nix b/tests/default.nix
index 2ecffad88..0ebf47151 100644
--- a/tests/default.nix
+++ b/tests/default.nix
@@ -71,6 +71,7 @@ in import nmtSrc {
     ./modules/programs/dircolors
     ./modules/programs/direnv
     ./modules/programs/emacs
+    ./modules/programs/fastfetch
     ./modules/programs/feh
     ./modules/programs/fish
     ./modules/programs/gallery-dl
diff --git a/tests/modules/programs/fastfetch/basic-configuration.jsonc b/tests/modules/programs/fastfetch/basic-configuration.jsonc
new file mode 100755
index 000000000..70eabbd65
--- /dev/null
+++ b/tests/modules/programs/fastfetch/basic-configuration.jsonc
@@ -0,0 +1,28 @@
+{
+  "display": {
+    "binaryPrefix": "si",
+    "color": "blue",
+    "separator": "  "
+  },
+  "logo": {
+    "padding": {
+      "right": 1
+    },
+    "source": "nixos_small"
+  },
+  "modules": [
+    {
+      "format": "{1}-{3}-{11}",
+      "key": "Date",
+      "type": "datetime"
+    },
+    {
+      "format": "{14}:{17}:{20}",
+      "key": "Time",
+      "type": "datetime"
+    },
+    "break",
+    "player",
+    "media"
+  ]
+}
diff --git a/tests/modules/programs/fastfetch/basic-configuration.nix b/tests/modules/programs/fastfetch/basic-configuration.nix
new file mode 100644
index 000000000..a0b6783a8
--- /dev/null
+++ b/tests/modules/programs/fastfetch/basic-configuration.nix
@@ -0,0 +1,39 @@
+{
+  programs.fastfetch = {
+    enable = true;
+    settings = {
+      logo = {
+        source = "nixos_small";
+        padding = { right = 1; };
+      };
+      display = {
+        binaryPrefix = "si";
+        color = "blue";
+        separator = "  ";
+      };
+      modules = [
+        {
+          type = "datetime";
+          key = "Date";
+          format = "{1}-{3}-{11}";
+        }
+        {
+          type = "datetime";
+          key = "Time";
+          format = "{14}:{17}:{20}";
+        }
+        "break"
+        "player"
+        "media"
+      ];
+    };
+  };
+
+  test.stubs.fastfetch = { };
+
+  nmt.script = let configFile = "home-files/.config/fastfetch/config.jsonc";
+  in ''
+    assertFileExists "${configFile}"
+    assertFileContent "${configFile}" ${./basic-configuration.jsonc}
+  '';
+}
diff --git a/tests/modules/programs/fastfetch/default-configuration.nix b/tests/modules/programs/fastfetch/default-configuration.nix
new file mode 100644
index 000000000..428081d4d
--- /dev/null
+++ b/tests/modules/programs/fastfetch/default-configuration.nix
@@ -0,0 +1,9 @@
+{
+  programs.fastfetch.enable = true;
+
+  test.stubs.fastfetch = { };
+
+  nmt.script = ''
+    assertPathNotExists "home-files/.config/fastfetch/config.jsonc"
+  '';
+}
diff --git a/tests/modules/programs/fastfetch/default.nix b/tests/modules/programs/fastfetch/default.nix
new file mode 100644
index 000000000..d347e562e
--- /dev/null
+++ b/tests/modules/programs/fastfetch/default.nix
@@ -0,0 +1,4 @@
+{
+  fastfetch-default-configuration = ./default-configuration.nix;
+  fastfetch-basic-configuration = ./basic-configuration.nix;
+}