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

Separate headers from source files #12764

Merged
merged 3 commits into from
Mar 31, 2025
Merged

Separate headers from source files #12764

merged 3 commits into from
Mar 31, 2025

Conversation

Ericson2314
Copy link
Member

@Ericson2314 Ericson2314 commented Mar 27, 2025

Motivation

The short answer for why we need to do this is so we can consistently do #include "nix/...". Without this change, there are ways to still make that work, but they are hacky, and they have downsides such as making it harder to make sure headers from the wrong Nix library (e..g.
libnixexpr headers in libnixutil) aren't being used.

The C API alraedy used nix_api_*, so its headers are not put in subdirectories accordingly.

See comments on the script; this is supposed to avoid breaking muscle memory without complicating the build system (which proved harder than I thought too) or not doing the header hygiene change at all.

Context

Progress on #7876

We resisted doing this for a while because it would be annoying to not have the header source file pairs close by / easy to change file path/name from one to the other. But I am ameliorating that with symlinks in the final commit.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added documentation new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority store Issues and pull requests concerning the Nix store repl The Read Eval Print Loop, "nix repl" command and debugger fetching Networking with the outside (non-Nix) world, input locking c api Nix as a C library with a stable interface labels Mar 27, 2025
@Ericson2314 Ericson2314 force-pushed the header-file-namespacing branch 2 times, most recently from 25a1909 to 923e0e8 Compare March 27, 2025 05:31
@Mic92 Mic92 force-pushed the header-file-namespacing branch from 1e0108a to b6cb4be Compare March 27, 2025 11:24
@Ericson2314 Ericson2314 force-pushed the header-file-namespacing branch 3 times, most recently from 2cda96e to f930f15 Compare March 28, 2025 16:06
@roberth
Copy link
Member

roberth commented Mar 28, 2025

Glad that the symlinks aren't committed.

my earlier misread

I don't think the symlinks are a good idea, because it opens up more opportunities for various tools and editors, as well as users to get confused.

Furthermore I believe in practice this isn't as useful as it seems.
For example, consider the following

  1. user navigates to a certain header
  2. editor follows the symlink
  3. editor buffer has include/... as its file name
  4. user opens accompanying tree view / file open UI / default basedir, expecting to navigate to a sibling .cc file
  5. user is launched into the includes dir, not the one with the .cc files

Also I've seen instances of symlink contents appearing as "distinct" files while they are not, e.g. allowing both the canonical and non-canonical paths to have an editor buffer simultaneously, leading to conflicts if the user isn't paying attention to that. Obviously that's an editor bug, but this is the kind of problem we're opening ourselves up to by inviting in more symlinks.

And that's assuming the bug is in the tooling. Humans also have to make sense of it.
I'd much prefer a simple solution to the include namespace problem, without symlinks.

@Ericson2314 Ericson2314 force-pushed the header-file-namespacing branch from f930f15 to 31b2417 Compare March 28, 2025 17:04
@roberth
Copy link
Member

roberth commented Mar 28, 2025

       > ../nix_api_util.cc:10:10: fatal error: config-util.h: No such file or directory
       >    10 | #include "config-util.h"

Looks like an interaction with #12773. config-util.h is a private header here.

nix build .#nix-util-tests.tests.run

@Ericson2314 Ericson2314 force-pushed the header-file-namespacing branch from 31b2417 to 385be10 Compare March 28, 2025 17:30
@Ericson2314 Ericson2314 added backport 2.26-maintenance Automatically creates a PR against the branch backport 2.27-maintenance Automatically creates a PR against the branch labels Mar 28, 2025
@edolstra edolstra removed backport 2.26-maintenance Automatically creates a PR against the branch backport 2.27-maintenance Automatically creates a PR against the branch labels Mar 28, 2025
@edolstra
Copy link
Member

Note: we really shouldn't have backport labels on a PR that changes 666 files. Code restructurings like this are inappropriate for the maintenance branches.

@edolstra
Copy link
Member

What's the impact of this on IDEs, i.e. can editors still follow a line like

#include "nix/machines.hh"

to the corresponding file in the Git repo?

Also, shouldn't the subdirectory of the component be included, e.g.

#include "nix/libstore/machines.hh"

?

@Ericson2314 Ericson2314 added backport 2.26-maintenance Automatically creates a PR against the branch backport 2.27-maintenance Automatically creates a PR against the branch and removed backport 2.26-maintenance Automatically creates a PR against the branch backport 2.27-maintenance Automatically creates a PR against the branch labels Mar 29, 2025
@Ericson2314 Ericson2314 force-pushed the header-file-namespacing branch from 385be10 to fd716fa Compare March 29, 2025 01:10
@Ericson2314
Copy link
Member Author

What's the impact of this on IDEs, i.e. can editors still follow a line like

#include "nix/machines.hh"

I don't know, but see the symlink script that is included. With that, your workflow should be exactly the same as before,

@Ericson2314
Copy link
Member Author

Also, shouldn't the subdirectory of the component be included, e.g.

#include "nix/libstore/machines.hh"

I was starting more minimally, but if you prefer that, I'm happy to jump all the way to that.

Just one thing, I would do

#include "nix/store/machines.hh"

I don't think the lib prefix is adding anything of value.

@Ericson2314
Copy link
Member Author

Ericson2314 commented Mar 29, 2025

Actually, because the per-project dirs is a less mechanical change, I would rather do it in a follow-up please. Happy to make it right-away, however.

@Mic92
Copy link
Member

Mic92 commented Mar 29, 2025

@Ericson2314 btw. I refactored the python script a bit but you seemed to have pushed over this change again: b6cb4be

@Mic92 Mic92 force-pushed the header-file-namespacing branch 2 times, most recently from b6cb4be to fd716fa Compare March 31, 2025 16:06
@Ericson2314 Ericson2314 force-pushed the header-file-namespacing branch from fd716fa to c8eed31 Compare March 31, 2025 16:12
- Since it's now private, give it a rename. Note that I want to switch the
  word order on the public ones too.

- Since it is only needed by two files, just include there rather than
  the nasty blanket-forced thing.
The short answer for why we need to do this is so we can consistently do
`#include "nix/..."`. Without this change, there are ways to still make
that work, but they are hacky, and they have downsides such as making it
harder to make sure headers from the wrong Nix library (e..g.
`libnixexpr` headers in `libnixutil`) aren't being used.

The C API alraedy used `nix_api_*`, so its headers are *not* put in
subdirectories accordingly.

Progress on #7876

We resisted doing this for a while because it would be annoying to not
have the header source file pairs close by / easy to change file
path/name from one to the other. But I am ameliorating that with
symlinks in the next commit.
@Ericson2314 Ericson2314 force-pushed the header-file-namespacing branch from c8eed31 to f3e1c47 Compare March 31, 2025 16:21
@Ericson2314
Copy link
Member Author

OK this no longer includes the symlink script --- the plan is that @Mic92 will make that in a separate PR (he is working on it right now).

@Ericson2314 Ericson2314 added the backport 2.28-maintenance Automatically creates a PR against the branch label Mar 31, 2025
@Ericson2314
Copy link
Member Author

@mergify queue

Copy link
Contributor

mergify bot commented Mar 31, 2025

queue

✅ The pull request has been merged automatically

The pull request has been merged automatically at 5a8dedc

@Ericson2314
Copy link
Member Author

As to

#include "nix/store/machines.hh"

I think we should do that, but that should also be in a separate PR, since it is a bit less mechanical than this, as I said above.

mergify bot added a commit that referenced this pull request Mar 31, 2025
@mergify mergify bot merged commit 5a8dedc into master Mar 31, 2025
27 checks passed
@mergify mergify bot deleted the header-file-namespacing branch March 31, 2025 18:03
mergify bot added a commit that referenced this pull request Mar 31, 2025
…2764

Separate headers from source files (backport #12764)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.28-maintenance Automatically creates a PR against the branch c api Nix as a C library with a stable interface documentation fetching Networking with the outside (non-Nix) world, input locking new-cli Relating to the "nix" command repl The Read Eval Print Loop, "nix repl" command and debugger store Issues and pull requests concerning the Nix store with-tests Issues related to testing. PRs with tests have some priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants