From e6869735d25bb2d33a93ac8e5fcc9a02bbbb5351 Mon Sep 17 00:00:00 2001
From: Roman Timushev <rtimush@gmail.com>
Date: Fri, 13 May 2022 22:53:16 +0200
Subject: [PATCH] htop: fix darwin defaults

M_SHARE is not a valid column on Darwin. It seems that previously htop
ignored unknown columns, but the current version does not display all
subsequent columns.
---
 modules/programs/htop.nix                     | 34 +++++++++++--------
 tests/modules/programs/htop/header_layout.nix |  9 +++--
 .../programs/htop/settings-without-fields.nix |  9 +++--
 3 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/modules/programs/htop.nix b/modules/programs/htop.nix
index 0d3530264..7141ff34a 100644
--- a/modules/programs/htop.nix
+++ b/modules/programs/htop.nix
@@ -3,6 +3,7 @@
 with lib;
 
 let
+  inherit (pkgs.stdenv.hostPlatform) isDarwin;
 
   cfg = config.programs.htop;
 
@@ -73,6 +74,21 @@ let
     M_PSSWP = 120;
   };
 
+  defaultFields = with fields; [
+    PID
+    USER
+    PRIORITY
+    NICE
+    M_SIZE
+    M_RESIDENT
+    M_SHARE
+    STATE
+    PERCENT_CPU
+    PERCENT_MEM
+    TIME
+    COMM
+  ];
+
   modes = {
     Bar = 1;
     Text = 2;
@@ -154,20 +170,10 @@ in {
 
     xdg.configFile."htop/htoprc" = let
       defaults = {
-        fields = with fields; [
-          PID
-          USER
-          PRIORITY
-          NICE
-          M_SIZE
-          M_RESIDENT
-          M_SHARE
-          STATE
-          PERCENT_CPU
-          PERCENT_MEM
-          TIME
-          COMM
-        ];
+        fields = if isDarwin then
+          remove fields.M_SHARE defaultFields
+        else
+          defaultFields;
       };
 
       before = optionalAttrs (cfg.settings ? header_layout) {
diff --git a/tests/modules/programs/htop/header_layout.nix b/tests/modules/programs/htop/header_layout.nix
index 2cf34998d..aaf628522 100644
--- a/tests/modules/programs/htop/header_layout.nix
+++ b/tests/modules/programs/htop/header_layout.nix
@@ -17,7 +17,12 @@ with lib;
 
     # Test that the 'fields' key is written in addition to the customized
     # settings or htop won't read the options.
-    nmt.script = ''
+    nmt.script = let
+      fields = if pkgs.stdenv.hostPlatform.isDarwin then
+        "0 48 17 18 38 39 2 46 47 49 1"
+      else
+        "0 48 17 18 38 39 40 2 46 47 49 1";
+    in ''
       htoprc=home-files/.config/htop/htoprc
       assertFileExists $htoprc
       assertFileContent $htoprc \
@@ -28,7 +33,7 @@ with lib;
             column_meters_1=Tasks LoadAverage Uptime Systemd
             column_meters_modes_0=1 1 1 2
             column_meters_modes_1=2 2 2 2
-            fields=0 48 17 18 38 39 40 2 46 47 49 1
+            fields=${fields}
           ''
         }
     '';
diff --git a/tests/modules/programs/htop/settings-without-fields.nix b/tests/modules/programs/htop/settings-without-fields.nix
index 2f4b3e864..f1efac2d4 100644
--- a/tests/modules/programs/htop/settings-without-fields.nix
+++ b/tests/modules/programs/htop/settings-without-fields.nix
@@ -11,14 +11,19 @@ with lib;
 
     # Test that the 'fields' key is written in addition to the customized
     # settings or htop won't read the options.
-    nmt.script = ''
+    nmt.script = let
+      fields = if pkgs.stdenv.hostPlatform.isDarwin then
+        "0 48 17 18 38 39 2 46 47 49 1"
+      else
+        "0 48 17 18 38 39 40 2 46 47 49 1";
+    in ''
       htoprc=home-files/.config/htop/htoprc
       assertFileExists $htoprc
       assertFileContent $htoprc \
         ${
           builtins.toFile "htoprc-expected" ''
             color_scheme=6
-            fields=0 48 17 18 38 39 40 2 46 47 49 1
+            fields=${fields}
           ''
         }
     '';