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

build: run diff-hook under --check and document diff-hook #2798

Merged
merged 7 commits into from May 12, 2019

Conversation

grahamc
Copy link
Member

@grahamc grahamc commented May 10, 2019

for r13y.com

<para>The diff hook can be run as root. Take care to run as little
as possible as root, for this example we use <command>runuser</command>
to drop privileges.
</para>
Copy link
Member

Choose a reason for hiding this comment

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

Hm, rather than document the lack of security, we should just fix? E.g. run the hook under the build user ID?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, I updated the docs and it now runs as the build user / build group.

@grahamc
Copy link
Member Author

grahamc commented May 11, 2019

I want to emphasize that I don't write much C++, so please do carefully review this :P That said, it does work locally! :)

if (diff != "")
printError(chomp(diff));
} catch (Error & error) {
printError("diff hook execution failed: %s", error.what());
Copy link
Member

Choose a reason for hiding this comment

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

Returning the exit code from the diff-hook might be a nice alternative for #2779

Copy link
Member Author

Choose a reason for hiding this comment

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

mmmm I'd like #2779 better :) as returning this exit code means the diff hooks have to be very smart about their code to avoid being confusing with other Nix exit codes.

@arianvp
Copy link
Member

arianvp commented May 11, 2019

Related: #2619

I never got diff-hook to work so far, as Nix seems to ignore the option for me. Does this PR fix that?

@grahamc
Copy link
Member Author

grahamc commented May 11, 2019

@arianvp this PR doesn't change the handling of the options, although it does try to fix the mystery around why your options weren't working:

@grahamc
Copy link
Member Author

grahamc commented May 11, 2019

Also, if you follow the "tutorial" bit it should work!

src/libstore/build.cc Outdated Show resolved Hide resolved
throw SysError("setuid failed");

try {
auto diff = runProgram(diffHook, true, {tryA, tryB, drvPath, tmpDir});
Copy link
Member

Choose a reason for hiding this comment

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

To prevent a double fork, it would be nice to move uid-setting into runProgram, i.e. by adding uid/gid options to RunOptions.

Copy link
Member Author

Choose a reason for hiding this comment

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

That seems good, should I pass allowVfork in as a RunOption? How should it default?

</listitem>
</orderedlist>

<para>The diff hook should not print data to stderr or stdout, as
Copy link
Member

Choose a reason for hiding this comment

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

What exactly do you mean here? I guess it should print to stderr right? Otherwise how does the user figure out the output of the diff hook?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. None of the output printed by the diff hook is shown to the user, it all goes to the nix-daemon log.

Copy link
Member

Choose a reason for hiding this comment

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

But that doesn't mean it is 'bad' for a diff-hook to output something right? The sentence now seems to imply that diff hooks shouldn't print results. But we do want them to print results, such that we can actually see what the diff-hook found out, when looking at the nix-daemon logs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, though writing the diff report elsewhere might be a better bet. Do you have a suggestion on exactly how to reword this?

Copy link
Member

@arianvp arianvp May 12, 2019

Choose a reason for hiding this comment

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

Note that if the diff hook prints data to stderr or stdout, and nix-daemon is enabled, this is not displayed directly to the user. Instead,
it will be printed in the nix-daemon log

(Assuming here that if you use diff-hook without nix-daemon, it will show up directly to the user)

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 remember how this is implemented exactly, but I think it might be pretty easy to forward the output of the hook to the client. Similar to how this is done for build logs.

@@ -1025,6 +1026,16 @@ void runProgram2(const RunOptions & options)
if (source && dup2(in.readSide.get(), STDIN_FILENO) == -1)
throw SysError("dupping stdin");

//if (options.chdir && chdir((*options.chdir).c_str()) == -1)
// throw SysError("chdir failed");
Copy link
Member

Choose a reason for hiding this comment

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

Was this intended to be commented out?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops ... Commented that out thinking it was causing #2801. I'll uncomment and re-test. Good catch, thank you.

@grahamc
Copy link
Member Author

grahamc commented May 12, 2019

Uncommented, tested it, and pushed a fixup commit. I'm thinking it'd be best to merge this as a squashed commit -- no need for individual commits.

@edolstra edolstra merged commit d5c95e2 into NixOS:master May 12, 2019
@grahamc grahamc deleted the diff-hook branch May 12, 2019 21:18
LnL7 pushed a commit to LnL7/nix that referenced this pull request May 12, 2019
build: run diff-hook under --check and document diff-hook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants