Run systemd services with `DynamicUser` unless there is a concrete reason to give them a fixed user, and create static users with fixed UIDs and GIDs only when such a reason exists. Use when defining or modifying systemd services in NixOS, or when about to add a system user for a service.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Systemd Dynamic User?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/ncaq-systemd-dynamic-user)
---
name: systemd-dynamic-user
description: Run systemd services with `DynamicUser` unless there is a concrete reason to give them a fixed user, and create static users with fixed UIDs and GIDs only when such a reason exists. Use when defining or modifying systemd services in NixOS, or when about to add a system user for a service.
user-invocable: false
---
# systemdのサービスは`DynamicUser`で動かす
systemdのサービスは、動作に問題が無ければ`DynamicUser`で動かしてください。
固定のUIDとGIDを持つユーザは、固定が要る理由がある時だけ作ります。
```nix
{
systemd.services.example.serviceConfig = {
DynamicUser = true;
StateDirectory = "example";
};
}
```
`DynamicUser`のユーザは、ユニットの起動時に割り当てられ、停止すると解放されます。
UIDの割り当てとユーザの宣言を管理せずに済みます。
`ProtectSystem=strict`や`PrivateTmp`なども暗に有効になり、
サービスが書き込める場所が絞られます。
書き込みが要るディレクトリは、
`StateDirectory`、`CacheDirectory`、`LogsDirectory`、`RuntimeDirectory`で用意します。
systemdが動的なユーザの所有にして管理します。
nixpkgsのモジュールには、既定で`DynamicUser`を使うものが多くあります。
理由無く`DynamicUser = lib.mkForce false`で固定のユーザに変えないでください。
## 固定のユーザが要る場合
固定のユーザを足す前に、固定が要る理由があるかを確かめます。
例えば以下の場合です。
- 別のコンテナやホストにいる相手が、UIDをユーザ名に解決して認証する。
PostgreSQLのpeer認証が当たります。
- ホストとコンテナのように境界をまたいで、
他のサービスと同じデータ領域を同じユーザで読み書きする。
- 大きな永続データを持ち、UIDが変わるとsystemdがデータ全体の所有者を再帰的に書き換えることになる。
ephemeralなコンテナでは起動のたびに全データをchownすることになりかねません。
APIを叩くだけのサービスのように理由が無いものは、`DynamicUser`で動かします。
固定のユーザにした場合は、その理由をコメントに書いてください。