1{ lib, pkgs, config, ... }:
2
3let
4 cfg = config.services.pict-rs;
5 inherit (lib) maintainers mkOption types;
6
7 is03 = lib.versionOlder cfg.package.version "0.4.0";
8
9in
10{
11 meta.maintainers = with maintainers; [ happysalada ];
12 meta.doc = ./pict-rs.md;
13
14 options.services.pict-rs = {
15 enable = lib.mkEnableOption "pict-rs server";
16
17 package = lib.mkPackageOption pkgs "pict-rs" { };
18
19 dataDir = mkOption {
20 type = types.path;
21 default = "/var/lib/pict-rs";
22 description = ''
23 The directory where to store the uploaded images & database.
24 '';
25 };
26
27 repoPath = mkOption {
28 type = types.nullOr (types.path);
29 default = null;
30 description = ''
31 The directory where to store the database.
32 This option takes precedence over dataDir.
33 '';
34 };
35
36 storePath = mkOption {
37 type = types.nullOr (types.path);
38 default = null;
39 description = ''
40 The directory where to store the uploaded images.
41 This option takes precedence over dataDir.
42 '';
43 };
44
45 address = mkOption {
46 type = types.str;
47 default = "127.0.0.1";
48 description = ''
49 The IPv4 address to deploy the service to.
50 '';
51 };
52
53 port = mkOption {
54 type = types.port;
55 default = 8080;
56 description = ''
57 The port which to bind the service to.
58 '';
59 };
60 };
61
62 config = lib.mkIf cfg.enable {
63 services.pict-rs.package = lib.mkDefault (
64 # An incompatible db change happened in the transition from 0.3 to 0.4.
65 if lib.versionAtLeast config.system.stateVersion "23.11"
66 then pkgs.pict-rs
67 else pkgs.pict-rs_0_3
68 );
69
70 # Account for config differences between 0.3 and 0.4
71 assertions = [
72 {
73 assertion = !is03 || (cfg.repoPath == null && cfg.storePath == null);
74 message = ''
75 Using `services.pict-rs.repoPath` or `services.pict-rs.storePath` with pict-rs 0.3 or older has no effect.
76 '';
77 }
78 ];
79
80 systemd.services.pict-rs = {
81 # Pict-rs split it's database and image storage paths in 0.4.0.
82 environment =
83 if is03 then {
84 PICTRS__PATH = cfg.dataDir;
85 PICTRS__ADDR = "${cfg.address}:${toString cfg.port}";
86 } else {
87 PICTRS__REPO__PATH = if cfg.repoPath != null then cfg.repoPath else "${cfg.dataDir}/sled-repo";
88 PICTRS__STORE__PATH = if cfg.storePath != null then cfg.storePath else "${cfg.dataDir}/files";
89 PICTRS__SERVER__ADDR = "${cfg.address}:${toString cfg.port}";
90 };
91 wantedBy = [ "multi-user.target" ];
92 serviceConfig = {
93 DynamicUser = true;
94 StateDirectory = "pict-rs";
95 ExecStart = if is03 then "${lib.getBin cfg.package}/bin/pict-rs" else "${lib.getBin cfg.package}/bin/pict-rs run";
96 };
97 };
98 };
99
100}