Skip to content

recipes: install-service in the user's systemd manager with SERVICE_SCOPE=user - #69

Merged
DevelopmentCats merged 1 commit into
mainfrom
devcats/recipe-service-user-scope
Nov 12, 2026
Merged

DevelopmentCats merged 1 commit into
mainfrom
devcats/recipe-service-user-scope

Conversation

@DevelopmentCats

Copy link
Copy Markdown
Collaborator

recipes install-service always wrote to /etc/systemd/system and ran sudo systemctl, so a machine where the serving user has no sudo could not keep a recipe up across reboots.
The single-model unit already supports SERVICE_SCOPE=user; this brings the recipe unit in line.

What changes

  • Scope follows the existing SERVICE_SCOPE config key. There is no new flag.
  • With SERVICE_SCOPE=user:
    • The recipe unit and its watchdog go to ~/.config/systemd/user (honours XDG_CONFIG_HOME) and are driven with systemctl --user.
    • The unit has no User= line, does not order against docker.service, and is WantedBy=default.target.
    • The install runs loginctl enable-linger so the units start at boot without a login. If that fails it prints a note with the sudo loginctl enable-linger command and carries on.
    • --user naming someone other than $USER is rejected, since a user manager can only run units as its own user.
    • Standing down the native unit uses systemctl --user too, which is where SERVICE_SCOPE=user puts that unit.
    • The "Not started" hint prints systemctl --user enable --now ....
  • The watchdog gets a hidden --user-scope flag on recipes health-check. It checks and restarts the unit through the user manager and keeps its miss count under $XDG_RUNTIME_DIR instead of /run.
  • System-scope output (unit files, health units, hints) is byte-for-byte unchanged.

Not changed

migrate / upgradeRecipeUnits still only look in /etc/systemd/system.
A user-scope unit installed by this release already names its profile, so it has nothing to upgrade.

Checks

  • gofmt -l ., go vet ./..., npx prettier@3.9.6 --check .
  • go test -race ./... passes except internal/detect TestProbeReportsThisMachine, which fails the same way on main in this workspace (it depends on free RAM).
  • tools/recipediff: vet and tests pass.
  • CLI reference regenerated with go run ./scripts/gendocs.

Copilot AI balanced review requested due to automatic review settings November 12, 2026 20:20
@DevelopmentCats
DevelopmentCats merged commit 63314bb into main Nov 12, 2026
5 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The watchdog’s shared fallback counter allows cross-user interference, and user-scope services lack retries after startup failures, potentially leaving recipes down when the watchdog is disabled.

2 open issues
What changed in this PR

This PR extends recipe services to use the existing SERVICE_SCOPE=user setting, allowing installation in the serving user's systemd manager without sudo.

Changes:

  • Adds user-scoped unit installation, service control, lingering, and startup hints while preserving system-scope output.
  • Routes watchdog checks and restarts through the user manager and stores counters in the user runtime directory.
  • Adds tests and updates user documentation, generated CLI help, and the changelog.
File Description
internal/​cli/​recipeservice.go Adds scope-aware installation, unit rendering, and service control.
internal/​cli/​recipeservice_internal_test.go Tests user-scoped units, lingering, paths, and user validation.
internal/​cli/​recipehealth.go Adds user-scoped watchdog commands and counter paths.
internal/​cli/​recipehealth_internal_test.go Tests watchdog scope handling and runtime paths.
docs/​reference/​cli/​recipes/​install-service.md Updates generated help for user-scoped installation.
docs/​multi-node.md Documents installation without sudo and lingering behavior.
CHANGELOG.md Records recipe service support for SERVICE_SCOPE.

Review effort: Balanced

Comment on lines +75 to +78
if dir == "" {
dir = os.TempDir()
}
return filepath.Join(dir, strings.TrimSuffix(unit, ".service")+"-misses")
func renderRecipeSystemdUnit(u recipeSystemdUnit) string {
after, userLine, wantedBy := "network-online.target docker.service", "User="+u.User+"\n", "multi-user.target"
if u.UserScope {
after, userLine, wantedBy = "network-online.target", "", "default.target"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants