{
    "mode": "man",
    "parameter": "systemd.environment-generator",
    "section": "7",
    "url": "https://www.chedong.com/phpMan.php/man/systemd.environment-generator/7/json",
    "generated": "2026-06-03T07:46:54Z",
    "synopsis": "",
    "sections": {
        "NAME": {
            "content": "systemd.environment-generator - systemd environment file generators\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "",
            "subsections": [
                {
                    "name": "/lib/systemd/system-environment-generators/some-generator",
                    "content": ""
                },
                {
                    "name": "/usr/lib/systemd/user-environment-generators/some-generator",
                    "content": "/run/systemd/system-environment-generators/*\n/etc/systemd/system-environment-generators/*\n/usr/local/lib/systemd/system-environment-generators/*\n/lib/systemd/system-environment-generators/*\n\n/run/systemd/user-environment-generators/*\n/etc/systemd/user-environment-generators/*\n/usr/local/lib/systemd/user-environment-generators/*\n/usr/lib/systemd/user-environment-generators/*\n\n"
                }
            ]
        },
        "DESCRIPTION": {
            "content": "Generators are small executables that live in /lib/systemd/system-environment-generators/ and\nother directories listed above.  systemd(1) will execute those binaries very early at the\nstartup of each manager and at configuration reload time, before running the generators\ndescribed in systemd.generator(7) and before starting any units. Environment generators can\noverride the environment that the manager exports to services and other processes.\n\nGenerators are loaded from a set of paths determined during compilation, as listed above.\nSystem and user environment generators are loaded from directories with names ending in\nsystem-environment-generators/ and user-environment-generators/, respectively. Generators\nfound in directories listed earlier override the ones with the same name in directories lower\nin the list. A symlink to /dev/null or an empty file can be used to mask a generator, thereby\npreventing it from running. Please note that the order of the two directories with the\nhighest priority is reversed with respect to the unit load path, and generators in /run/\noverwrite those in /etc/.\n\nAfter installing new generators or updating the configuration, systemctl daemon-reload may be\nexecuted. This will re-run all generators, updating environment configuration. It will be\nused for any services that are started subsequently.\n\nEnvironment file generators are executed similarly to unit file generators described in\nsystemd.generator(7), with the following differences:\n\n•   Generators are executed sequentially in the alphanumerical order of the final component\nof their name. The output of each generator output is immediately parsed and used to\nupdate the environment for generators that run after that. Thus, later generators can use\nand/or modify the output of earlier generators.\n\n•   Generators are run by every manager instance, their output can be different for each\nuser.\n\nIt is recommended to use numerical prefixes for generator names to simplify ordering.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "",
            "subsections": [
                {
                    "name": "Example 1. A simple generator that extends an environment variable if a directory exists in",
                    "content": ""
                },
                {
                    "name": "the file system",
                    "content": "# 50-xdg-data-dirs.sh\n\n#!/bin/bash\n\n# set the default value\nXDGDATADIRS=\"${XDGDATADIRS:-/usr/local/share/:/usr/share}\"\n\n# add a directory if it exists\nif [[ -d /opt/foo/share ]]; then\nXDGDATADIRS=\"/opt/foo/share:${XDGDATADIRS}\"\nfi\n\n# write our output\necho \"XDGDATADIRS=${XDGDATADIRS}\"\n"
                },
                {
                    "name": "Example 2. A more complicated generator which reads existing configuration and mutates one",
                    "content": ""
                },
                {
                    "name": "variable",
                    "content": "# 90-rearrange-path.py\n\n#!/usr/bin/env python3\n\n\"\"\"\n\nProof-of-concept systemd environment generator that makes sure that bin dirs\nare always after matching sbin dirs in the path.\n(Changes /sbin:/bin:/foo/bar to /bin:/sbin:/foo/bar.)\n\nThis generator shows how to override the configuration possibly created by\nearlier generators. It would be easier to write in bash, but let's have it\nin Python just to prove that we can, and to serve as a template for more\ninteresting generators.\n\n\"\"\"\n\nimport os\nimport pathlib\n\ndef rearrangebinsbin(path):\n\"\"\"Make sure any pair of .../bin, .../sbin directories is in this order\n\n>>> rearrangebinsbin('/bin:/sbin:/usr/sbin:/usr/bin')\n'/bin:/sbin:/usr/bin:/usr/sbin'\n\"\"\"\nitems = [pathlib.Path(p) for p in path.split(':')]\nfor i in range(len(items)):\nif 'sbin' in items[i].parts:\nind = items[i].parts.index('sbin')\nbin = pathlib.Path(*items[i].parts[:ind], 'bin', *items[i].parts[ind+1:])\nif bin in items[i+1:]:\nj = i + 1 + items[i+1:].index(bin)\nitems[i], items[j] = items[j], items[i]\nreturn ':'.join(p.asposix() for p in items)\n\nif name == 'main':\npath = os.environ['PATH'] # This should be always set.\n# If it's not, we'll just crash, which is OK too.\nnew = rearrangebinsbin(path)\nif new != path:\nprint('PATH={}'.format(new))\n"
                },
                {
                    "name": "Example 3. Debugging a generator",
                    "content": "SYSTEMDLOGLEVEL=debug VARA=something VARB=\"something else\" \\\n/lib/systemd/system-environment-generators/path-to-generator\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "systemd-environment-d-generator(8), systemd.generator(7), systemd(1), systemctl(1)\n\n\n\nsystemd 249                                                         SYSTEMD.ENVIRONMENT-GENERATOR(7)",
            "subsections": []
        }
    },
    "summary": "systemd.environment-generator - systemd environment file generators",
    "flags": [],
    "examples": [
        "# 50-xdg-data-dirs.sh",
        "#!/bin/bash",
        "# set the default value",
        "XDGDATADIRS=\"${XDGDATADIRS:-/usr/local/share/:/usr/share}\"",
        "# add a directory if it exists",
        "if [[ -d /opt/foo/share ]]; then",
        "XDGDATADIRS=\"/opt/foo/share:${XDGDATADIRS}\"",
        "fi",
        "# write our output",
        "echo \"XDGDATADIRS=${XDGDATADIRS}\"",
        "# 90-rearrange-path.py",
        "#!/usr/bin/env python3",
        "\"\"\"",
        "Proof-of-concept systemd environment generator that makes sure that bin dirs",
        "are always after matching sbin dirs in the path.",
        "(Changes /sbin:/bin:/foo/bar to /bin:/sbin:/foo/bar.)",
        "This generator shows how to override the configuration possibly created by",
        "earlier generators. It would be easier to write in bash, but let's have it",
        "in Python just to prove that we can, and to serve as a template for more",
        "interesting generators.",
        "\"\"\"",
        "import os",
        "import pathlib",
        "def rearrangebinsbin(path):",
        "\"\"\"Make sure any pair of .../bin, .../sbin directories is in this order",
        ">>> rearrangebinsbin('/bin:/sbin:/usr/sbin:/usr/bin')",
        "'/bin:/sbin:/usr/bin:/usr/sbin'",
        "\"\"\"",
        "items = [pathlib.Path(p) for p in path.split(':')]",
        "for i in range(len(items)):",
        "if 'sbin' in items[i].parts:",
        "ind = items[i].parts.index('sbin')",
        "bin = pathlib.Path(*items[i].parts[:ind], 'bin', *items[i].parts[ind+1:])",
        "if bin in items[i+1:]:",
        "j = i + 1 + items[i+1:].index(bin)",
        "items[i], items[j] = items[j], items[i]",
        "return ':'.join(p.asposix() for p in items)",
        "if name == 'main':",
        "path = os.environ['PATH'] # This should be always set.",
        "# If it's not, we'll just crash, which is OK too.",
        "new = rearrangebinsbin(path)",
        "if new != path:",
        "print('PATH={}'.format(new))",
        "SYSTEMDLOGLEVEL=debug VARA=something VARB=\"something else\" \\",
        "/lib/systemd/system-environment-generators/path-to-generator"
    ],
    "see_also": [
        {
            "name": "systemd-environment-d-generator",
            "section": "8",
            "url": "https://www.chedong.com/phpMan.php/man/systemd-environment-d-generator/8/json"
        },
        {
            "name": "systemd.generator",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/systemd.generator/7/json"
        },
        {
            "name": "systemd",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/systemd/1/json"
        },
        {
            "name": "systemctl",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/systemctl/1/json"
        },
        {
            "name": "SYSTEMD.ENVIRONMENT-GENERATOR",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/SYSTEMD.ENVIRONMENT-GENERATOR/7/json"
        }
    ]
}