Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
docs: add systemd agent service configuration to Polykey CLI guide
  • Loading branch information
xrissoula committed May 16, 2025
commit 04ef0aa44e6e6d8ff18ba3a7aa81bcff63e95437
193 changes: 193 additions & 0 deletions docs/Getting-Started/polykey-cli/running-the-agent-with-systemd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# Polykey Agent: Service and Programs Module Documentation

This guide introduces and explains the newly added `services` and `programs`
modules for managing the Polykey Agent using `systemd`. These modules were
introduced as part of a broader effort to improve automation, reliability, and
user experience across both user-level and system-wide contexts.

---

## Background

The Polykey Agent is a long-lived background process that facilitates secure
secret management and distributed key infrastructure. Traditionally, users had
to manually start the agent from the terminal. To streamline this, two modules
were introduced:

- `programs`: Configures **user-level services** for personal development and
desktop use.
- `services`: Configures **system-level services** for shared machines and
server environments.

These modules utilize `systemd`, a service manager used in most Linux
distributions.

---

## What is `systemd`?

`systemd` is the default init and service manager in most Linux distros. It
allows you to:

- Start, stop, and restart background services.
- Automatically launch services at boot or login.
- View logs and monitor service health.

`systemd` uses unit files (like `.service`) to define how services behave.

---

## Key Concepts

### User vs System Services

| Mode | Controlled By | Suitable For | Config Path |
| ---------- | ------------- | ------------------------------- | ------------------------- |
| **User** | Regular user | Local development, personal use | `~/.config/systemd/user/` |
| **System** | Root/admin | Shared systems, production use | `/etc/systemd/system/` |

The new modules are designed to target both these contexts.

---

## Programs Module (User Services)

The `programs` module sets up a user-level `systemd` service that:

- Starts the agent on login.
- Runs the agent under the current user.
- Stores logs in the user's journal.

### Setup Instructions (User Mode)

1. Ensure the Polykey binary is installed and accessible via `$PATH`.
2. Copy the service file to:

```sh
mkdir -p ~/.config/systemd/user
cp polykey-agent.service ~/.config/systemd/user/
```

3. Enable and start the service:

```sh
systemctl --user daemon-reload
systemctl --user enable polykey-agent
systemctl --user start polykey-agent
```

4. Verify it's running:

```sh
systemctl --user status polykey-agent
journalctl --user -u polykey-agent
```
Copy link

@brynblack brynblack May 20, 2025

Choose a reason for hiding this comment

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

This section needs to explain how to set up Polykey using Nix configurations, as that is how the service is actually set up. It does not use .service files. Instead, you configure it like the following, under your home-manager service (at least for the user-level service):

    polykey = {
      enable = true;
      passwordFilePath = "%h/.polykeypass";
      recoveryCodeOutPath = "%h/.polykeyrecovery";
    };
  • enable will enable the service.
  • passwordFilePath will set the path to grab the vault password from.
  • recoveryCodeOutPath will set where to create/grab the recovery code from.

There may need to be an explanation on how Polykey is integrated into a NixOS system.

Take a look at:


---

## Services Module (System Services)

The `services` module sets up a root-owned service that:

- Runs globally for all users.
- Is launched at boot.
- Is managed from `/etc/systemd/system/`.

### Setup Instructions (System Mode)

1. Copy the service file to:

```sh
sudo cp polykey-agent.service /etc/systemd/system/
```

2. Enable and start the service:

```sh
sudo systemctl daemon-reload
sudo systemctl enable polykey-agent
sudo systemctl start polykey-agent
```

3. Check status:

```sh
sudo systemctl status polykey-agent
sudo journalctl -u polykey-agent
```

Choose a reason for hiding this comment

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

This needs to also be inline with the comment I put above. There is typically no need to manually modify service files under a NixOS system. Unless in the future we make a generic Linux-agnostic version of the Polykey Agent.

But make sure to distinguish the system-level service from the user-level service.


---

## Configuration Details

The service files can be customized:

- `ExecStart` can point to any valid Polykey binary.
- `Environment` variables like `NODE_ENV`, `POLYKEY_DATA_PATH` can be passed in.
- Restart policies and timeouts can be modified.

To override a system service without editing the base file:

```sh
sudo systemctl edit polykey-agent
```

Choose a reason for hiding this comment

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

The service can be customised, but not through service files. It is done using the home-manager configuration, as described in my first comment. Make sure to look at the links I put in it.


---

## Handling Secrets & Recovery Codes

The new modules support secure handling of recovery codes and agent secrets:

- Set environment variables or use configuration files in the home directory.
- Avoid running agents as root unless necessary.
- For system mode, ensure secrets are stored in restricted root-only paths.

---

## Troubleshooting

- **"Service not found"**:

- Run `daemon-reload` after copying or editing unit files.

- **"Permission denied"**:

- Ensure system-level services are started with `sudo`.

- **Service not starting**:

- Run `journalctl -u polykey-agent` for logs.

- **User services not auto-starting**:

- Check that `linger` is enabled for the user:

```sh
sudo loginctl enable-linger $USER
```
Copy link

@brynblack brynblack May 20, 2025

Choose a reason for hiding this comment

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

This technically is valid, as services can be manually restarted or analysed if there are problems. However, the daemon-reload part is if you are manually editing the service files, which isn't really needed for NixOS.

Definitely add an explainer on how to check the status of the service, using commands such as:

systemctl status --user polykey.service - checks the status of the service
systemctl restart --user polykey.service - restarts the service

Copy link
Contributor

@aryanjassal aryanjassal May 21, 2025

Choose a reason for hiding this comment

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

Christina doesn't have a nix system or even a linux system, so she can't run the systemctl commands herself.

This is the output of systemctl status --user polykey.service on my system which you can use for reference.

[aryanj@zenith:~]$ systemctl --user status polykey.service    
● polykey.service - Polykey Agent
     Loaded: loaded (/home/aryanj/.config/systemd/user/polykey.service; enabled; preset: ignored)
     Active: active (running) since Wed 2025-05-21 06:02:43 AEST; 5h 23min ago
 Invocation: 4bb789c30b8d4a398da384b83e7bbb34
    Process: 1007615 ExecStartPre=/nix/store/2y0dbl6jmvcwrv4h549d0pgcm3dl8p4k-polykey-cli-0.18.13/bin/polykey --password-file /home/aryanj/.pkpass --node-path /home/ary>
   Main PID: 1007651 (polykey-agent)
      Tasks: 41 (limit: 37944)
     Memory: 497.7M (peak: 715.4M)
        CPU: 1h 41min 59.690s
     CGroup: /user.slice/user-1000.slice/[email protected]/app.slice/polykey.service
             └─1007651 polykey-agent
May 21 11:24:19 zenith polykey[1007651]: WARN:polykey.PolykeyAgent.task v0q1d4u6t5to0193h3sb26b4810:Failed - Reason: ErrorProviderCall
May 21 11:24:19 zenith polykey[1007651]: WARN:polykey.PolykeyAgent.task v0q1d4u6trdo01efitk10jlvfdg:Failed - Reason: ErrorProviderCall
May 21 11:24:52 zenith polykey[1007651]: WARN:polykey.PolykeyAgent.task v0q1d4unqqho014bu3oicllik2k:Failed - Reason: ErrorProviderCall
May 21 11:25:12 zenith polykey[1007651]: WARN:polykey.PolykeyAgent.task v0q1d4v1h09o0137uglrcpi2dno:Failed - Reason: ErrorProviderCall
(... more logs)

When you run systemctl restart --user polykey.service, it should not give you any output but polykey should be restarted in the background automatically.


---

## Use Cases

- **Developers**: Enable `programs` to automatically start the agent at login.
- **Sysadmins**: Deploy `services` module for always-on availability of the
agent across all users.
- **Security-sensitive installations**: Customize environment securely and
inspect logs via `journalctl`.

---

## Next Steps

- Finalize documentation with visual diagrams (systemd flow, unit layering).
- Incorporate examples of overriding default behavior.
- Validate this guide on different distros (e.g. Ubuntu, Fedora, Arch).

---

## Related References

- [systemd documentation](https://www.freedesktop.org/wiki/Software/systemd/)
- [Polykey PR #138](https://github.com/MatrixAI/Polykey-CLI/pull/138)
- [CLI Installation Guide](./installation.md)