Skip to content

Commit 73f3c9b

Browse files
nixos/paperless: move paperless-manage to proper systemPackage (#367496)
2 parents 9ea79e7 + 36a3c6c commit 73f3c9b

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

nixos/doc/manual/release-notes/rl-2505.section.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@
223223
- The `services.locate` module does no longer support findutil's `locate` due to its inferior performance compared to `mlocate` and `plocate`. The new default is `plocate`.
224224
As the `service.locate.localuser` option only applied when using findutil's `locate`, it has also been removed.
225225

226+
- `services.paperless` now installs `paperless-manage` as a normal system package instead of creating a symlink in `/var/lib/paperless`.
227+
`paperless-manage` now also changes to the appropriate user when being executed.
228+
226229
- `kmonad` is now hardened by default using common `systemd` settings.
227230
If KMonad is used to execute shell commands, hardening may make some of them fail. In that case, you can disable hardening using {option}`services.kmonad.keyboards.<name>.enableHardening` option.
228231

nixos/modules/services/misc/paperless.nix

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,22 @@ let
3232
else toString s
3333
) cfg.settings);
3434

35-
manage = pkgs.writeShellScript "manage" ''
35+
manage = pkgs.writeShellScriptBin "paperless-manage" ''
3636
set -o allexport # Export the following env vars
3737
${lib.toShellVars env}
3838
${lib.optionalString (cfg.environmentFile != null) "source ${cfg.environmentFile}"}
39-
exec ${cfg.package}/bin/paperless-ngx "$@"
39+
40+
cd '${cfg.dataDir}'
41+
sudo=exec
42+
if [[ "$USER" != ${cfg.user} ]]; then
43+
${
44+
if config.security.sudo.enable then
45+
"sudo='exec ${config.security.wrapperDir}/sudo -u ${cfg.user} -E'"
46+
else
47+
">&2 echo 'Aborting, paperless-manage must be run as user `${cfg.user}`!'; exit 2"
48+
}
49+
fi
50+
$sudo ${lib.getExe cfg.package} "$@"
4051
'';
4152

4253
defaultServiceConfig = {
@@ -94,14 +105,13 @@ in
94105
type = lib.types.bool;
95106
default = false;
96107
description = ''
97-
Enable Paperless.
108+
Whether to enable Paperless-ngx.
98109
99-
When started, the Paperless database is automatically created if it doesn't
100-
exist and updated if the Paperless package has changed.
110+
When started, the Paperless database is automatically created if it doesn't exist
111+
and updated if the Paperless package has changed.
101112
Both tasks are achieved by running a Django migration.
102113
103-
A script to manage the Paperless instance (by wrapping Django's manage.py) is linked to
104-
`''${dataDir}/paperless-manage`.
114+
A script to manage the Paperless-ngx instance (by wrapping Django's manage.py) is available as `paperless-manage`.
105115
'';
106116
};
107117

@@ -139,8 +149,7 @@ in
139149
A file containing the superuser password.
140150
141151
A superuser is required to access the web interface.
142-
If unset, you can create a superuser manually by running
143-
`''${dataDir}/paperless-manage createsuperuser`.
152+
If unset, you can create a superuser manually by running `paperless-manage createsuperuser`.
144153
145154
The default superuser name is `admin`. To change it, set
146155
option {option}`settings.PAPERLESS_ADMIN_USER`.
@@ -288,6 +297,8 @@ in
288297
};
289298

290299
config = lib.mkIf cfg.enable (lib.mkMerge [ {
300+
environment.systemPackages = [ manage ];
301+
291302
services.redis.servers.paperless.enable = lib.mkIf enableRedis true;
292303

293304
services.postgresql = lib.mkIf cfg.database.createLocally {
@@ -336,7 +347,9 @@ in
336347
environment = env;
337348

338349
preStart = ''
339-
ln -sf ${manage} ${cfg.dataDir}/paperless-manage
350+
# remove old papaerless-manage symlink
351+
# TODO: drop with NixOS 25.11
352+
[[ -L '${cfg.dataDir}/paperless-manage' ]] && rm '${cfg.dataDir}/paperless-manage'
340353
341354
# Auto-migrate on first run or if the package has changed
342355
versionFile="${cfg.dataDir}/src-version"
@@ -504,10 +517,10 @@ in
504517
OnSuccess = services;
505518
};
506519
enableStrictShellChecks = true;
520+
path = [ manage ];
507521
script = ''
508-
./paperless-manage document_exporter ${cfg.exporter.directory} ${lib.cli.toGNUCommandLineShell {} cfg.exporter.settings}
522+
paperless-manage document_exporter ${cfg.exporter.directory} ${lib.cli.toGNUCommandLineShell {} cfg.exporter.settings}
509523
'';
510524
};
511-
})
512-
]);
525+
})]);
513526
}

nixos/tests/paperless.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ import ./make-test-python.nix ({ lib, ... }: {
9999
# Double check that our attrset option override works as expected
100100
cmdline = node.succeed("grep 'paperless-manage' $(systemctl cat paperless-exporter | grep ExecStart | cut -f 2 -d=)")
101101
print(f"Exporter command line {cmdline!r}")
102-
assert cmdline.strip() == "./paperless-manage document_exporter /var/lib/paperless/export --compare-checksums --delete --no-progress-bar --no-thumbnail", "Unexpected exporter command line"
102+
assert cmdline.strip() == "paperless-manage document_exporter /var/lib/paperless/export --compare-checksums --delete --no-progress-bar --no-thumbnail", "Unexpected exporter command line"
103103
104104
test_paperless(simple)
105105
simple.send_monitor_command("quit")

0 commit comments

Comments
 (0)