As you can see, there is kind of a looping error Failed to load module "libinput" (module does not exist, 0) appearing in the log. What could be the reason? Before startx, libinput was configured as in the handbook: https://wiki.gentoo.org/wiki/Libinput
The second error that bothers me is Screen 0 deleted because of no matching config section. It is quite obvious that there is no configuration file for my device monitor and therefore it can't be found, but wasn't it said in the Xorg guide that it is designed to run out-of-the-box and thus creates the necessary files on its own? If it's how Xorg is supposed to work, why doesn't it create a config for the monitor? If not, how do I do it on my own? The xorg guide on configuration and sections is pretty tough to understand.
Btw, my /etc/X11/xorg.conf is not configured anyhow specifically - just standard configuration I found in the handbook. The same applies to all nvidia and modesetting conf files (the last one is instead of intel conf file) in /etc/X11/xorg.conf.d/ directory - nothing exotic in them.
I also tried the /etc/X11/xorg.conf configuration provided by nvidia-xconfig command to test if anything changes in the log, but sadly to no success.

Let's start with the most basic way of starting Xorg, which is what happens when you type in "X", just plain X no quotes from the command line.

Make sure that line of a user included in the video group (this is important). If you are getting the same error messages, which you will likely then the kernel is probably the next place to look, but I haven't had a chance to check it out and see for myself.

EDIT: Also please verify if you followed the instructions for setting up X, other than that one point you mentioned. Please note, you are using an out of tree kernel module as a driver (nvidia...that's proprietary), often times what happens with it is that it breaks the system if it isn't reinstalled (use emerge @module-rebuild for that). That is if you updated the kernel you must also remember to update your nvidia package so that everything gets installed again for the new kernel. Do you remember seeing that somewhere in either the installation handbook or the nvidia drivers explained in the wiki. It's there.

Jaglover, I used Nvidia/Optimus, Nvidia-drivers, intel and Xorg/guide pages of the wiki to set up X.

Problem with libinput solved after emerge -e @world (I was switching to 17.0 profile alongside with upgrading gcc)
So now I have only one error (that one with Screen 0 deleted because of no matching config section) and font warnings, which are not that important really.

LIsLinuxIsSogood, "X" gives just a black screen with a dash in the upper left corner. Can't leave it with CTRL+ALT+BACKSPACE as said on the web.

Code:

X &>log.txt

outputs nothing (the command has no STDOUT and STDERR). I should also mention that I have no DM, WM or DE installed yet as I need to solve the Xorg problem now and then decide

Try to post the contents of these files here in case others may be able to help to assess the damage done. It seems very possible this was while running nvidia-xconfig, which according to the post above, and other sources as well can do some pretty messy job in trying to arrange X configuration files. That is you may have to step back and undo some of the mess that is before you are back up and running iwht a WM.
Trying different configuration files (or borrowing from sections of files) to see if by cutting and pasting those you have a situation that works for Xorg. That could possibly work. As could just removing the confiruation files altogether and starting fresh. Take note something else interesting I just learned while looking it up that X itself has a auto-configuration tool, kinda cool.

But in order to also make sure you don't go losing it while trying to get the GUI and graphics up on your device, keep this in mind...The goal isn't to remove the screen error (that much was clear to me before I read the link above). The goal is a working UI, or X11 display. Therefore it is best to consider that error message to be a piece of the puzzle only, and do some testing out of different things even if it causes more error messages, then those will help to start direct you to the underlying issues. Since anyway it appears that some other linux users are able to have their UI and X11 displays in good order even with this error message, so I wouldn't put too much into just that one line of output.

Does that make sense Also if you have the time just look across the different distributions all have wiki's for this kind of a thing. Not every time is the description for just that one distribution and it can help to refer back to at times, like with Arch Linux, or Debian, Ubuntu or whatever. They all have some good information you can at least attempt while exploring.

LIsLinuxIsSogood,
I suppose that some of my problems might have been caused by the 'nomodeset' option in my kernel boot parameters. I set it earlier when I was solving one of the graphical problems during boot and, as it seems, forgot to remove. Without nomodeset I got a very different X log, and as far as I understand, it now seems to work properly (it just terminates because it has nothing to do - no WM or DE to launch). However, there are a couple of warnings and errors in the log, but I don't think they are that crucial.
https://paste.pound-python.org/show/cC22PW0LSSYsmZDX6eFb/

UPD: after installing openbox and typing "startx" I have just a black screen with no buttons and touchpad working (solveable only with reboot). The config is just basic for openbox, I just added it to /etc/env.d/90xsessio and ~/.xinitrc.
However, as it is expected, it should have provided some very basic interface. So I guess something is still wrong with Xorg

I concur that nothing is maybe wrong with your X. The log you posted had only one error that was for glamor, and that I believe is an optional flag for the correct running of it.

I don't think that openbox was your best bet, but you have the right idea to install something for X to pass along the arguments of it (I'm using the term argument here loosely to mean the environment, and the screen and inputs and all that stuff.)

Past methods that have worked for me was to utilize a very minimal window manager to test my X configuration.

Have you tried that? Check to see what session options you may have. Also, since startx is simply the front end for xinit and X, those are the two scripting and programming interfaces to the X display, I would refrain from testing with startx until you've achieved some modestly acceptable results like a screen with mouse and keyboard capabilities.

Please describe the situation in terms of what is and isn't working with it. You get a black screen, that much I understand. But then what? Does it crash or do you have to switch vt's or cancel the server or what is it tha happens next?

LIsLinuxIsSogood, well, in case it's something wrong with openbox I tried emerging xterm and then startx, but it was still the same black screen, so I conclude that the startup process doesn't even reach openbox or xterm or whatever, so the problem lies in my X or driver configuration maybe (despite I don't see any critical errors in the startx log)

You should read up on using virtual console, and also the process or task way of killing processes and... tasks.

NOTE: Make sure you go slowly when making changes to the configuration files, and document what you've tried somehow that way you won't be repeating old work. I know it is boring and sounds like nonsense, but it will help if you can do it.

while maybe you will have to try a couple different variations and that is expected with the software (X11 is a very adaptable software working on just about every single machine you can imagine, but one thing it does very well is to adapt to each situation by guessing the drivers needed and probing hardware, but one thing it can't do is actually undo what you've done to configure

take my advice, and the help offered here, I know it can be frustrating to have to wait. Here's the list of things you can do.

1) Go back and read/follow the wiki carefully, it will get you the results you need if you pay attention to each section
2) Specifically make sure to incorporate the instructions about you particular version of X (as part of teh configuration, there isa warning mid page about it and tells you what to do)
3) Same for xrandr, and from the looks of it you will have to incorporate a couple of scripted lines into your .xinitrc file for that. (You can remove the openbox lines, and for now it would be better if you installed another more minimal wm like twm, or wmii or something just to test.)

This isn't necessary but it also wouldn't hurt because it is such a small program that it won't likely get in the way regardless.

Code:

emerge -v x11-wm/twm.

Everything BELOW this line is actually just copied from the wiki page, and I suggest that you review IT but then ALSO TRY to make your questions about what is there, since the instructions tend to work that are posted there. That is unless you are trying to do something very diferent with the X11 software on your machine.

Warning
The xrandr commands must be added to the system's X session start up scripts (such as ~/.xinitrc) in order for the X display to start using modesetting. Failure to do so will result in a black screen.

Manually adding the lines, after you take a look at the example in the wiki page should be no problem and it may clear the whole thing up.

Also as I mentioned in the wiki is this
If X.Org X server version 1.17.2 or higher is installed ([1])

To remind you again, with startx being just a program that basically will run xinit, which itself is a small script to start X with some applications, something you may want to do is
1)

If you are familiar and aware with the files in /etc/X11/xinit and how they are supposed to look it is worth a look into there...how familiar with those are you? Make sure you either have a working xinitrc file in that folder, or else one in your user folder at ~/.xinitrc

2)

To clean things up (others may disagree or agree here IDK) you may want to unmerge the xorg-server package, and then reinstall it. That alone could do the trick.

Disclaimer: What you should know is the process of troubleshooting the GUI can be a bit tricky. But with no errors in the X log, it would appear that you've made some progress by making the changes to the kernel so that's a good start.

Well, it seems like I have some kind of progress now. I unmerged openbox and removed al of its config files for easier debugging process. I also emerged twm in addition to xterm in order to test X. I also re-checked all of the config files related to Xorg in /etc/X11 directory so they are written according to the wiki guides. For xorg.conf, I used the following configuration: https://wiki.gentoo.org/wiki/NVIDIA/Optimus/xorg.conf.

Also, thanks for your advice about xrandr, I actually forgot to add the proper lines to ~/.xinitrc. Now it looks like that:

From this point, startx was giving me black screen even without an ability to change display with ctrl+alt+f*. So then I created an ~/.xsessionrc file as advised in the wiki and copied the contents of ~/.xinitrc there. After that, I got some result which can be seen in the photo. However, I can do nothing with that - the cursor doesn't move and changing the display is also not possible. Well, at least it's not completely black screen now.
https://imgur.com/95PBWY7
By the way, xrandr is still not working if I call it from the shell: xrandr command (with or without options) says 'can't open display' and I can't quite figure out why

Also, a question not directly related to the topic - why do I have some kernel boot parameters set (I can see them when I type 'e' in grub boot menu), even though I don't have any in /etc/default/grub, where they are supposed to be configured? This confuses me a bit

There's a lot that can be done with these scripts, and also even more with the implementing of xinitrc or other session management. If I were you I would immediately take a look at something called a login manager... https://wiki.gentoo.org/wiki/SLiM , it will help you immensely to figure out how to switch between different sessions.

NOTE: The login manager will also help with any possible errors in the xinit script for the sessions wm, which I think you can separate for now as I mentioned remove it from xinit file, and just keep the xrandr stuff there, since soon you'll want to bounce from one to another sessions and not be limited to twm and xterm, agreed?

startx /usr/bin/twm provides me with a black screen, can't open another display too
maybe editing xinit scripts may help? However, I am not familiar yet with how they work

what about boot parameters - I have nomodeset and ro activated each boot, so I need to manually delete nomodeset from boot parameters each boot in order for my X to work properly. It seems strange to me as these parameters are defined in CONFIG_CMDLINE_DEFAULT variable in /etc/default/grub, however I have nothing written there. This confuses me a little bit

stormblade,
Can you please repeat the process, but make sure to also confirm if the user is in the video group, with

Code:

storm% id

Just verifying that. Also, perform a quick check for the location of the window manager you will be using with startx...to do this use the which command or else some other tool to locate the binary application for the wm.

It should probably be the same as the file I mentioned which is /usr/bin/twm, but this is to ensure that it is before you go using that to pass as a argument to X.

This time, when you run the command startx and include the path that you found out for your window manager. Then post back here with the log for Xorg!!!!

Then the boot parameters there's got to be an explanation for the way they showed up, but I don't know the answer to it... One place I might tell you to check anyway is within the kernel, under the kernel setting CONFIG_CMDLINE. More importantly, if you are unhappy with the way the options look when using grub-mkconfig, the process of writing your own grub.cfg is really not that difficult and you can research and do it shouldn't take much time.

In case you are thinking to try with a different window manager, don't bother at this point since the purpose of the window manager is just as a testing tool so it doesn't matter which it is as long as it is basically a working one.

What we don't want to do is to try troubleshooting a problem with X with a wm that has some complex problems of its own (which I would include in there the Gnome or plasma KDE or something like that). Catch what I'm getting at?

The other things you should inform here more about if you've tried any of the following (all of these are mentioned in the wiki):
1. Saving the screen's EDID to a file for use
2. Attempting different xorg.conf like the one in the gentoo wiki page for NVidia/Optimus/EDID Xorg.conf Example
3. Have you installed any display manager or login manager and these can often take the place of some of the tougher configuration challenges like this one possibly even.
4. Have you read up on the need to be doing this if KMS is enabled then you need to be sure that is isn't in conflict with other "older" drivers. There's a few ways this could be done, and one is at the boot line, another could involve removing kernel support for certain devices. Here's what's stated in the Xorg/Guide..

Code:

Important
KMS conflicts with legacy framebuffer drivers, which must remain disabled in the kernel configuration.

Stormblade, I also must admit that the issue with graphics is really that is a subject matter a bit more than i can handle in terms of helping you to troubleshoot. Maybe start another post, or else wait to see if someone will step in, since it is more than likely an issue with how you followed the wiki. I can relate with the fact that wiki articles are often very choppy and not laid out in a sensical way, so better to just describe the problem and post the error messages and let someone try and spot the specific problem with the card your using. I'm stepping aside in other words.

LIsLinuxIsSogood,
thanks for all the advice you've given to me, I've reviewed all the stuff again and found some nasty mistakes I've made during the configuration (like forgetting to add some xrandr options to the .xinitrc and .xsessionrc). Also I removed an odd option from my VIDEO_CARDS variable (can't remember why I even put it there, it was i965 option, that one dealing with some intel graphic cards) and updated world after that. And imagine what - I've got X working, despite a couple of errors that don't seem to affect anything (at least yet) and the nature of which I didn't figure out by that time. Now I'm setting up Openbox and all the stuff, really enjoying the process

So, alongside with some of my own mistakes, I guess it were some dependency conflicts between intel i915 and i965, I believe, that caused so much trouble.

Speaking about Arch wiki - that's a great resource, I look there sometimes because gentoo's one is less verbose on some topics (and that's quite sad), or just some pages are not updated for a while. I hope to see the improvement of the situation in the future and wouldn't even bother writing something on my own, but I am just inexperienced to do so... let's see how it goes

I really just needed to mention it sooner, looking back I had a relative similar experience in trying to get things working, but also here is a post that deals with the issue for the generation of processor that you are dealing with (I think) that requires the kernel boot parameters. You can try this. Or else, please expand...how did you get around the conflict between drivers for intel (915 v 965)

LIsLinuxIsSogood, ty for the topic, I'll take that kernel boot parameter into consideration. I haven't faced such type of a problem with the screen yet, so that's not quite related to my issues, but may be useful anyway.

As for my conflict - I forgot to tell one more thing about what I've done to overcome it. Besides the VIDEO_CARDS='intel i915 i965 nvidia' (which I then changed to 'intel i915 nvidia'), I also had odd options there INPUT_DEVICES='libinput keyboard mouse'. As I've figured out, either libinput or evdev are enough in most cases to get everything working, so I left libinut alone there and update world in order for the changes to take place. Then I reconfigured it a little bit (like, removed the symlink in /etc/X11/xorg.conf.d/) because it was by then the only input driver I had; I also recompiled dev-libs/libinput and x11-drivers/xf86-input-libinput. And after that I changed VIDEO_CARDS and made another world update (I know I could do it at one time and just do all the stuff with one world update, but I came up with these ideas not simultaneously and, anyway, when it comes to debugging and troubleshooting, it's better to do everything step by step).
So, after all that I mentioned, I got a working X, with this log:
https://paste.pound-python.org/show/NCMBCnwDlK7sug6IHfcR/

LIsLinuxIsSogood,
now it is: VIDEO_CARDS="intel i915 nvidia"
I also spoke to a guy who's one of the gentoo devs, he told me that the 'intel' option is obsolete now and one can have a working system with just "i915 nvidia". Didn't get much expanation from him, sadly, and later he confirmed that was wrong and intel is still needed. This confused me a little bit but, anyway, I have X working, so should be ok for now