@@ -0,0 +1,111 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ inherit (pkgs) ipfs;
7+
8+ cfg = config.services.ipfs;
9+
10+ ipfsFlags = ''${if cfg.autoMigrate then "--migrate" else ""} ${if cfg.enableGC then "--enable-gc" else ""} ${toString cfg.extraFlags}'';
11+
12+in
13+
14+{
15+
16+ ###### interface
17+
18+ options = {
19+
20+ services.ipfs = {
21+
22+ enable = mkEnableOption "Interplanetary File System";
23+
24+ user = mkOption {
25+ type = types.str;
26+ default = "ipfs";
27+ description = "User under which the IPFS daemon runs";
28+ };
29+
30+ group = mkOption {
31+ type = types.str;
32+ default = "ipfs";
33+ description = "Group under which the IPFS daemon runs";
34+ };
35+
36+ dataDir = mkOption {
37+ type = types.str;
38+ default = "/var/lib/ipfs";
39+ description = "The data dir for IPFS";
40+ };
41+
42+ autoMigrate = mkOption {
43+ type = types.bool;
44+ default = false;
45+ description = ''
46+ Whether IPFS should try to migrate the file system automatically.
47+ '';
48+ };
49+
50+ enableGC = mkOption {
51+ type = types.bool;
52+ default = false;
53+ description = ''
54+ Whether to enable automatic garbage collection.
55+ '';
56+ };
57+
58+ extraFlags = mkOption {
59+ type = types.listOf types.str;
60+ description = "Extra flags passed to the IPFS daemon";
61+ default = [];
62+ };
63+ };
64+ };
65+
66+ ###### implementation
67+
68+ config = mkIf cfg.enable {
69+ environment.systemPackages = [ pkgs.ipfs ];
70+
71+ users.extraUsers = mkIf (cfg.user == "ipfs") {
72+ ipfs = {
73+ group = cfg.group;
74+ home = cfg.dataDir;
75+ createHome = false;
76+ uid = config.ids.uids.ipfs;
77+ description = "IPFS daemon user";
78+ };
79+ };
80+
81+ users.extraGroups = mkIf (cfg.group == "ipfs") {
82+ ipfs = {
83+ gid = config.ids.gids.ipfs;
84+ };
85+ };
86+
87+ systemd.services.ipfs = {
88+ description = "IPFS Daemon";
89+
90+ wantedBy = [ "multi-user.target" ];
91+ after = [ "network.target" "local-fs.target" ];
92+ path = [ pkgs.ipfs pkgs.su pkgs.bash ];
93+
94+ preStart =
95+ ''
96+ install -m 0755 -o ${cfg.user} -g ${cfg.group} -d ${cfg.dataDir}
97+ if [[ ! -d ${cfg.dataDir}/.ipfs ]]; then
98+ cd ${cfg.dataDir}
99+ ${pkgs.su}/bin/su -s ${pkgs.bash}/bin/sh ${cfg.user} -c "${ipfs}/bin/ipfs init"
100+ fi
101+ '';
102+
103+ serviceConfig = {
104+ ExecStart = "${ipfs}/bin/ipfs daemon ${ipfsFlags}";
105+ User = cfg.user;
106+ Group = cfg.group;
107+ PermissionsStartOnly = true;
108+ };
109+ };
110+ };
111+}