Setting an environment variable for a single package?

I’m trying to use OBS Studio (OBS) on wayland, but for it to properly work I need to set the environment variable QT_QPA_PLATFORM = "wayland", so I did this in my configuration.nix as follows:

environment.variables = {
  QT_QPA_PLATFORM = "wayland";
};

However, this seems to clash with my use of corectrl. So I was wondering, is there a way I can set this variable only for OBS?

OBS is installed as a systemPackage:

let
  unstable = import <nixos-unstable> { config = { allowUnfree = true; }; };
in {
  ...
  environment.systemPackages = with pkgs; [
    ...
    unstable.obs-studio
...

I was thinking I could use either some override, or perhaps an overlay? Though not sure how either works yet.

Anybody here knows how to set an environment variable for a single package?

1 Like

You could do this:

let
  unstable = import <nixos-unstable> { config = { allowUnfree = true; }; };

  obs-wayland = pkgs.runCommand "obs-wayland" { buildInputs = [ pkgs.makeWrapper ]; } ''
      makeWrapper ${pkgs.obs-studio}/bin/obs $out/bin/obs-wayland --set QT_QPA_PLATFORM wayland
  '';
in {
  ...
  environment.systemPackages = with pkgs; [
    ...
    obs-wayland
    ...

This will create the <system packages>/bin/obs-wayland wrapper. So you can run obs-wayland.

2 Likes

By doing that, I lost my gnome shortcut. Do you know a workaround for this?

See this? Nix Cookbook - NixOS Wiki

1 Like

Yeah, the suggested approach does not maintain things like desktop entries and such. I would try something like below.

I didn’t test this, so you might need to tweak it a bit still.

let
  unstable = import <nixos-unstable> { config = { allowUnfree = true; }; };

  obs-studio-wayland = unstable.obs-studio.overrideAttrs (prevAttrs: {
    nativeBuildInputs = (prevAttrs.nativeBuildInputs or []) ++ [ unstable.makeBinaryWrapper ];

    postInstall = (prevAttrs.postInstall or "") + ''
      wrapProgram $out/bin/obs --set QT_QPA_PLATFORM wayland
    '';
  });
in
{
  environment.systemPackages = [
    obs-studio-wayland
  ];
}
1 Like