lol
1{
2 config,
3 lib,
4 options,
5 pkgs,
6 ...
7}:
8let
9 cfg = config.services.peerflix;
10 opt = options.services.peerflix;
11
12 configFile = pkgs.writeText "peerflix-config.json" ''
13 {
14 "connections": 50,
15 "tmp": "${cfg.downloadDir}"
16 }
17 '';
18
19in
20{
21
22 ###### interface
23
24 options.services.peerflix = {
25 enable = lib.mkOption {
26 description = "Whether to enable peerflix service.";
27 default = false;
28 type = lib.types.bool;
29 };
30
31 stateDir = lib.mkOption {
32 description = "Peerflix state directory.";
33 default = "/var/lib/peerflix";
34 type = lib.types.path;
35 };
36
37 downloadDir = lib.mkOption {
38 description = "Peerflix temporary download directory.";
39 default = "${cfg.stateDir}/torrents";
40 defaultText = lib.literalExpression ''"''${config.${opt.stateDir}}/torrents"'';
41 type = lib.types.path;
42 };
43 };
44
45 ###### implementation
46
47 config = lib.mkIf cfg.enable {
48 systemd.tmpfiles.rules = [
49 "d '${cfg.stateDir}' - peerflix - - -"
50 ];
51
52 systemd.services.peerflix = {
53 description = "Peerflix Daemon";
54 wantedBy = [ "multi-user.target" ];
55 after = [ "network.target" ];
56 environment.HOME = cfg.stateDir;
57
58 preStart = ''
59 mkdir -p "${cfg.stateDir}"/{torrents,.config/peerflix-server}
60 ln -fs "${configFile}" "${cfg.stateDir}/.config/peerflix-server/config.json"
61 '';
62
63 serviceConfig = {
64 ExecStart = "${pkgs.nodePackages.peerflix-server}/bin/peerflix-server";
65 User = "peerflix";
66 };
67 };
68
69 users.users.peerflix = {
70 isSystemUser = true;
71 group = "peerflix";
72 };
73 users.groups.peerflix = { };
74 };
75}