<!--
SPDX-FileCopyrightText: (c) 2025-2026 run0edit authors <https://github.com/HastD/run0edit>

SPDX-License-Identifier: Apache-2.0 OR MIT
-->

# run0edit

`run0edit` is to
[`run0`](https://www.freedesktop.org/software/systemd/man/257/run0.html) what
`sudoedit` is to `sudo` (in its most basic usage).

`run0edit` allows a permitted user to edit a file as root. Authorization uses
the same mechanism as `run0`, which typically takes the form of a password
prompt. The file (if it already exists) is copied to a temporary location and
opened in an unprivileged editor; if modified, the edited file contents are
copied back to the original location when the editor is closed.

If the editor exits with an abnormal status code or copying the data back to the
original location fails, then the temporary file will be left in a directory of
the form `/tmp/run0edit-*` (where `*` is replaced with a randomly generated
suffix).

The choice of editor can be customized by writing the path to a text editor (for
example, `/usr/bin/vim`) to one of the following (listed in order of priority):

- The `--editor` command-line argument to `run0edit`;
- The file `/etc/run0edit/editor.conf` (must be an absolute path); or
- The environment variables `VISUAL` or `EDITOR`.

If none of these point to an executable file, `run0edit` will default to using
`nano` or `vi`.

> [!NOTE]
> Editors that make use of JIT compilation (such as Neovim built with LuaJIT)
> might not work: as a security measure, `run0edit` makes use of systemd
> sandboxing settings, including `MemoryDenyWriteExecute`, which prevents code
> generated dynamically at runtime from being executed.

`run0edit` can also be used to edit files that have the immutable attribute set.
In this case, the user will be informed of the presence of the immutable
attribute and asked whether they wish to continue editing; if so, the immutable
attribute will be removed before the edited file contents are copied back to the
original location and then reapplied afterward. This introduces a brief window
during which another user could also edit the file (if they were permitted to do
so if it were not for the immutable attribute), so to protect against this, the
file is compared with the edited temporary file after the immutable attribute is
reapplied, and `run0edit` gives an error message if the file contents do not
match.

## Installation

For installation via system package manager, .deb and .rpm packages can be
downloaded from the
[latest release](https://github.com/HastD/run0edit/releases/latest). For
automatic updates on Fedora-based distros, the package is also available in a
[Copr repository](https://copr.fedorainfracloud.org/coprs/amaranth/run0edit/).

Manual installation:

```sh
git clone https://github.com/HastD/run0edit
cd run0edit
mkdir -p build
meson setup build -Dprefix=/usr
meson compile -C build
run0 meson install -C build
```

Note: `run0edit` requires systemd version 256 or newer.

## Usage

```
run0edit [--editor EDITOR] [--background COLOR] FILE
run0edit [--help | -h]
run0edit [--version | -v]
```

(There is also a `--debug` option meant for debugging purposes; it causes the
program to exit with a stack trace in various error states instead of printing a
more user-friendly error message.)
