Proper way to configure monitors

What is the proper way to configure your monitors in Nix/NixOS? I have two monitors connected to my main home machine, one Ultrawide 100Hz monitor and my TV. Usually, that TV is turned off. By default it seems Xorg selects my TV as my primary screen, and sets my Ultrawide to 60Hz.

I can, of course, use xrandr --output HDMI-1 --off --output DP-1 --primary --mode 3440x1440 --rate 100 to fix this, and that works just fine, but is there any proper way to configure this without having to do this on every login?

I’ve tried using services.xserver.displayManager.sessionCommands and services.xserver.displayManager.setupCommands in combination with the xrandr command above, but that didn’t actually do anything.

I’ve also tried using services.xserver.xrandrHeads. This one gets me a little further along, because it allows me to select my primary monitor through services.xserver.xrandrHeads.*.primary = true, but that still doesn’t allow me to disable the TV or set my Ultrawide’s refresh rate. The documentation says that services.xserver.xrandrHeads.*.monitorConfig takes any configuration listen in the MONITOR section of man 5 xorg.conf, but there are no relevant configuration options in that section (at least that I can find).

Ideally, I want to be able to:

  • Set my Ultrawide as my primary display
  • Set the refresh rate of the Ultrawide to 100Hz
  • Mirror my ultrawide on the TV (so that it displays my screen on the TV when turned on, but keeps everything locked to my ultrawide monitor, so that for example my mouse doesn’t continue to scroll past the screen boundaries)

I know that theoretically I can invoke xrandr in some startup script somewhere, but that does not seem to fit with the spirit of Nix and NixOS (that’s something I’d do if I were on Arch/Gentoo), so I’m looking for a proper solution here.

I previously just dealt with it by making changes in GNOMEs configuration settings, but it’s really annoying now that I’m trying out my new Xmonad setup.

Thanks!

1 Like

I’m doing it over home-manager’s autorandr module. It’s a bit sad that I even needed to supply "fingerprint"s of monitors in order to do it declarative, but it seems to work so far. Either way, check out autorandr.

1 Like

I wrote a tool for this recently: GitHub - theotherjimmy/autorandr-rs: like autorandr, but kdl and a daemon

I use it in my home-manager flake:

3 Likes

Holy pumpkins, that looks awesome. Will try :+1: Thanks!

1 Like

I just realized that there is no utility to read edids :sweat_smile: I’ll have to make that too.

I wrote it and updated things. The links should be the same, but the autorandr-rs package now includes a randr-edid binary that reads edids through randr, and dumps config toml that matches the attached monitors.

Let me know, either here or thorough issues on the repo if you run into any trouble.

Does it need to be in a separate toml file, or can it just be configured with normal Nix config?

You’re free to modify my home-grown module to your hearts content. autorandrd (the daemon part of autorandr-rs) accepts a toml config, so you’d have to use something to make the toml. I wrote the toml by hand when testing autorandrd, so I had no need to convert it.

@gurkan Thanks, totally missed the autorandr module!

@theotherjimmy That looks super useful. I was honestly thinking about writing something something similar in Rust as well, glad someone else did it first! ^^

Feel free to contribute or fork at your whim.

The way I have it working so far is with setupCommands in my configuration.nix

services.xserver.displayManager.setupCommands = ''
    LEFT='DVI-D-0'
    CENTER='DVI-I-1'
    RIGHT='HDMI-A-0'
    ${pkgs.xorg.xrandr}/bin/xrandr --output $CENTER --rotate left --output $LEFT --rotate left --left-of $CENTER --output $RIGHT --right-of $CENTER
'';

I don’t know if this is the proper way but it works.

5 Likes

@gurkan thank you for your answer, I’ve checked out home-manager ‘autorandr’ and succeeded in making somewhat pleasant configuration for my displays!

Shared config:
https://github.com/efim/dotfiles/blob/18c3dd12a596d5fd2a0ca3bc7b734fe9206637b0/nixpkgs/my-autorandr.nix

Declaration:
https://github.com/efim/dotfiles/blob/18c3dd12a596d5fd2a0ca3bc7b734fe9206637b0/nixpkgs/hosts/personal-laptop/home.nix

Now automatic configurations can be parametrized and shared between my different hosts, and default autorandr configurations used as very helpful xmonad hotkeys.

2 Likes