paths
paths
¶
Platform-aware path resolution for the terok ecosystem.
Provides generic namespace resolvers that any sibling package can
call to place its state/config/runtime under the shared terok/
namespace.
Resolution priority for each resolver:
- Package-specific override (
env_varargument) TEROK_ROOTenv var (namespace-wide override; state only)- Platform default (FHS root paths or XDG dirs via
platformdirs)
Layered config-file reading (system → installation prefix → user)
lives here too: config_file_paths
names the layers, writable_config_path
the one an operator edits, and the read_config_* helpers merge them.
Packages that want schema validation compose
ConfigStack over the same layers.
__all__ = ['config_file_paths', 'host_uid', 'namespace_config_dir', 'namespace_runtime_dir', 'namespace_state_dir', 'read_config_section', 'read_config_top_level', 'writable_config_path']
module-attribute
¶
host_uid()
¶
Return this process's UID as its parent user namespace sees it.
Inside an unprivileged user namespace (rootless podman / crun
hook, sandboxed CI runner, unshare -U), os.geteuid() returns
the inner-userns UID — typically 0 even when the operator
really ran the program as UID 1000. Network peers (D-Bus
SO_PEERCRED, AUTH EXTERNAL) and kernel-level checks see the
outer (host) UID via the userns uid_map translation, so a
process that advertises its inner UID over the wire is rejected for
a credential mismatch. This helper hands callers the outer UID those
peers expect.
The mapping comes from /proc/self/uid_map. When it is
unavailable (macOS, BSD, exotic chroot) or no row covers the
effective UID, the bare geteuid() answer is returned — correct on
systems without Linux user namespaces.
One hop, not all of them. uid_map's second column is a UID in the
parent user namespace (user_namespaces(7)), which equals the
initial namespace only at a single level of nesting. Rootless podman's
--userns=keep-id — how terok runs agent containers — nests twice, so
an unprivileged process there translates to 0: the parent's view,
not the host's.
That makes this the wrong question to ask about privilege: a 0 here
says "some ancestor namespace calls me root", not "I can write
/etc". Privilege is decided by _is_root() instead, which asks
whether uid 0 is held in the initial namespace. What this is right
for is what it was written for: telling a peer outside our namespace
which UID it will see us as (D-Bus SO_PEERCRED / AUTH EXTERNAL).
Source code in src/terok_util/paths.py
config_file_paths()
¶
Ordered config.yml locations with scope labels (lowest → highest priority).
TEROK_CONFIG_FILE → single override (no layering). Otherwise:
/etc/terok/config.yml (system) → sys.prefix/etc/terok/config.yml
(prefix) → ~/.config/terok/config.yml (user). Root's writable user
file is the system file; it appears once, at highest priority.
Public so consumers can render an "edit one of these to override X" hint to the operator (which file gets the highest priority, where on disk the operator would put the override, etc.).
Source code in src/terok_util/paths.py
writable_config_path()
¶
The config.yml an operator edits and every sibling package writes.
The highest-priority layer of
config_file_paths: the explicit
TEROK_CONFIG_FILE when set, otherwise the user file (the system
file for root). One derivation for every writer, so the path one
package writes is the path the others read.
Source code in src/terok_util/paths.py
read_config_section(section)
¶
Read a top-level section from layered terok configs (cached, fail-silent).
Merges every layer of
config_file_paths via
ConfigStack — higher layers
override lower ones at the leaf level. Lazy-imports
config_stack so importing paths doesn't drag the YAML
parser into a process that only needs the platform defaults.
Source code in src/terok_util/paths.py
read_config_top_level(key)
¶
Read a top-level scalar / list / mapping from layered terok configs.
Counterpart to
read_config_section for
keys whose value isn't a dict — e.g. the ecosystem-wide
experimental: true opt-in or a bare log_level: debug knob.
Returns the merged value (user wins over system) or None when
the key is absent or the config files can't be loaded. Cached for
the lifetime of the process; reaches for the _config_top_level_cache
private to flush in tests.
Source code in src/terok_util/paths.py
namespace_state_dir(subdir='', *, env_var=None)
¶
Resolve a state directory under the terok/ namespace.
Priority:
- env_var (package-specific override, e.g.
TEROK_SANDBOX_STATE_DIR) TEROK_ROOTenv var (namespace override)config.yml→paths.root(Podman model — all packages honour it)- Platform default (
/var/lib/terok/<subdir>for root, XDG data dir otherwise)
env_var is keyword-only so a positional second argument can never accidentally be reinterpreted as an override name.
Source code in src/terok_util/paths.py
namespace_config_dir(subdir='', *, env_var=None)
¶
Resolve a config directory under the terok/ namespace.
Priority: env_var → /etc/terok/<subdir> (root) → platformdirs
→ ~/.config/terok/<subdir>. env_var is keyword-only.
Source code in src/terok_util/paths.py
namespace_runtime_dir(subdir='', *, env_var=None)
¶
Resolve a runtime directory under the terok/ namespace.
Priority: env_var → /run/terok/<subdir> (root)
→ $XDG_RUNTIME_DIR/terok/<subdir> → $XDG_STATE_HOME/terok/<subdir>
→ ~/.local/state/terok/<subdir>. env_var is keyword-only.