As of systemd v256, the systemctl(8) subcommand edit now has the --stdin option.
From the v256 release notes:
'systemctl edit --stdin' allows creation of unit files and drop-ins
with contents supplied via standard input. This is useful when creating
configuration programmatically; the tool takes care of figuring out
the file name, creating any directories, and reloading the manager
afterwards.
From the latest systemctl man page:
If --stdin is specified, the new contents will be read from standard
input. In this mode, the old contents of the file are discarded.
To quickly create create a drop-in file we can redirect any text we want in the final override file to systemctl(8) on stdin, for example, by using a Here-Document:
systemctl edit --stdin sddm <<EOF
[Service]
ExecStartPre=/bin/sleep 5
EOF
This creates the drop-in file /etc/systemd/system/sddm.service.d/override.conf with the contents between the "words" EOF and reloads the systemd configuration in a way that is equivalent to daemon-reload.
If using a systemd version earlier than v256, you can change which editor systemctl(8) uses for its edit subcommand with the SYSTEMD_EDITOR environment variable. This essentially constructs a command whose final argument is always a systemctl(8) created temporary file that, once written to, is then moved to override.conf.
Because of this, we can use tee(1) as the SYSTEMD_EDITOR and then redirect any text we want from the shell to the entire systemctl(8) command on stdin. For example, redirecting a Here-Document:
SYSTEMD_EDITOR=tee systemctl edit sddm <<EOF
[Service]
ExecStartPre=/bin/sleep 5
EOF
This creates the drop-in file /etc/systemd/system/sddm.service.d/override.conf with the contents between the "words" EOF.
Under-the-hood the edit subcommand looks something like this:
# Create drop-in directory for service
mkdir -p /etc/systemd/system/sddm.service.d
# Create empty temporary file
touch /etc/systemd/system/sddm.service.d/.\#override.conf846580011dbe64db
# Pass temporary file as last argument of SYSTEMD_EDITOR
tee /etc/systemd/system/sddm.service.d/.\#override.conf846580011dbe64db<<EOF
[Service]
ExecStartPre=/bin/sleep 5
EOF
# Rename temporary file
mv /etc/systemd/system/sddm.service.d/{.\#override.conf846580011dbe64db,override.conf}
After the unit has been edited, the systemd configuration is reloaded automatically (in a way that is equivalent to daemon-reload).