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
  ];
}
2 Likes

I’ve just found this thread through search and here’s an update with a common contemporary strategy to handle this as it took me some time to figure out so here’s a quick step-by-step:


create overlays.nix next to your configuration.nix and create an overlay for your package using @R-VdP suggestion to extend the postInstall script:

self: super: {
  obs-studio = super.obs-studio.overrideAttrs (oldAttrs: {
    # extend old postInstall (if exists) with wrapProgram
    postInstall = (oldAttrs.postInstall or "") + ''
      wrapProgram $out/bin/obs --set QT_QPA_PLATFORM wayland
    '';
  });
}

Here the original packaging of obs-studio is being modified and post installation script is being added that wraps the obs executable with an env variable. The wrapProgram tool is the key ingredient here and it’s really poorly documented but the --set key value just wraps executable with environment setting.

Then in your configuration.nix you need to set the nixpkgs.overlays value with your overlays.nix import:

  nixpkgs.overlays = [ 
    (import ./overlays.nix)
  ];

now when you rebuild with sudo nixos-rebuild switch the package will be reinstalled with a new postInstall script that adds the env variable!

3 Likes

@wraptile I tried your approach and it worked nicely.

As I’m using flakes I didn’t want to use overlays, but created a copy of the original package with the modified attributes,and it worked.

I posted the code I used here: bug: alacritty no longer working on non-NixOS system · Issue #4720 · nix-community/home-manager · GitHub
I was patching alacritty to have an extra variable set (some OpenGL magic apparently).