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

Nixpkgs diff for forks #196

Merged
merged 11 commits into from
May 14, 2024
Merged

Nixpkgs diff for forks #196

merged 11 commits into from
May 14, 2024

Conversation

infinisil
Copy link
Member

This is super tricky stuff, but this should allow us to run the Nixpkgs diff for forks without safety issues (e.g. for #193). See https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ for some background.

Also included is:

Best reviewed commit-by-commit.


This work is sponsored by Antithesis

This is preparation for a future commit that makes this workflow usable
for PRs from forks
Makes it possible to use treefmt in the sync-pr.sh script without it
being single-threaded slow
@infinisil infinisil requested a review from tomberek April 19, 2024 02:42
@infinisil infinisil force-pushed the nixpkgs-diff-for-forks branch from 79c4675 to 93bd412 Compare April 19, 2024 02:50
@infinisil
Copy link
Member Author

The downside is that we'll need separate PRs to test changes to CI, because the workflows are taken from the base branch of PRs, not the PR branch. This is also why CI won't run in this PR.

I did just create a PR to verify it works though: tweag#3

@infinisil infinisil linked an issue Apr 19, 2024 that may be closed by this pull request
@piegamesde
Copy link
Member

Could you make it that CI takes it from the current branch if it is not a fork?

@infinisil
Copy link
Member Author

I think that only works in a hacky way by having it trigger for both pull_request and pull_request_target events, which will then trigger two events for every PR and therefore doubling the number of workflows started, and then maybe we can cancel half of them somehow. Haven't tried this yet, but I kind of don't expect it to work nicely.

@piegamesde
Copy link
Member

I see. Could you maybe make this based on the branch name? I.e. only trigger that CI when the branch is called ci and then we'll use only that for CI changes?

@infinisil infinisil force-pushed the nixpkgs-diff-for-forks branch from 93bd412 to 484fa57 Compare April 24, 2024 21:56
Copy link

github-actions bot commented Apr 24, 2024

Nixpkgs diff

Previously it ran potentially untrusted code (via result/bin/nixfmt).
Now it runs all fetched code in derivations for safety.

This makes it possible to use it safely for processing PRs from forks

(intermediate commit had `-- .` added to the `git checkout` command)

Fix sync-pr.sh script when commits existed already

Fixes the problem seen in https://github.com/NixOS/nixfmt/actions/runs/8824022484/job/24226382708?pr=196

This happens because `git checkout` only updates HEAD when no path was given.

And we need it to update HEAD, because otherwise it thinks
our files get overridden when we try to switch branches.
@infinisil infinisil force-pushed the nixpkgs-diff-for-forks branch from a61d385 to 276014f Compare April 25, 2024 00:14
@infinisil infinisil marked this pull request as draft April 25, 2024 02:33
By switching from pull_request to pull_request_target, which gives
access to secrets even for forks.

This is only safe because of parent commits making sure that all
untrusted code is run sandboxed in derivations.
More efficient and restricted
Allows running `nix-build -A ci` without relying on `nix-build`'s
recursion
Turns out `github.ref` doesn't point to the pull request's ref when
using `pull_request_target`: NixOS/nixpkgs#306430
CI is getting too slow, rebuilding too many derivations, especially now
that we depend on a patched treefmt (ironically the patch is there to
speed it up)
Hides warnings for:
- Not having a default branch name set
- Checking out non-branches
Now it can be run with nixfmt URLs that have any casing.

This was really annoying when I tried to debug CI using

  ./sync-pr.sh https://github.com/nixos/nixfmt 196 https://github.com/NixOS/nixpkgs

until I realised that CI used https://github.com/NixOS/nixfmt with a cased "NixOS"!
@infinisil infinisil force-pushed the nixpkgs-diff-for-forks branch from 276014f to 19d12a6 Compare April 25, 2024 03:45
@infinisil
Copy link
Member Author

I tried really hard, but it's just not reasonably possible. I managed to kind of get it working, but this is super hacky and broken in some cases.

So without that, I'm just testing it in a fork, and that does work: tweag#3. And the sync-pr.sh script can also be run locally, works just fine.

@infinisil infinisil marked this pull request as ready for review April 26, 2024 13:43
Copy link
Contributor

@tomberek tomberek left a comment

Choose a reason for hiding this comment

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

Looks straightforward. Ci build functions as expected.

@infinisil infinisil merged commit 3bcb63c into master May 14, 2024
@infinisil infinisil deleted the nixpkgs-diff-for-forks branch May 14, 2024 15:44
@infinisil infinisil mentioned this pull request May 14, 2024
@infinisil
Copy link
Member Author

Can confirm that it works, see #193 :)

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

Successfully merging this pull request may close these issues.

PRs from forks fail the Nixpkgs diff check
3 participants