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

nixos/firejail: add wrappedPackages option #45469

Closed
wants to merge 1 commit into from

Conversation

dotlambda
Copy link
Member

@dotlambda dotlambda commented Aug 22, 2018

Motivation for this change

Using this, not only the wrapped binaries but also all other directories from the derivations are put into /run/current-system/sw. This is useful if you need desktop entries, icons, etc.

My buildCommand is certainly far from perfect because I'm not good at programming bash. I'm open for suggestions.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Fits CONTRIBUTING.md.

cc @peterhoeg @adisbladis

@dotlambda
Copy link
Member Author

dotlambda commented Aug 26, 2018

This is a reminder for myself: I should try to do this using symlinkJoin. See e.g. https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/video/mpv/wrapper.nix

name = "firejail-" + pkg.name;
buildCommand = ''
mkdir -p $out
ln -s ${pkg}/* $out
Copy link
Member

Choose a reason for hiding this comment

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

You should quote variables like ${pkg} and $out as they may contain spaces (in case of a store path with spaces) and confuse bash.

Copy link
Member

Choose a reason for hiding this comment

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

Be careful not to include the glob in the quote though, so "${pkg}"/*

buildCommand = ''
mkdir -p $out
ln -s ${pkg}/* $out
rm $out/bin
Copy link
Member

Choose a reason for hiding this comment

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

What if the package doesn't have a bin directory? Doesn't make much sense in combination with firejail, but might happen. In that case there should probably be a useful error message.

ln -s ${pkg}/* $out
rm $out/bin
mkdir $out/bin
for bin in ${pkg}/bin/*; do
Copy link
Member

Choose a reason for hiding this comment

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

I think the variable name bin is confusing here. Maybe dir instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

These are actual extecutables.

Copy link
Member

Choose a reason for hiding this comment

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

Oh nevermind, I don't know why I thought they were subdirectories.

rm $out/bin
mkdir $out/bin
for bin in ${pkg}/bin/*; do
newbin="$out/bin/$(basename $bin)"
Copy link
Member

Choose a reason for hiding this comment

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

$bin should also be quoted. $() allows nested quotes, so

newbin="$out/bin/$( basename "$bin")"

Although why is basename even needed here?

mkdir $out/bin
for bin in ${pkg}/bin/*; do
newbin="$out/bin/$(basename $bin)"
cat <<_EOF >$newbin
Copy link
Member

Choose a reason for hiding this comment

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

Why the underscore? $newbin needs to be quoted as well.

newbin="$out/bin/$(basename $bin)"
cat <<_EOF >$newbin
#!${pkgs.stdenv.shell} -e
/run/wrappers/bin/firejail $bin "\$@"
Copy link
Member

Choose a reason for hiding this comment

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

quote

#!${pkgs.stdenv.shell} -e
/run/wrappers/bin/firejail $bin "\$@"
_EOF
chmod 0755 $newbin
Copy link
Member

Choose a reason for hiding this comment

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

quote

Using this, not only the wrapped binaries but also all other
directories from the derivations are put into /run/current-system/sw.
This is useful if you need desktop entries, icons, etc.
pkgs.symlinkJoin {
name = "firejail-" + pkg.name;
paths = [ pkg ];
buildInputs = with pkgs; [ tree ];
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this should be needed as a build input. All of the below commands are included in stdenv.

buildInputs = with pkgs; [ tree ];
postBuild = ''
for bin in $(find "$out/bin" -type l); do
oldbin="$(readlink "$bin")"
Copy link
Member

Choose a reason for hiding this comment

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

Should be indented here.

@@ -20,13 +19,37 @@ let
'';
};

wrappedPkgs = map (pkg:
Copy link
Member

Choose a reason for hiding this comment

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

I think naming here is a little confusing. wrappedPkgs vs wrappedPackages seems like they should be the same thing (although I suppose it's not any worse than wrappedBins vs wrappedBinaries).

for bin in $(find "$out/bin" -type l); do
oldbin="$(readlink "$bin")"
rm "$bin"
cat <<_EOF >"$bin"
Copy link
Member

Choose a reason for hiding this comment

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

Instead of manually creating shell scripts (they are admittedly basic), just use makeWrapper.

  makeWrapper $bin /run/wrappers/bin/firejail \
    --argv0 $bin \
    --add-flags $oldbin

Copy link
Contributor

Choose a reason for hiding this comment

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

I tried this before analyzing it and it failed with mkdir: cannot create directory '/run': Permission denied because the second argument to makeWrapper is the destination it tries to create. I don't see how makeWrapper could be made to work here.

@mmahut
Copy link
Member

mmahut commented Aug 9, 2019

Any updates on this pull request, please?

@stale
Copy link

stale bot commented Jun 1, 2020

Thank you for your contributions.
This has been automatically marked as stale because it has had no activity for 180 days.
If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.
Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the
    related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse. 3. Ask on the #nixos channel on
    irc.freenode.net.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 1, 2020
@KamilaBorowska
Copy link
Member

still important to me

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 15, 2020
@dotlambda
Copy link
Member Author

@xfix Feel free to finish this based on my work.

I think we should mention in the docs that desktop files stating an absolute path will start non-wrapped binaries. Maybe we could implement some check & warning for that.

@peterhoeg
Copy link
Member

peterhoeg commented Jun 15, 2020 via email

@SuperSandro2000
Copy link
Member

Replaced by #108204

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants