about summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/shiori.nix
blob: df3eeaef161833fb931c328581be541ab00648e6 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{ config, lib, pkgs, ... }:

let cfg = config.services.shiori;
in {
  options = {
    services.shiori = {
      enable = lib.mkEnableOption "Shiori simple bookmarks manager";

      package = lib.mkPackageOption pkgs "shiori" { };

      address = lib.mkOption {
        type = lib.types.str;
        default = "";
        description = ''
          The IP address on which Shiori will listen.
          If empty, listens on all interfaces.
        '';
      };

      port = lib.mkOption {
        type = lib.types.port;
        default = 8080;
        description = "The port of the Shiori web application";
      };

      webRoot = lib.mkOption {
        type = lib.types.str;
        default = "/";
        example = "/shiori";
        description = "The root of the Shiori web application";
      };

      environmentFile = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        default = null;
        example = "/path/to/environmentFile";
        description = ''
          Path to file containing environment variables.
          Useful for passing down secrets.
          <https://github.com/go-shiori/shiori/blob/master/docs/Configuration.md#overall-configuration>
        '';
      };

      databaseUrl = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
        default = null;
        example = "postgres:///shiori?host=/run/postgresql";
        description = "The connection URL to connect to MySQL or PostgreSQL";
      };
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.shiori = {
      description = "Shiori simple bookmarks manager";
      wantedBy = [ "multi-user.target" ];
      after = [ "postgresql.service" "mysql.service" ];
      environment = {
        SHIORI_DIR = "/var/lib/shiori";
      } // lib.optionalAttrs (cfg.databaseUrl != null) {
        SHIORI_DATABASE_URL = cfg.databaseUrl;
      };

      serviceConfig = {
        ExecStart =
          "${cfg.package}/bin/shiori server --address '${cfg.address}' --port '${
            toString cfg.port
          }' --webroot '${cfg.webRoot}'";

        DynamicUser = true;
        StateDirectory = "shiori";
        # As the RootDirectory
        RuntimeDirectory = "shiori";

        # Security options
        EnvironmentFile =
          lib.optional (cfg.environmentFile != null) cfg.environmentFile;
        BindReadOnlyPaths = [
          "/nix/store"

          # For SSL certificates, and the resolv.conf
          "/etc"
        ] ++ lib.optional (config.services.postgresql.enable &&
                           cfg.databaseUrl != null &&
                           lib.strings.hasPrefix "postgres://" cfg.databaseUrl)
            "/run/postgresql"
          ++ lib.optional (config.services.mysql.enable &&
                           cfg.databaseUrl != null &&
                           lib.strings.hasPrefix "mysql://" cfg.databaseUrl)
            "/var/run/mysqld";

        CapabilityBoundingSet = "";
        AmbientCapabilities = "CAP_NET_BIND_SERVICE";

        DeviceAllow = "";

        LockPersonality = true;

        MemoryDenyWriteExecute = true;

        PrivateDevices = true;
        PrivateUsers = true;

        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;

        RestrictNamespaces = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
        RestrictRealtime = true;
        RestrictSUIDSGID = true;

        RootDirectory = "/run/shiori";

        SystemCallArchitectures = "native";
        SystemCallErrorNumber = "EPERM";
        SystemCallFilter = [
          "@system-service"
          "~@cpu-emulation"
          "~@debug"
          "~@keyring"
          "~@memlock"
          "~@obsolete"
          "~@privileged"
          "~@setuid"
        ];
      };
    };
  };

  meta.maintainers = with lib.maintainers; [ minijackson CaptainJawZ ];
}