How to enable global dark mode using Home manager in NixOS

I am using nixos and home manager, and I am trying to enable dark mode, but it does not seem to have been activated. My chromium is in light mode

When I run this in the chromium console:

window.matchMedia('(prefers-color-scheme: dark)') -> MediaQueryList {media: '(prefers-color-scheme: dark)', matches: false, onchange: null}

This is my file gtk.nix:

{ pkgs, ... }:
{
  home.packages = with pkgs; [
    dconf
  ];

  dconf = {
    enable = true;
    settings = {
      "org/gnome/desktop/interface" = {
        color-scheme = "prefer-dark";
      };
    };
  };

  gtk = {
    enable = true;
    theme = {
      name = "orchis-theme";
      package = pkgs.orchis-theme;
    };
    iconTheme = {
      name = "Adwaita";
      package = pkgs.gnome.adwaita-icon-theme;
    };
    cursorTheme = {
      name = "Adwaita";
      package = pkgs.gnome.adwaita-icon-theme;
    };
  };
}

And chromium.nix:

{ ... }:
{
 programs.chromium.enable = true;
}

When I try checking if dark mode is enabled, I get the following:

> gsettings get org.gnome.desktop.interface gtk-theme
No schemas installed

You’ll probably also want to set gtk-application-prefer-dark-theme=1 for each GTK version, that supports it.

gtk settings can be set like:

environment.etc = {
  "xdg/gtk-2.0/gtkrc".text = "gtk-error-bell=0";
  "xdg/gtk-3.0/settings.ini".text = ''
    [Settings]
    gtk-error-bell=false
  '';
  "xdg/gtk-4.0/settings.ini".text = ''
    [Settings]
    gtk-error-bell=false
  '';
};

You are mixing multiple things:

  • The color-scheme is a key in XDG Settings portal.
    • When using xdg-desktop-portal-gtk or xdg-desktop-portal-gnome , this is stored under color-scheme key from org.gnome.desktop.interface GSettings schema, which you are setting in the dconf database.
    • This is indeed the correct way to enable dark mode but as it is a rather new standard, it is not yet supported by all apps.
  • prefers-color-scheme is a CSS media query used by web pages.
    • This is not necessarily connected to the above setting – Firefox respects it out of the box but Chromium currently does not (might be fixed in 114).
  • GTK themes predate the color-scheme setting and are orthogonal to it.
    • Even if the GTK theme looks dark, the app has no way to determine whether it is dark other than by matching on its name (or maybe analysing the colours and guessing).
    • GTK themes are not really supported by GTK and can result in unreadable content in apps so use them on your own risk.
    • When using a dark GTK theme in Chromium, its chrome will be dark but it will not affect media queries.
  • Some GTK themes (including GNOME’s default Adwaita) have a dark stylesheet which can be enabled with gtk-application-prefer-dark-theme but again that has no connection to the color-scheme.
    • It can also affect Chromium’s chrome but not media queries.

As mentioned, this has nothing to do with dark mode. And you are getting the error because we intentionally try to not expose GSettings schemas into the environment, see GSettings schema of installed software not available to gsettings program or dconf-editor · Issue #33277 · NixOS/nixpkgs · GitHub

1 Like

Thank you for the insightful answer! Is there any way to launch chromium in such a way that it uses dark mode? I am able to get dark mode when running chromium --force-dark-mode, but that does not feel very elegant.