1{ config, lib, pkgs, ... }:
2let
3 cfg = config.services.fourStore;
4 stateDir = "/var/lib/4store";
5 fourStoreUser = "fourstore";
6 run = "${pkgs.su}/bin/su -s ${pkgs.stdenv.shell} ${fourStoreUser}";
7in
8with lib;
9{
10
11 ###### interface
12
13 options = {
14
15 services.fourStore = {
16
17 enable = mkOption {
18 default = false;
19 description = "Whether to enable 4Store RDF database server.";
20 };
21
22 database = mkOption {
23 default = "";
24 description = "RDF database name. If it doesn't exist, it will be created. Databases are stored in ${stateDir}.";
25 };
26
27 options = mkOption {
28 default = "";
29 description = "Extra CLI options to pass to 4Store.";
30 };
31
32 };
33
34 };
35
36
37 ###### implementation
38
39 config = mkIf cfg.enable {
40
41 assertions = singleton
42 { assertion = cfg.enable -> cfg.database != "";
43 message = "Must specify 4Store database name.";
44 };
45
46 users.extraUsers = singleton
47 { name = fourStoreUser;
48 uid = config.ids.uids.fourstore;
49 description = "4Store database user";
50 home = stateDir;
51 };
52
53 services.avahi.enable = true;
54
55 systemd.services."4store" = {
56 after = [ "network.target" ];
57 wantedBy = [ "multi-user.target" ];
58
59 preStart = ''
60 mkdir -p ${stateDir}/
61 chown ${fourStoreUser} ${stateDir}
62 if ! test -e "${stateDir}/${cfg.database}"; then
63 ${run} -c '${pkgs.rdf4store}/bin/4s-backend-setup ${cfg.database}'
64 fi
65 '';
66
67 script = ''
68 ${run} -c '${pkgs.rdf4store}/bin/4s-backend -D ${cfg.options} ${cfg.database}'
69 '';
70 };
71 };
72}