Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to wrap nix derivations with makeWrapper #133

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,47 @@ For Vulkan programs:
```bash
$ nixVulkanNvidia program args
$ nixVulkanIntel program args

## Wrapping applications
If you are using home-manager, you can use this approach to avoid having to add the wrapper call
each time:

```nix
let
nixGLWrap = binary: drv: pkgs.symlinkJoin {
name = "${drv.name}-nixglwrapped";
paths = [ drv ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
# This will break if wrapProgram is ever changed, so fingers crossed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are not using wrapProgram directly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, there's no way to tell makeShellWrapper to make an actual wrapper in the sense that nixGL wraps stuff, so I overrode it with what we need here.
I still wanted to use wrapProgram though because it has some logic to correctly deal with multiple layers of wrapping.
If you think it would be better I'll inline it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double wrapping as you describe it should be avoided as much as possible as it is very hard to get right. I think it would probably be an idea to modify the existing wrappers to fit your usecase to create one wrapped binary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be misinterpreting that, but my reading of the code is that this is what wrapProgram is for. I'm actually wrapping mixxx twice myself (nixGL and adding LD_LIBRARY_PATH for jack), and I'm pretty sure the package adds another wrapper itself.

This is obviously not optimal, but it makes things somewhat composable.

Anyways, I don't think we really have something to argue about here. I just found this solution for myself and thought it would be valuable to share.
I'm personally a bit hesitant to propose using wrapProgram directly because it will not work on packages which use a wrapper internally.

It's your decision what to put in the readme, I'm just offering a suggestion from my personal point of view.

Copy link
Member

@Artturin Artturin Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, there's no way to tell makeShellWrapper to make an actual wrapper in the sense that nixGL wraps stuff, so I overrode it with what we need here. I still wanted to use wrapProgram though because it has some logic to correctly deal with multiple layers of wrapping. If you think it would be better I'll inline it.

You should be able to do it the same way that electron programs are handled in nixpkgs

makeWrapper ${electron}/bin/electron $out/bin/${pname} \
  --inherit-argv0 \
  --add-flags $out/share/${pname}/resources/app

Also please use makeBinaryWrapper, inherit-argv0 works better with it AFAIK.

makeShellWrapper() {
local original="$1"
local wrapper="$2"
cat << EOF > "$wrapper"
#! ${pkgs.bash}/bin/bash -e
exec "${pkgs.nixgl.auto.nixGLDefault}/bin/nixGL" "$original"
EOF
chmod +x "$wrapper"
}

wrapProgram "$out/bin/${binary}"
'';
};
in {
home.packages = [
(nixGLWrap "glxinfo" glxinfo)
];
}
```

This generates a wrapper script that automatically adds the `nixGL` call so you won't have to
remember it.

This could be done simpler with
`writeShellScriptBin "glxinfo" "${..}/bin/nixGL ${glxinfo}/bin/glxinfo"`
but this way, only the binary will be available in your environment and you'll [lose manpages and
possibly important supporting files](https://nixos.wiki/wiki/Nix_Cookbook#Wrapping_packages).

# OpenGL - Hybrid Intel + Nvidia laptop

After installing `nixGLIntel` and `nixGLNvidiaBumblebee`.
Expand Down