How do I override TwigEnvironment? I have been trying for a few hours to beautify the output that twig generates, such a way that will be properly indented with tabs.
For example, I would like the following output:

&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;


To become something like:
For example, I would like the following output:

&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;/body&gt;
&lt;/html&gt;


I would like for this "autoindentation" to happen for all tags, because it makes the markup clean and easier to debug.
So I found this question: https://stackoverflow.com/questions/11878728/rendering-clean-tidy-html-with-twig-templates
And since I have been trying to make drupal load that twig indent extension and that twig lexer extension.

My code so far:

root.info.yml

name: Root
type: module
description: ‘Provides basic cosmetic functionality that should be in core.’
core: 8.x
version: 0.0.1


root.services.yml

services:
  # Override the default Twig Environment so we can add our custom Twig Lexer.
  twig:
    class: Drupal\root\TwigEnvironment
    arguments: ['@app.root', '@cache.default', '%twig_extension_hash%', '@state', '@twig.loader', '%twig.config%']
    tags:
      - { name: service_collector, tag: 'twig.extension', call: addExtension }
  root.twig.Indent:
    class: Drupal\root\Indent
    tags:
      - {name: twig.extension}
  root.twig.Lexer:
    class: Drupal\root\Lexer
    tags:
      - {name: twig.lexer}


and the Indent.php and Lexer.php as provided in that answer.
The problem is that the lexer wold not load because the tag twig.lexer does not exist.

So I tried to add the following code:

namespace Drupal\root;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Template\TwigEnvironment as CoreTwigEnvironment;
use Drupal\root\Indent;

/**
 * Twig environment that uses temporary file storage.
 */
class TwigEnvironment extends CoreTwigEnvironment {

  public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, $options = []) {
    parent::__construct($root, $cache, $twig_extension_hash, $state, $loader, $options);
    // Set our own lexer to the Twig environment.
    $this-&gt;setLexer(new Lexer($this));
  }

}


and drupal does not let me replace the Twig Environment and comes with the following complaint:

&lt;em class="placeholder"&gt;LogicException&lt;/em&gt;: Unable to register extension &amp;quot;drupal_core&amp;quot; as extensions have already been initialized. in &lt;em class="placeholder"&gt;Twig_Environment-&amp;gt;addExtension()&lt;/em&gt; (line &lt;em class="placeholder"&gt;941&lt;/em&gt; of &lt;em class="placeholder"&gt;/var/www/konquer.technology/vendor/twig/twig/lib/Twig/Environment.php&lt;/em&gt;). &lt;pre class="backtrace"&gt;call_user_func_array(Array, Array) (Line: 322)


Is there a way to do this? I can't find any documentation on this topic.
Or maybe another way to achieve my goal of indented twig output
You're using an undefined twig.lexer tag there.

It looks as though you want that tag to work as a service_collector like twig.extension, but calling setLexer instead of addExtension.

For that, you probably need to add a line to your twig service definition, below the other tag:

- { name: service_collector, tag: 'twig.lexer', call: setLexer }


(Note: I don't know if this is actually good architecture. The service_collector tag is normally used when there can be multiple extensions, and multiple lexers would just replace each other. But it should work.)