about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/web-apps/pict-rs.nix
blob: 983342c37732a1e7e65f2aea40e94e2ae06ea1e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{ lib, pkgs, config, ... }:

let
  cfg = config.services.pict-rs;
  inherit (lib) maintainers mkOption types;

  is03 = lib.versionOlder cfg.package.version "0.4.0";

in
{
  meta.maintainers = with maintainers; [ happysalada ];
  meta.doc = ./pict-rs.md;

  options.services.pict-rs = {
    enable = lib.mkEnableOption (lib.mdDoc "pict-rs server");

    package = lib.mkPackageOption pkgs "pict-rs" { };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/pict-rs";
      description = lib.mdDoc ''
        The directory where to store the uploaded images & database.
      '';
    };

    repoPath = mkOption {
      type = types.nullOr (types.path);
      default = null;
      description = lib.mdDoc ''
        The directory where to store the database.
        This option takes precedence over dataDir.
      '';
    };

    storePath = mkOption {
      type = types.nullOr (types.path);
      default = null;
      description = lib.mdDoc ''
        The directory where to store the uploaded images.
        This option takes precedence over dataDir.
      '';
    };

    address = mkOption {
      type = types.str;
      default = "127.0.0.1";
      description = lib.mdDoc ''
        The IPv4 address to deploy the service to.
      '';
    };

    port = mkOption {
      type = types.port;
      default = 8080;
      description = lib.mdDoc ''
        The port which to bind the service to.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    services.pict-rs.package = lib.mkDefault (
      # An incompatible db change happened in the transition from 0.3 to 0.4.
      if lib.versionAtLeast config.system.stateVersion "23.11"
      then pkgs.pict-rs
      else pkgs.pict-rs_0_3
    );

    # Account for config differences between 0.3 and 0.4
    assertions = [
      {
        assertion = !is03 || (cfg.repoPath == null && cfg.storePath == null);
        message = ''
          Using `services.pict-rs.repoPath` or `services.pict-rs.storePath` with pict-rs 0.3 or older has no effect.
        '';
      }
    ];

    systemd.services.pict-rs = {
      # Pict-rs split it's database and image storage paths in 0.4.0.
      environment =
        if is03 then {
          PICTRS__PATH = cfg.dataDir;
          PICTRS__ADDR = "${cfg.address}:${toString cfg.port}";
        } else {
          PICTRS__REPO__PATH = if cfg.repoPath != null then cfg.repoPath else "${cfg.dataDir}/sled-repo";
          PICTRS__STORE__PATH = if cfg.storePath != null then cfg.storePath else "${cfg.dataDir}/files";
          PICTRS__SERVER__ADDR = "${cfg.address}:${toString cfg.port}";
        };
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        DynamicUser = true;
        StateDirectory = "pict-rs";
        ExecStart = if is03 then "${lib.getBin cfg.package}/bin/pict-rs" else "${lib.getBin cfg.package}/bin/pict-rs run";
      };
    };
  };

}