Skip to content

setup-hooks: add auto-patch-pc-hook#394610

Open
marcin-serwin wants to merge 1 commit intoNixOS:masterfrom
marcin-serwin:push-qyxlqonrzxnu
Open

setup-hooks: add auto-patch-pc-hook#394610
marcin-serwin wants to merge 1 commit intoNixOS:masterfrom
marcin-serwin:push-qyxlqonrzxnu

Conversation

@marcin-serwin
Copy link
Contributor

patch-pc replaces dependencies in pc files with their absolute paths, auto-patch-pc-hook automatically runs it during fixup phase for all .pc files produced by the package. See #334195 for motivation. Feel free to also comment on the patch-pc code in this PR, I'll tag it as 0.1.0 once I'm sure no more changes are necessary.

I am currently in the process of rebuilding my personal NixOS system on top of the #394607 branch. So far it's going well, the only problematic package is xorgproto which produces .pc files that reference packages depending on xorgproto, creating circular dependency. I've patched them out for now, not sure what's the proper way to address it.

cc @emilazy

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@nix-owners nix-owners bot requested a review from Ericson2314 March 30, 2025 13:32
@github-actions github-actions bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Mar 30, 2025
@emilazy
Copy link
Member

emilazy commented Mar 30, 2025

cc @alyssais @K900

Thanks so much for working on this!

I think we might want this hook to be in stdenv as an awful lot of packages produce .pc files? But OTOH that does mean another tool in the bootstrap. I guess it will usually fail loudly, though, so perhaps this is fine.

I’d like to request you pick another licence, if possible; CC0 is not really designed for code, and has an explicit disclaimer against granting patent rights that has led to e.g. Fedora rejecting its use for software.

@K900
Copy link
Contributor

K900 commented Mar 30, 2025

I think it makes sense to just put the code for the tool in nixpkgs.

@marcin-serwin
Copy link
Contributor Author

marcin-serwin commented Mar 30, 2025

I’d like to request you pick another licence, if possible; CC0 is not really designed for code, and has an explicit disclaimer against granting patent rights that has led to e.g. Fedora rejecting its use for software.

Changed to Unlicense

I think it makes sense to just put the code for the tool in nixpkgs.

Should the files go into pkgs/by-name/pa/patch-pc/ and the src changed to ./ or is there some other procedure for this?

@K900
Copy link
Contributor

K900 commented Mar 30, 2025

I'd put it in a subdirectory, personally, just for convenience.

@emilazy
Copy link
Member

emilazy commented Mar 30, 2025

I don’t think the code needs to be in Nixpkgs; we depend on plenty of tools like patchelf that aren’t. But if it is, we should probably make it MIT. Otherwise if someone copied code from elsewhere in Nixpkgs into the tool it could cause a headache.

@K900
Copy link
Contributor

K900 commented Mar 30, 2025

I thought it was smaller, to be honest, but also, looking at it now... Do we really have to reinvent string handling in C again? Can we do Python or something?

@marcin-serwin
Copy link
Contributor Author

Can we do Python or something?

Python depends on curl, this would make the bootstrap more complicated.

@K900
Copy link
Contributor

K900 commented Mar 30, 2025

We don't need curl if we vendor the sources though?

@marcin-serwin
Copy link
Contributor Author

marcin-serwin commented Mar 30, 2025

curl will depend on this package to patch its .pc file. Even with vendored sources this creates patch-pc -> Python -> curl -> patch-pc cycle.

@K900
Copy link
Contributor

K900 commented Mar 30, 2025

Can we just leave this out of some of the bootstrap? Or go with C++ or Rust or something. Hell, at least glib?

@marcin-serwin
Copy link
Contributor Author

marcin-serwin commented Mar 30, 2025

Can we just leave this out of some of the bootstrap?

Probably, that's what I meant when I said that it'll make the bootstrap more complicated.

Or go with C++ or Rust or something. Hell, at least glib?

rustc depends on curl, glib has .pc files to patch. C++ is viable I guess.

@K900
Copy link
Contributor

K900 commented Mar 30, 2025

I'm really really sorry if I'm coming on too strong here, but I also really, really don't want to see more C string manipulation code in nixpkgs, or anywhere for that matter. Skipping this for a few bootstrap packages is absolutely worth the maintenance cost savings of not doing it in C.

@emilazy
Copy link
Member

emilazy commented Mar 30, 2025

Probably much of the manual string parsing could be avoided by using the libpkgconf API.

@marcin-serwin marcin-serwin marked this pull request as draft March 30, 2025 18:29
@marcin-serwin marcin-serwin marked this pull request as ready for review April 1, 2025 20:54
@marcin-serwin
Copy link
Contributor Author

I've rewritten it in python. Turns out that with python3Minimal the bootstrap changes were minimal. I'm still rebuilding my system with this hook, but more than half of my packages have built already so I think that it's safe to say that the hook itself works.

@marcin-serwin marcin-serwin changed the title patch-pc: init at 0.1.0; create auto-patch-pc-hook setup-hooks: add auto-patch-pc-hook Apr 1, 2025
@github-actions github-actions bot added 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux. labels Apr 1, 2025
@K900
Copy link
Contributor

K900 commented Apr 2, 2025

Sorry for making you rewrite this! But I absolutely love the new version.

@marcin-serwin marcin-serwin marked this pull request as draft April 8, 2025 21:10
@marcin-serwin marcin-serwin marked this pull request as ready for review April 15, 2025 22:04
@marcin-serwin
Copy link
Contributor Author

I've managed to build my system atop of #394607, but I had to make some chanages:

  • I've removed dependency on findutils to simplify bootstrap. It's replaced by more specific globbing which I think covers all the places where the .pc files may be placed.

  • The hook relies on PKG_CONFIG_PATH being populated which is done by pkg-config-wrapper. I added pkg-config as a dummy propagated build input until switch to pkgconf is complete. After the switch, pkgconf can be propagated and used directly by the hook to avoid passing the binary as a parameter.

  • When building libreoffice, I've hit "output '[...]' is not allowed to refer to the following paths" error due to the disallowedRequisites present in this derivation. I suspect that after making the .pc requires absolute they are picked as direct dependencies for some derivations. Since .pc files are sometimes present in the out output instead of the dev output the libreoffice picks them up as transitive deps and that's why the error occurs. I've worked around this by switching from disallowedReferences to disallowedReferences which only checks the package itself and does not recurse into deps.

  • The bootstrap for linux only involved special casing for fetchurl which is localized to few overrides in top-level/all-packages. However, the darwin process is a bit more involved due to code signing – my attempt at fixing it is spread across several files. Is there a better way to do it?

Copy link
Contributor

@LordGrimmauld LordGrimmauld left a comment

Choose a reason for hiding this comment

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

Some more documentation and explanation would be nice.

The changes themselves look fine honestly - so far nothing uses this, but this is quite compact and certainly more maintainable than the C proposal you had.

Feel free to ignore my python nits, my coding style in py is a bit peculiar. But figured i'd bring those up, even if they end up ignored. Your code is very clean. Noone stops learning, discussing the best way to do things can be nice. But i also don't want to turn this to bike shedding hell, so if that ends up too annoying just close those comments please!

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: autoPatchPcHook might not be very descriptive, autoPatchPkgConfigHook might be easier to understand when looking at any package that would use this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this actually intended to be a different pkgconf than above propagated pkg-config? Might it make more sense to make a let...in for the whole block and use the same here as in propagation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this actually intended to be a different pkgconf than above propagated pkg-config?

Yes, the pkg-config in propagated build inputs is the freedesktop's implementation which is currently the default in nixpkgs. We can't use it in the patch-pc.py because it does not support the --path flag. I want to avoid packages having both pkg-config and pkgconf in their nativeBuildInputs so I propagate the pkg-config to ensure that its setup hook is registered while the script uses pkgconf. Once nixpkgs switches to pkgconf this can be simplified to propagating pkgconf and the script using the the program from PATH.

Copy link
Contributor

Choose a reason for hiding this comment

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

stylistic nit, feel free to ignore:

Suggested change
print(f"Requires{".private" if private else ""}:", ", ".join(requires))
print(f"Requires{".private" if private else ""}: ", end="")
print(*requires, sep=", ")

You could technically also replace ".private" if private else "" with ".private" * private because you are checking for bool parameter, but honestly don't, that would be too deep into python weirdness. But the print is a serious suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied the print suggestion.

Copy link
Contributor

@LordGrimmauld LordGrimmauld Apr 17, 2025

Choose a reason for hiding this comment

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

Stylistic nit (feel free to ignore):

Suggested change
requires = (transform_dep_spec(dep) for dep in list_requires(file, private))
requires = map(transform_dep_spec, list_requires(file, private))

We do functional programming in nix, a map might be more appropriate than list comprehension, but i am happy with either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't write a lot of Python but my understanding is that list comprehensions are considered more pythonic than the functional equivalents, e.g., see here.

@github-actions github-actions bot added 8.has: documentation This PR adds or changes documentation and removed 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux. labels Apr 19, 2025
Copy link
Contributor

@LordGrimmauld LordGrimmauld left a comment

Choose a reason for hiding this comment

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

Thanks for the documentation! Now the need for it is clearer, and when/how to use too. The python code looks clean, and the nix/bash part looks at least manageable. I am not too familiar with stdenv things, my approval shouldn't be sufficient to blindly merge this, but no complaints left here.

@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one person. label Apr 19, 2025
@K900
Copy link
Contributor

K900 commented Apr 21, 2025

I think we should enable this by default and have an opt-out in case it breaks things. Still need to review the Python bits closer.

@marcin-serwin
Copy link
Contributor Author

I think we should enable this by default and have an opt-out in case it breaks things. Still need to review the Python bits closer.

How can we do an opt-out that also removes runtime dependencies? Other hooks I've looked at do something like if [ -z $dontRunSomeHook ]; then ... in the bash script but this will not work for bootstrap.

@K900
Copy link
Contributor

K900 commented Apr 21, 2025

Can do something in mkDerivation I guess, but yeah, that might be a problem...

@LordGrimmauld
Copy link
Contributor

Maybe stdenv people have better ideas how to do this "properly"? @emilazy boop

@LordGrimmauld
Copy link
Contributor

LordGrimmauld commented Apr 21, 2025

One option might be to add the hook to pkgconf.propagatedNativeBuildInputs? It doesn't need to be in all of stdenv i think. Actually no that would get recursion...

@Aleksanaa Aleksanaa added the 2.status: wait for branch‐off Waiting for the next Nixpkgs branch‐off label Apr 21, 2025
@LordGrimmauld LordGrimmauld removed the 2.status: wait for branch‐off Waiting for the next Nixpkgs branch‐off label May 28, 2025
@LordGrimmauld LordGrimmauld mentioned this pull request Aug 20, 2025
13 tasks
@nixpkgs-ci nixpkgs-ci bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Oct 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants