Wrapping waybar to use specific icons

I have been trying to wrap waybar to use specific icons for the system tray (nm-applet, blueman-applet and etc…).

No matter what I do waybar will not use them, here is the current approach:

    packages.waybar = lib.makeOverridable ({
      font ? {
        package = pkgs.nerd-fonts._0xproto;
        family = "0xProto Nerd Font";
      },
      icons ? {
        package = pkgs.papirus-icon-theme;
        theme = "Papirus";
      },
      features ? [
        "audio"
        "backlight"
        "system-info"
        "battery"
        "tray"
      ],
      leftModules ? ["niri/workspaces"],
    }: let
      config = (pkgs.formats.json {}).generate "config.json" (self.settings.waybar {
        inherit lib;
        inherit pkgs;
        inherit leftModules;
        inherit features;
      });

      style = pkgs.writeText "style.css" (self.css.waybar {
        theme = inputs.colors.lib.withHash self.globals.theme.palette;
        fontFamily = font.family;
        fontWeight = "normal";
      });

      fontConfig = self.lib.mkFontsConf pkgs font.package;

      gtk3 = pkgs.writeTextDir "gtk-3.0/settings.ini" ''
        [Settings]
        gtk-application-prefer-dark-theme=true
        gtk-icon-theme-name=${icons.theme}
        gtk-interface-color-scheme=dark
      '';
    in (pkgs.symlinkJoin {
      name = "waybar";
      paths = [pkgs.waybar];
      buildInputs = [pkgs.makeWrapper];
      postBuild = let
        flags = [
          "--config ${config}"
          "--style ${style}"
        ];
      in
        lib.strings.concatStringsSep " " (
          [
            "wrapProgram $out/bin/waybar"
            "--add-flags \"${lib.strings.concatStringsSep " " flags}\""
          ]
          ++ (
            lib.optional (font != null)
            "--set FONTCONFIG_FILE ${fontConfig}"
          )
          ++ lib.optionals (icons != null) [
            "--prefix XDG_DATA_DIRS : ${icons.package}/share"
            "--prefix XDG_CONFIG_DIRS : ${gtk3}"
          ]
        );

      meta.mainProgram = "waybar";
    })) {};

It’s working in-progress so it’s a little messy, we just add the gtk-3.0 settings and the icon package to XDG_DATA_DIRS and XDG_CONFIG_DIR. I’m testing this in a VM and I checked that both are added to the waybar’s process environment variables with:

cat /proc/$(pgrep .waybar)/environ | tr \\0 \\n | grep etc...

What is overriding this? The nixos config doesn’t set gtk-3.0 anywhere.

Waybar launches as a service:

    homeModules.waybarService = {
      lib,
      pkgs,
      ...
    }: {
      systemd.user.services.waybar = {
        Unit = {
          Description = "Waybar service";
          PartOf = "graphical-session.target";
          After = "graphical-session.target";
        };

        Service = {
          ExecStart = "${lib.getExe self.packages.${pkgs.stdenv.hostPlatform.system}.waybar}";
          Restart = "on-failure";
          RestartSec = "1s";
          KillMode = "process";
        };

        Install = {
          WantedBy = ["graphical-session.target"];
        };
      };
    };

Thanks!

Okay, I finally found a working solution. I don’t know if something can overrule this so I won’t mark it as solved; I have to do some more research about this, not really familiar with this stuff.

Why the above didn’t work I believe is that gsettings was setting the icon theme to some default icon theme that overruled the one set in gtk-3.0 or maybe the config was faulty, but I don’t see how.

Anyway writing our own gsettings thingy works:

      gsettings = let
        schemas = pkgs.gsettings-desktop-schemas;
        deps = [
          "org.gnome.desktop.enums.xml"
          "org.gnome.desktop.interface.gschema.xml"
        ];
      in
        pkgs.runCommand "waybar-gsettings"
        {
          gschema = ''
            [org.gnome.desktop.interface]
            icon-theme='${icons.theme}'
          '';

          passAsFile = ["gschema"];
          nativeBuildInputs = [
            pkgs.glib
          ];
        }
        ''
          mkdir -p $out/
          cp ${schemas}/share/gsettings-schemas/${schemas.name}/glib-2.0/schemas/{${lib.strings.concatStringsSep "," deps}} $out/
          cp $gschemaPath $out/waybar.gschema.override
          glib-compile-schemas --strict $out
        '';

And in our wrapper we set:

"--prefix XDG_DATA_DIRS : ${icons.package}/share"
"--set GSETTINGS_SCHEMA_DIR ${gsettings}"

EDIT:

As is turns out setting values in dconf will overrule this setting, but we can simply get around this by adding:

"--set GSETTINGS_BACKEND memory"