Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

htop meters are in wrong order #2060

Closed
rvl opened this issue Jun 1, 2021 · 4 comments · Fixed by #2065
Closed

htop meters are in wrong order #2060

rvl opened this issue Jun 1, 2021 · 4 comments · Fixed by #2065
Labels

Comments

@rvl
Copy link
Contributor

rvl commented Jun 1, 2021

Issue description

I tried out programs.htop with the example configuration:

{ config, ... }: {
  programs.htop = {
    enable = true;
    settings = {
       color_scheme = 6;
       cpu_count_from_one = 0;
       delay = 15;
       fields = with config.lib.htop.fields; [
         PID
         USER
         PRIORITY
         NICE
         M_SIZE
         M_RESIDENT
         M_SHARE
         STATE
         PERCENT_CPU
         PERCENT_MEM
         TIME
         COMM
       ];
       highlight_base_name = 1;
       highlight_megabytes = 1;
       highlight_threads = 1;
     } // (with config.lib.htop; leftMeters {
       AllCPUs2 = modes.Bar;
       Memory = modes.Bar;
       Swap = modes.Bar;
       Zram = modes.Text;
     }) // (with config.lib.htop; rightMeters {
       Tasks = modes.Text;
       LoadAverage = modes.Text;
       Uptime = modes.Text;
       Systemd = modes.Text;
     });
  };
}

Expected Result

Meters in right-hand column are in order as defined in configuration: Tasks, LoadAverage, Uptime, Systemd.

Actual Result

Meters in right-hand column are in alphabetical order: LoadAverage, Systemd, Tasks, Uptime.

  0[|||                    6.5%]    4[||                     5.3%] Load average: 1.01 3.71 4.26
  1[|||||||||             27.7%]    5[||                     4.8%] Systemd: N/A (?/? failed) (?/? jobs)
  2[||||                   7.1%]    6[||                     6.0%] Tasks: 179, 1064 thr, 168 kthr; 1 running
  3[|||||                 13.6%]    7[|||                    6.5%] Uptime: 5 days, 21:45:29
Mem[||||||||||||||||||||||||||||||||||||||||||||||||||15.4G/31.1G]
Swp[||                                                 812M/32.0G]
zrm:0K used:0K uncompressed:0K

Info

It is very useful to be able to customize the order of meters.

This bug probably occurs because nix attrsets are unordered, and #1844 changes the meters lists to be attrsets.

Also

Also I don't know why the systemd meter doesn't work. Do I need to configure something else for that?

Maintainer CC

cc: @bjpbakker

Technical details

Using home-manager rev 9049302.

Thanks

Thank you for the programs.htop module - it's cool.

@berbiche
Copy link
Member

berbiche commented Jun 2, 2021

Yeah, this is an unfortunate restriction of the Nix language.

Attribute sets are stored in a lexicographical order and not insertion order so it's not possible to order keys.

I think the functions leftMeters and rightMeters should accept a list of attributes to maintain the order:

formatMeters = side: meters: {
"${side}_meters" = mapAttrsToList (x: _: x) meters;
"${side}_meter_modes" = mapAttrsToList (_: y: y) meters;
};
leftMeters = formatMeters "left";
rightMeters = formatMeters "right";

@berbiche berbiche added the bug label Jun 2, 2021
@rvl
Copy link
Contributor Author

rvl commented Jun 2, 2021

Something like this might work (and allow for backwards compatibility):

programs.htop.settings = otherStuff // (with config.lib.htop; metersSettings {
  left = [
    (bar "AllCPUs2")
    "Memory"
    (bar "Swap")
  ];[
  right = [
    (text "Tasks")
    (graph "LoadAverage")
    (led "Clock")
   ];
};

# implementation would have something like this
let
  text = meter: { kind = meter; mode = modes.Text };
  # etc.

  metersSettings = { left ? [], right ? [] }:
    formatMeterList "left" left //
    formatMeterList "right" right;

  # similar to formatMeters
  formatMeterList = side: meters: {
    "${side}_meters" = builtins.map
      (m: if builtins.isAttrs m then m.kind else m)
       modes;
    "${side}_meter_modes" = builtins.map
      (m: if (builtins.isAttrs m) then (if (m.mode or null) == null then meters.${m.kind} else m.mode) else meters.${m})
       modes;
  };
in
  ...

But actually, it may be better to just use nixos options for configuring this.

@bjpbakker
Copy link
Contributor

Hm yes, that's a good point. Thanks for bringing it up.

A single settings key still is preferable IMO. Splitting the settings in various nixos options (like before) is too limiting. But the input type should probably be a list of pairs rather than an attrset, like you suggest above.

I will provide a fix for this.

bjpbakker added a commit to bjpbakker/home-manager that referenced this issue Jun 2, 2021
Pass meters for formatting in a list of attrsets so that ordering can be
preserved. In addition provide some mode-specific functions to create these
attrsets, to make for a bit nicer config.

This fixes nix-community#2060.
@bjpbakker
Copy link
Contributor

bjpbakker commented Jun 2, 2021

@rvl I opened #2065 to address this. I'll backport to 21.05 when merged.

Also I like your suggestion of having 1 function to pass both the left and right meters lists to. I'll add that a bit later after I've done some cleanup of the deprecated things on master.

Thanks again for bringing this up :)

berbiche pushed a commit that referenced this issue Jun 2, 2021
Pass meters for formatting in a list of attrsets so that ordering can be
preserved. In addition provide some mode-specific functions to create these
attrsets, to make for a bit nicer config.

This fixes #2060.
bjpbakker added a commit to bjpbakker/home-manager that referenced this issue Jun 3, 2021
Pass meters for formatting in a list of attrsets so that ordering can be
preserved. In addition provide some mode-specific functions to create these
attrsets, to make for a bit nicer config.

This fixes nix-community#2060 on 21.05.
berbiche pushed a commit that referenced this issue Jun 4, 2021
Pass meters for formatting in a list of attrsets so that ordering can be
preserved. In addition provide some mode-specific functions to create these
attrsets, to make for a bit nicer config.

This fixes #2060 on 21.05.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants